Dimu4
Dimu4

Reputation: 233

ansible playbook fails when grep is not finding anything

Trying to run simple ansible playbook command:

---
- hosts: all
  tasks:
    - name: Check errors during package installation
      shell: sudo dpkg -l | grep -c '^i[^i]'
      register: result
      notify: Fix problems with packages
  handlers:
    - name: Fix problems with packages
      shell: sudo dpkg --configure --pending
      when: result.stdout != "0"

But getting an error:

failed: [jasper01.stage2.qa.whs] => {"changed": true, "cmd": "sudo dpkg -l | grep -c '^i[^i]'", "delta": "0:00:00.045734", "end": "2017-05-15 13:18:14.157175", "rc": 1, "start": "2017-05-15 13:18:14.111441", "warnings": []}
stdout: 0

FATAL: all hosts have already failed -- aborting

If i'm changing my regex to '^i[i]' - it's running fine, but it's not what i need. What could be the reason, and how to bypass this?

Upvotes: 1

Views: 1530

Answers (1)

Berlin
Berlin

Reputation: 1464

I guess your grep is not finding anything, so it returns rc=1 which ansible interprets as the task failing, you can use ignore_errors: yes or failed_when: false to bypass this 'failure', and do not use sudo, use become.

---
- hosts: all
  become: yes
  become_method: sudo
  tasks:
    - name: Check errors during package installation
      shell: dpkg -l | grep -c '^i[^i]'
      ignore_errors: yes
      register: result
      notify: Fix problems with packages
  handlers:
    - name: Fix problems with packages
      shell: dpkg --configure --pending
      when: result.stdout != "0"

Upvotes: 1

Related Questions