Updated April 6, 2023
Definition of Ansible Expect
Ansible Expect command is a built-in Ansible module and comes default with the ansible installation and this module executes a command on the remote nodes and responds to the prompt that is provided with the response parameter and it can be useful for passing the arguments to the command. Although it uses a command parameter to run the command, it doesn’t use the shell or win_command to run the commands.
Syntax:
- Ansible Expect module uses the “command” parameter to run the command.
- Ansible Expect module uses the “response” parameter to take the arguments for the input.
Other supported parameters:
- chdir:
Changes into this directory before running the command.
- command: (Required)
The command module takes command to run.
- creates:
It creates a file and if it already exists then this step won’t be executed.
- echo:
Values: no (default) / yes
Whether or not to echo out your output string
- responses: (required)
This parameter is to pass the series of strings or arguments to the input expected by the command parameter. You can pass the responses in the form of the string or the regex (Regular expression). If the response is a list, successive matches returns the successive responses.
- timeout:
Value: 30 (Default)
Amount of the time in seconds to wait for the expected strings. Use null to disable the timeout.
How Expect works in Ansible?
Ansible “expect” command is the built-in command and installed by default with the ansible installation. The main purpose of this command to execute the command on the remote nodes using the “command” parameter and provide the required arguments to run the command using the “response” parameter.
This “expect” command is suitable for all most all the Ansible modules like Windows, Unix, etc.
When we use the command parameter, it doesn’t pass it through the shell but it is a separate command. If we need to specify the shell command or the cmd command then we need to specify the path of the cmd, shell, or the PowerShell to execute the command. For example, running PowerShell command will look like this,
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe Read-host "Enter Username"
In the case of cmd, we can provide
C:\WINDOWS\system32\cmd.exe yourcommand
Similarly, for the shell, we can provide,
/bin/bash -c "/path/to/something | grep else"
When we pass the argument, we can provide the mapping of the string or the python regex in the response parameter. Case insensitive searches are indicated with a prefix of a “?i”
If we have the series of the responses to pass then we can provide them as shown below.
- name: Generic question with multiple different responses
ansible.builtin.expect:
command: /path/to/custom/command
responses:
Question:
- response1
- response2
- response3
In the above example, the command will run from the path given, and then it responds with the series of the commands that we have provided in the responses section. In addition, you can add the various parameters to handle the errors, for example, if we need to fail the task when there are specific keywords in the output then we can use the failed_when parameter.
- name: Generic question with multiple different responses
ansible.builtin.expect:
command: /path/to/custom/command
responses:
Question:
- response1
- response2
- response3
register: outvar
failed_when: condition
Examples
Let us discuss examples of Ansible Expect.
Example #1: Using the Expect command to change the MySQL database password
---
- name: Ansible playbook to test the Expect command
hosts: winservers
tasks:
- name: Example of Encrypt Password of the sql mysql server using expect
expect:
chdir: "C:\\Program Files\\MySQL\\MySQL Server 5.7\\bin"
command: mysql_config_editor.exe set --login-path=3306 --password
responses:
(?i)Enter password: "{{ mysql_password }}"
ansible-playbook expecttest.yml
The above playbook will set the MySQL password on the windows servers group.
Output:
Example #2: Expect the module to change the password on the Unix systems
---
- name: Ansible playbook to test the Expect command
hosts: linuxservers
tasks:
- name: Example of the Expect module to change the password of the user.
expect:
command: passwd testusr
responses:
(?i)password: "P@$$w0rd"
The above playbook will change the password on the Unix system for the username called testusr.
Example #3: Expect a module example to install the DB server bypassing response.
- name: Ansible playbook to test the db installation using Expect module
hosts: winservers
vars:
installdir: 'C:\temp\MYSQLInsallation'
executable_filename: "mysqlinstaller.exe"
db_port: "3306"
db_username: "dbadmin"
db_password: "D@taB@se#123"
tasks:
- name: expect example
expect:
echo: yes
chdir: "{{ installdir }}"
command: "./{{ exectuable_filename }}"
timeout: "300"
responses:
(.*)Please enter your name(.*): "Jack"
(.*)Please enter your age(>*): "25"
(.*)db port(.*): "{{ db_port }}"
(.*)db user(.*): "{{ db_user }}"
(.*)db pass(.*): "{{ db_pass }}"
register: expect_example_result
failed_when: "expect_example_result.rc != 0 and 'Success' not in expect_example_result.stdout"
ansible-playbook expecttest.yml
The above playbook will install the MySQL database with the different parameters provided and in addition
Output:
Example #4: Except module example to install the software on the windows system
- name: Ansible playbook to test the Software installation using Expect module
hosts: winservers
vars:
sourcedir: 'C:\temp\'
installdir: 'C:\program Files\myorgsoft'
executable_command: "myorgsoft.exe"
tasks:
- name: install software using expect module
expect:
echo: yes
chdir: "{{ sourcedir }}"
command: "{{ executable_command }}"
responses:
(.*)Accept terms and conditions: "yes"
InstallationDir: "{{ installdir }}"
CUSTOMATTRIBUTES: "Division=Prod, Business_class=Atlanta, Patching_class: 33456, proxy: localhost"
ForceRestart: "yes"
The above playbook will install the software on the remote windows nodes, add the custom attributes, change the installation directory, and forcefully restart the system.
ansible-playbook expecttest.yml
Output:
Conclusion
Ansible “expect” parameter is helpful when the command requires some responses or the arguments like when we install software we need to provide certain responses like the path to install, accept software agreement, connection strings, etc which makes it easier to pass the responses that the command requires.
Recommended Articles
This is a guide to Ansible Expect. Here we discuss the Definition, syntax, How Expect works in Ansible? examples with code implementation. You may also have a look at the following articles to learn more –