【发布时间】:2015-03-04 22:56:25
【问题描述】:
我有一个运行 shell 命令的 ansible playbook。如果输出中有特定消息,我需要终止 playbook。这是我尝试过的:
- name : Do foo
shell: /bin/my_application arg1 arg2 arg3
args:
creates: /tmp/foo_1
with_items: data_items
register: foo_result
- debug: var=foo_result
- debug: var=foo_result
when:
- foo_result.results is defined
- '"[Foo Bar Baz] Err Code abc123" in foo_result.results.stdout'
failed_when: true
这里的意图是 if '"[Foo Bar Baz] Err Code abc123"' 出现在程序的输出中,我想打印 full 输出(其中包含非常有用的信息,但有 很多 信息,所以我不想一直打印它所有)然后中止剧本。 p>
不幸的是,这不太行。
第一个调试语句打印出类似这样的内容:
TASK: [do_stuff | debug var=foo_result] ****************************
ok: [some-node] => {
"foo_result": {
"changed": true,
"msg": "All items completed",
"results": [
{
"changed": true,
[Snip..]
"stderr": "",
"stdout": "Very large stack trace containing [Foo Bar Baz] Err Code abc123"
}
]
}
}
所以,我知道我感兴趣的错误消息是在stdout。
但是第二个打印了这个:
TASK: [do_stuff | debug var=foo_result] ****************************
fatal: [some-node] => error while evaluating conditional: "[Foo Bar Baz] Err Code abc123" in foo_result.results.stdout
我也尝试过使用foo_result.stdout,并将[ 和] 转义为\[ 和\] 在条件下,我已经尝试测试foo.results.stdout is defined,我'不知道为什么我会得到所有这些排列的条件评估错误。我本来预计会出现语法错误或其他东西......有没有更好的方法来做我在这里尝试做的事情(当命令的标准输出中有特定文本然后打印该标准输出时失败)?
(Ansible 版本为 1.6.10)
【问题讨论】: