expect – Executes a command and responds to prompts¶
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¶
The below requirements are needed on the host that executes this module.
- python >= 2.6
- pexpect >= 3.3
Parameters¶
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"
. - The question, or key, under responses is a python regex match. Case insensitive searches are indicated with a prefix of
?i
. - By default, if a question is encountered multiple times, its 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.
- The expect module is designed for simple scenarios. For more complex needs, consider the use of expect code with the shell or script modules. (An example is part of the shell module documentation)
See Also¶
See also
- script – Runs a local script on a remote node after transferring it
- The official documentation on the script module.
- shell – Execute shell commands on targets
- The official documentation on the shell module.
Examples¶
- name: Case insensitive password string match
expect:
command: passwd username
responses:
(?i)password: "MySekretPa$$word"
# you don't want to show passwords in your logs
no_log: true
- name: Generic question with multiple different responses
expect:
command: /path/to/custom/command
responses:
Question:
- response1
- response2
- response3
Status¶
- This module is not guaranteed to have a backwards compatible interface. [preview]
- This module is maintained by the Ansible Community. [community]
Authors¶
- Matt Martz (@sivel)
Hint
If you notice any issues in this documentation, you can edit this document to improve it.