当grep找不到任何东西时,ansible playbook失败了

时间:2017-05-15 20:23:52

标签: regex grep ansible

尝试运行简单的ansible playbook命令:

(224,244)

但是收到错误:

---
- 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"

如果我正在将我的正则表达式更改为 '^ i [i]' - 它运行正常,但这不是我需要的。 可能是什么原因,以及如何绕过这个?

1 个答案:

答案 0 :(得分:1)

我猜你的grep没找到任何东西,所以它 返回rc = 1,ansible将其解释为任务失败,您可以使用 ignore_errors: yesfailed_when: false要绕过此“失败”,并且不使用sudo,请使用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"