当 grep 结果为空时,Ansible shell 模块返回错误 [英] Ansible shell module returns error when grep results are empty

查看:26
本文介绍了当 grep 结果为空时,Ansible shell 模块返回错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Ansible 的 shell 模块来查找特定字符串并将其存储在变量中.但是如果 grep 没有找到任何东西,我就会收到错误消息.

I am using Ansible's shell module to find a particular string and store it in a variable. But if grep did not find anything I am getting an error.

示例:

- name: Get the http_status
  shell: grep "http_status=" /var/httpd.txt
  register: cmdln
  check_mode: no

当我运行此 Ansible 剧本时,如果 http_status 字符串不存在,剧本将停止.我没有收到 stderr.

When I run this Ansible playbook if http_status string is not there, playbook is stopped. I am not getting stderr.

即使没有找到字符串,如何让 Ansible 不间断地运行?

How can I make Ansible run without interruption even if the string is not found?

推荐答案

如您所见,如果 grep 退出代码不为零,ansible 将停止执行.您可以使用 ignore_errors 忽略它.

Like you observed, ansible will stop execution if the grep exit code is not zero. You can ignore it with ignore_errors.

另一个技巧是将 grep 输出通过管道传送到 cat.所以 cat 退出代码将始终为零,因为它的标准输入是 grep 的标准输出.如果有匹配项和不匹配项,它都可以工作.试试吧.

Another trick is to pipe the grep output to cat. So cat exit code will always be zero since its stdin is grep's stdout. It works if there is a match and also when there is no match. Try it.

- name: Get the http_status
  shell: grep "http_status=" /var/httpd.txt | cat
  register: cmdln
  check_mode: no

这篇关于当 grep 结果为空时,Ansible shell 模块返回错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆