I am a newbie in Ansible. I was trying to use expect with Ansible to automate package installation (in below example – uninstall). During lynx package uninstall manually, I get following prompt. In expect response field – I was trying to match this pattern (both lines) but my script fails. Though it works for one line – Is this ok [y/N]: y. Regex that I have used for both lines is Installed size: 5.4 M\nIs this ok [y/N]: y
-----------------------
Installed size: 5.4 M
Is this ok [y/N]:
----------------------
Code 1 (pattern match for both lines – failure):
[ansible@localhost ansible]$ cat main.yml
---
- hosts: localhost
remote_user: ansible
become: yes
become_method: sudo
connection: ssh
gather_facts: yes
tasks:
- name: remove a package
expect:
command: yum remove lynx
responses:
Installed size: 5.4 M\nIs this ok \[y\/N\]: y
#Is this ok \[y\/N\]: y
echo: yes
Output:
[ansible@localhost ansible]$ ansible-playbook main.yml
[DEPRECATION WARNING]: DEFAULT_SUDO_USER option, In favor of become which is a generic framework . This feature will be removed in version 2.8. Deprecation
warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.
ERROR! Syntax Error while loading YAML.
The error appears to have been in '/etc/ansible/main.yml': line 13, column 51, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
responses:
Installed size: 5.4 M\nIs this ok \[y\/N\]: y
^ here
exception type: <class 'yaml.scanner.ScannerError'>
exception: mapping values are not allowed in this context
in "<unicode string>", line 13, column 51
[ansible@localhost ansible]$
=======================================================================
Code 2 (pattern match for one line – works fine):
[ansible@localhost ansible]$ cat main.yml
---
- hosts: localhost
remote_user: ansible
become: yes
become_method: sudo
connection: ssh
gather_facts: yes
tasks:
- name: remove a package
expect:
command: yum remove lynx
responses:
#Installed size: 5.4 M\nIs this ok \[y\/N\]: y
Is this ok \[y\/N\]: y
echo: yes
Output
[ansible@localhost ansible]$
[ansible@localhost ansible]$ ansible-playbook main.yml
[DEPRECATION WARNING]: DEFAULT_SUDO_USER option, In favor of become which is a generic framework . This feature will be removed in version 2.8. Deprecation
warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.
PLAY [localhost] *********************************************************************************************************************************************
TASK [Gathering Facts] ***************************************************************************************************************************************
ok: [127.0.0.1]
TASK [remove a package] **************************************************************************************************************************************
changed: [127.0.0.1]
PLAY RECAP ***************************************************************************************************************************************************
127.0.0.1 : ok=2 changed=1 unreachable=0 failed=0
Best Answer
You would be better off using the yum module.
Or package
You should always use a module over command/shell when possible. Modules give you error checking and idempotency without you having to adding tons of logic to your shell/command.