Ansible 条件始终评估为 false [英] Ansible condition always evaluates to false
问题描述
我正在尝试检查特定字符串的 shell 命令的输出,该字符串指示错误,并且应该终止剧本.
I'm trying to examine the output of a shell command for a particular string which indicates an error, and that the playbook should be terminated.
我正在尝试像这样调试它:
I'm trying to debug it something like this:
- debug: var=foo_result
- debug: msg={{ 'Some error text' in foo_result }}
在这个例子中,install_result
被注册为包含命令的输出,它确实:
In this example, install_result
was registered to contain the output of the command, and it does:
TASK: [do_stuff | debug var=foo_result] ****************************
ok: [some-node] => {
"foo_result": {
"changed": true,
"msg": "All items completed",
"results": [
{
"changed": true,
[Snip..]
"stderr": "",
"stdout": "...Some error text..."
}
]
}
}
在 foo_result
中检查一些错误文本"的第二个调试语句总是评估为假".
The second debug statement, which checked for "some error text" in foo_result
always evaluates to "false".
我仍然发现 Ansible 语法有点混乱,我不确定我在这里做错了什么.
I'm still finding the Ansible syntax a little confusing, I'm not sure what I did wrong here.
Ansible 版本:1.6.10
推荐答案
Ansible 很奇怪.也许我只是没有明白"它......
Ansible is weird. Maybe I'm just not "getting" it...
请注意,在原始输出中,foo_install.results
包含一个具有单个元素的数组.如果你想调试或测试 stdout
的文本,你可以这样做:
Notice in the original output, that foo_install.results
contains an array with a single element. If you want to debug or test stdout
for text, you can do it this way:
- debug: msg={{ 'My Error' in foo_result.results[0].stdout }}
when: foo_result.changed
注意我必须添加数组索引符号才能正确打印true".
Notice I had to add array index notation to get this to print "true" properly.
但是如果我这样做:
- name: Do Foo Stuff
shell: /some/path/to/some/command
register: foo_result
failed_when: "'My Error' in foo_result.stdout"
我不需要参考results[0]
.我不知道这个区别是怎么回事.我不知道为什么访问 foo_result
似乎工作不同,如果我从注册它的同一个任务与不同的任务访问它.
I don't need to refer to results[0]
. I don't know what's up with that difference. I don't know why accessing foo_result
seems to work differently if I access it from the same task that registered it vs. a different task.
也许这在最近的版本中得到了修复,但这是我在 1.6.10 中需要做的事情
Maybe this is fixed in more recent versions, but this is what I needed to do to get this working in 1.6.10
这篇关于Ansible 条件始终评估为 false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!