Note
This module is part of ansible-core and included in all Ansible installations. In most cases, you can use the short module name expect even without specifying the collections keyword. However, we recommend you use the Fully Qualified Collection Name (FQCN) ansible.builtin.expect for easy linking to the module documentation and to avoid conflicting with other collections that may have the same module name.
$HOME and operations like "<", ">", "|", and "&" will not work.The below requirements are needed on the host that executes this module.
Parameter | Comments |
|---|---|
chdir path | Change into this directory before running the command. |
command string / required | The command module takes command to run. |
creates path | A filename, when it already exists, this step will not be run. |
echo boolean | Whether or not to echo out your response strings. Choices:
|
removes path | A filename, when it does not exist, this step will not be run. |
responses dictionary / required | Mapping of prompt regular expressions and corresponding answer(s). Each key in The value of each key is a string or list of strings. If the value is a string and the prompt is encountered multiple times, the answer will be repeated. Provide the value as a list to give different answers for successive matches. |
timeout any | Amount of time in seconds to wait for the expected strings. Use Default: |
Attribute | Support | Description |
|---|---|---|
check_mode | Support: none | Can run in check_mode and return changed status prediction without modifying target, if not supported the action will be skipped. |
diff_mode | Support: none | Will return details on what has changed (or possibly needs changing in check_mode), when in diff mode |
platform | Platform: posix | Target OS/families that can be operated against |
Note
<, >, |, and so on), you must specify a shell in the command such as /bin/bash -c "/path/to/something | grep else".(?i).pexpect library used by this module operates with a search window of 2000 bytes, and does not use a multiline regex match. To perform a start of line bound match, use a pattern like (?m^pattern).base64.See also
Runs a local script on a remote node after transferring it.
Execute shell commands on targets.
- name: Case insensitive password string match
ansible.builtin.expect:
command: passwd username
responses:
(?i)password: "MySekretPa$$word"
# you don't want to show passwords in your logs
no_log: true
- name: Match multiple regular expressions and demonstrate individual and repeated responses
ansible.builtin.expect:
command: /path/to/custom/command
responses:
Question:
# give a unique response for each of the 3 hypothetical prompts matched
- response1
- response2
- response3
# give the same response for every matching prompt
"^Match another prompt$": "response"
- name: Multiple questions with responses
ansible.builtin.expect:
command: /path/to/custom/command
responses:
"Please provide your name":
- "Anna"
"Database user":
- "{{ db_username }}"
"Database password":
- "{{ db_password }}"
© 2012–2018 Michael DeHaan
© 2018–2025 Red Hat, Inc.
Licensed under the GNU General Public License version 3.
https://docs.ansible.com/ansible/latest/collections/ansible/builtin/expect_module.html