1

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   
  • 1
    yum remove lynx -y – jordanm Mar 23 '18 at 23:05
  • 2
    Don't do this. Use package or (deprecated) yum. – Michael Hampton Mar 23 '18 at 23:09
  • 1
    @MichaelHampton Where from did you get information that yum module is deprecated? – techraf Mar 23 '18 at 23:22
  • 1
    @techraf Currently (ansible 2.4) package encompasses all supported package managers. You can still use yum if you wish, for the time being, but it will probably go away eventually. It has some performance improvements in 2.5 and after that the other ones (yum, apt, etc) will likely get deprecated. – Michael Hampton Mar 23 '18 at 23:24
  • 1
    "Will likely get" doesn't mean it is "deprecated". – techraf Mar 23 '18 at 23:29
4

You would be better off using the yum module.

---
- hosts: localhost 
  remote_user: ansible
  become: yes
  become_method: sudo
  connection: ssh
  gather_facts: yes
  tasks:
  - yum: 
      name: lynx
      state: absent

Or package

---
- hosts: localhost 
  remote_user: ansible
  become: yes
  become_method: sudo
  connection: ssh
  gather_facts: yes
  tasks:
  - pacakge: 
      name: lynx
      state: absent

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.

| improve this answer | |
2

Thank you guys for you suggestions. Yes, I'll use YUM. Anyway, below regex expression resolved my problem.

Installed.*\nIs this ok \[y\/N\]: y


Code:
---
- 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.*\nIs this ok \[y\/N\]: y 
        # Is this ok \[y\/N\]: y
      echo: yes
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.