ansible.builtin.expect module – Executes a command and responds to prompts

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 FQCN for easy linking to the module documentation and to avoid conflicting with other collections that may have the same module name.

New in version 2.0: of ansible.builtin

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

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:

  • no ← (default)

  • yes

removes

path

A filename, when it does not exist, this step will not be run.

responses

dictionary / required

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

integer

Amount of time in seconds to wait for the expected strings. Use null to disable timeout.

Default: 30

Attributes

Attribute

Support

Description

check_mode

Support: none

Can run in check_mode and return changed status prediction without modifying target

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

Notes

Note

  • If you want to run a command through the shell (say you are using <, >, |, and so on), 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.

  • The 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

  • 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 ansible.builtin.expect module is designed for simple scenarios. For more complex needs, consider the use of expect code with the ansible.builtin.shell or ansible.builtin.script modules. (An example is part of the ansible.builtin.shell module documentation).

See Also

See also

ansible.builtin.script

The official documentation on the ansible.builtin.script module.

ansible.builtin.shell

The official documentation on the ansible.builtin.shell module.

Examples

- name: Case insensitive password string match
  ansible.builtin.expect:
    ansible.builtin.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
  ansible.builtin.expect:
    ansible.builtin.command: /path/to/custom/command
    responses:
      Question:
        - response1
        - response2
        - response3

Authors

  • Matt Martz (@sivel)