Error handling Ansible:模拟一个;“当”时通过;模块
Ansible在模块时提供了一个Error handling Ansible:模拟一个;“当”时通过;模块,error-handling,module,ansible,Error Handling,Module,Ansible,Ansible在模块时提供了一个failed_,允许用户在任务中指定某些失败条件,例如,在stdout或stderr中找到某个字符串 我正试图做相反的事情:如果在stdout或stderr中找到一组字符串,我希望我的任务不会失败。换言之,我希望在模块时接近传递的功能 当返回码为0时,我仍然希望它正常通过 但是如果它失败(rc!=0),那么它应该首先检查某个字符串的出现 也就是说,如果找到某个字符串,不管发生什么,它都会通过 我的推理是这样的: 有很多原因可以解释为什么这项任务无法完成 失败了
failed_,允许用户在任务中指定某些失败条件,例如,在stdout或stderr中找到某个字符串
我正试图做相反的事情:如果在stdout或stderr中找到一组字符串,我希望我的任务不会失败。换言之,我希望在
模块时接近传递的功能
- 当返回码为0时,我仍然希望它正常通过
- 但是如果它失败(rc!=0),那么它应该首先检查某个字符串的出现
- 也就是说,如果找到某个字符串,不管发生什么,它都会通过
我的推理是这样的:
有很多原因可以解释为什么这项任务无法完成
失败了,但其中一些,取决于输出,我不考虑。
作为当前上下文中的失败
有人知道如何实现这一点吗?您最初的问题是这样表述的(使用布尔逻辑使其更简单):
如果在stdout或stderr中找到一组字符串,则执行命令
重新表述你的逻辑:
如果在stdout或stderr中找不到一组字符串,则失败。使用这种逻辑,很容易处理失败的\u。下面是一个片段:
---
- name: Test failed_when as succeed_if
hosts: localhost
connection: local
gather_facts: no
tasks:
- name: "'succeed_if' set of strings in stdout"
command: /bin/echo succeed1
register: command_result
failed_when: "command_result.stdout not in ['succeed1',]"
- name: "'succeed_if' set of strings in stdout (multiple values)"
command: /bin/echo succeed2
register: command_result
failed_when: "command_result.stdout not in ['succeed1', 'succeed2']"
- name: "'succeed_if' set of strings in stderr (multiple values)"
shell: ">&2 /bin/echo succeed2 "
register: command_result
failed_when: "command_result.stderr not in ['succeed1', 'succeed2']"
- name: "'succeed_if' set of strings in stderr (multiple values) or rc != 0"
shell: ">&2 /bin/echo succeed2; /bin/false"
register: command_result
failed_when: "command_result.stderr not in ['succeed1', 'succeed2'] and command_result.rc != 0"
# vim: set ts=2 sts=2 fenc=utf-8 expandtab list:
另外,您可能正在查找的文档是请在此处查看:
- 如果返回代码为0且stderr中没有“PONG”,则不会失败
- 如果strr中有“PONG”,则不会失败
因此,如果列表中的任何一个为False,它就会通过。问题是:当返回代码为0时,我仍然希望它正常通过。但是如果它会失败(rc!=0),那么它应该首先检查某个字符串的出现。只需添加相应的条件(参见最后一个示例…)我认为当
列表被链接时,会失败吗?即,如果command\u result.rc!=0
是真的(这是正常的行为)?不,这是我在引用的票证中写的(请看一下我与mp dehaan的邮件对话)。这在语义上很奇怪。。。如果命令_result.rc!=0,并且命令结果中没有“PONG”。stderr您能告诉我在哪里定义了command\u result.rc
之类的内容吗?register:command\u result
每个模块都返回值。命令模块返回其他rc
,请参见此处:
- name: ping pong redis
command: redis-cli ping
register: command_result
failed_when:
- "'PONG' not in command_result.stderr"
- "command_result.rc != 0"