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"