W3cubDocs

/Ansible

expect - Executes a command and responds to prompts

New in version 2.0.

Synopsis

The expect module executes a command and responds to prompts The given command will be executed on all selected nodes. It will not be processed through the shell, so variables like $HOME and operations like "<", ">", "|", and "&" will not work

Requirements (on host that executes module)

  • python >= 2.6
  • pexpect >= 3.3

Options

parameter required default choices comments
chdir
no
cd into this directory before running the command
command
yes
the command module takes command to run.
creates
no
a filename, when it already exists, this step will not be run.
echo
no
Whether or not to echo out your response strings
removes
no
a filename, when it does not exist, this step will not be run.
responses
yes
Mapping of expected string/regex and string to respond with. If the response is a list, successive matches return successive responses. List functionality is new in 2.1.
timeout
no 30
Amount of time in seconds to wait for the expected strings

Examples

# Case insensitve password string match
- expect:
    command: passwd username
    responses:
      (?i)password: "MySekretPa$$word"

# Generic question with multiple different responses
- expect:
    command: /path/to/custom/command
    responses:
      Question:
        - response1
        - response2
        - response3

Notes

Note

If you want to run a command through the shell (say you are using <, >, |, etc), you must specify a shell in the command such as /bin/bash -c "/path/to/something | grep else"

Note

The question, or key, under responses is a python regex match. Case insensitive searches are indicated with a prefix of ?i

Note

By default, if a question is encountered multiple times, it’s string response will be repeated. If you need different responses for successive question matches, instead of a string response, use a list of strings as the response. The list functionality is new in 2.1

This is an Extras Module

For more information on what this means please read Extras Modules

For help in developing on modules, should you be so inclined, please read Community Information & Contributing, developing_test_pr and Developing Modules.

© 2012–2016 Michael DeHaan
© 2016 Red Hat, Inc.
Licensed under the GNU General Public License version 3.
https://docs.ansible.com/ansible/expect_module.html