我正在使用Ansible的shell模块来查找特定字符串并将其存储在变量中.但如果grep没有找到任何东西我会收到错误.
例:
- name: Get the http_status
shell: grep "http_status=" /var/httpd.txt
register: cmdln
check_mode: no
Run Code Online (Sandbox Code Playgroud)
当我运行这个Ansible playbook时,如果http_status
没有字符串,则停止播放.我没有得到stderr.
即使找不到字符串,如何在不中断的情况下运行Ansible?
tec*_*raf 41
grep
如果找不到给定的字符串,则按设计返回代码1.如果返回代码不同于0,则设计Ansible将停止执行.您的系统正常运行.
要防止Ansible在此错误上停止播放执行,您可以:
将ignore_errors: yes
参数添加到任务中
使用failed_when:
具有适当条件的参数
因为grep
异常返回错误代码2,所以第二种方法似乎更合适,因此:
- name: Get the http_status
shell: grep "http_status=" /var/httpd.txt
register: cmdln
failed_when: "cmdln.rc == 2"
check_mode: no
Run Code Online (Sandbox Code Playgroud)
您也可以考虑添加,changed_when: false
以便每次都不会将任务报告为"已更改".
所有选项都在Playbooks中的错误处理文档中描述.
hel*_*loV 23
就像你观察到的那样,如果grep
退出代码不为零,ansible将停止执行.你可以忽略它ignore_errors
.
另一个技巧是将grep输出传递给cat
.因此cat
退出代码将始终为零,因为它的stdin是grep的stdout.如果匹配并且没有匹配时它可以工作.试试吧.
- name: Get the http_status
shell: grep "http_status=" /var/httpd.txt | cat
register: cmdln
check_mode: no
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
19194 次 |
最近记录: |