Regex 当grep找不到任何东西时ansible playbook就会失败
正在尝试运行简单的ansible playbook命令:Regex 当grep找不到任何东西时ansible playbook就会失败,regex,grep,ansible,Regex,Grep,Ansible,正在尝试运行简单的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 probl
---
- 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]”,它运行正常,但这不是我需要的。
原因可能是什么?如何绕过这一点 我猜你的grep找不到任何东西,所以 返回rc=1,ansible将其解释为任务失败,您可以使用
ignore\u错误:yes
或失败\u当:false
要绕过此“失败”,不要使用sudo
,请使用been
---
- 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"
我想,你想从我开始提取包裹数量,对吗。。?然后您可以将命令更改为sudo dpkg-l | awk'{print$2}'| grep-c'^i'可能重复的
ignore\u错误:yes
-不起作用,但失败\u当:false
帮助时。谢谢。声誉低于15的人所投的票会被记录下来,但不会改变公开显示的帖子分数=(