【问题标题】:ansible playbook fails when grep is not finding anything当 grep 没有找到任何东西时,ansible playbook 失败
【发布时间】:2017-05-15 20:23:52
【问题描述】:

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

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

但出现错误:

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

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

【问题讨论】:

标签: regex grep ansible


【解决方案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"

【讨论】:

  • ignore_errors: yes - 没用,但failed_when: false 有帮助。谢谢。
  • 声望低于 15 人的投票被记录,但不改变公开显示的帖子得分 =(
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-09-16
  • 2021-10-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-17
相关资源
最近更新 更多