ansible - 没有“FAILED”json输出的ansible中的条件
问题描述
我想根据是否安装了某个 RPM 来运行命令。我有一系列类似于以下内容的任务:
- name: This is the test for the RPM
command: rpm -q some-rpm
register: rpm_is_installed
ignore_errors: True
- name: Command to run if the RPM is installed
command: some_command_to_run
when: rpm_is_installed is succeeded
从功能上讲,这是可行的。但是,如果测试失败(即未安装 RPM),我会得到一个令人讨厌的 JSON 打印输出,它在执行“rpm -q some-rpm”时描述了失败。这被忽略了,所以 ansible 继续,一切都按预期有效运行,但我想抑制该错误输出,因此每次运行这些任务时都不会看到该错误消息。
我确实找到了这篇建议在测试任务中添加“no_log:True”的帖子。这不会抑制我的 ansible 版本中的输出,而是将输出更改为:
失败的!=> {"censored": "由于为此结果指定了 'no_log: true',因此输出已被隐藏"}
有没有办法抑制失败命令的输出?
- Ansible:2.4.2.0
- 操作系统:RHEL 7.7
解决方案
没有办法抑制错误日志,但您可以做的一件事是使用failed_when
.
no_log: true
--> 这将审查输出。
ignore_errors: true
--> 这将忽略错误并继续播放。
failed_when: false
--> 这不会让任务失败。
剧本看起来像这样:
- name: This is the test for the RPM
command: rpm -q some-rpm
register: rpm_is_installed
failed_when: false
no_log: true
除了ignore_errors
,这里是failed_when
您可以准确定义导致任务失败的原因的选项。我们不应该让任务一开始就失败。在这种情况下,任务永远不会失败。
没有 failed_when 的输出:
fatal: [localhost]: FAILED! => {
"censored": "the output has been hidden due to the fact that 'no_log: true' was specified for this result",
"changed": true
}
failed_when 的输出:
changed: [localhost] => {
"censored": "the output has been hidden due to the fact that 'no_log: true' was specified for this result",
"changed": true
}
这些是您可以用来忽略、审查和不让任务失败的唯一组合。