Skip to content

expect

Collection Note

This module is part of the ansible.builtin collection. To install the collection, use:

ansible-galaxy collection install ansible.builtin
Added in version 2.0.

You need further requirements to be able to use this module, see the Requirements section for details.

Synopsis

  • The M(ansible.builtin.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 C($HOME) and operations like C("<"), C(">"), C("|"), and C("&") will not work.

Requirements

The following Python packages are needed on the host that executes this module:

Parameters

Parameter Defaults / Choices Comments
chdir
path
Change into this directory before running the command.
command
str
required
The command module takes command to run.
creates
path
A filename, when it already exists, this step will B(not) be run.
echo
bool
Whether or not to echo out your response strings.
removes
path
A filename, when it does not exist, this step will B(not) be run.
responses
dict
required
Mapping of prompt regular expressions and corresponding answer(s).
Each key in O(responses) is a Python regex U(https://docs.python.org/3/library/re.html#regular-expression-syntax).
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
raw
Default: 30
Amount of time in seconds to wait for the expected strings. Use V(null) to disable timeout.

Notes

Note

  • If you want to run a command through the shell (say you are using C(<), C(>), C(|), and so on), you must specify a shell in the command such as C(/bin/bash -c "/path/to/something | grep else").
  • Case insensitive searches are indicated with a prefix of C((?i)).
  • The C(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 C((?m)^pattern).
  • The M(ansible.builtin.expect) module is designed for simple scenarios. For more complex needs, consider the use of expect code with the M(ansible.builtin.shell) or M(ansible.builtin.script) modules. (An example is part of the M(ansible.builtin.shell) module documentation).
  • If the command returns non UTF-8 data, it must be encoded to avoid issues. One option is to pipe the output through C(base64).

Examples

- 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 }}"

Authors

  • Matt Martz (@sivel)