Reputation: 27486
I have an ansible playbook that runs a shell command. If there is a specific message in the output, I need to terminate the playbook. Here's what I've tried:
- 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
The intention here is that if '"[Foo Bar Baz] Err Code abc123"' appears in the output of the program, I want to print the full output (which includes very useful information, but there is a lot of information so I don't want to print it all of the time) and then abort the playbook.
Unfortunately, this doesn't quite work.
The first debug statement prints something sort of like this:
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"
}
]
}
}
So, I know that the error message I'm interested in is in stdout
.
But the second one prints this:
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
I've also tried this using foo_result.stdout
, and also escaping the [
and ]
as \[
and \]
in the condition, and I've tried testing that foo.results.stdout is defined
, and I'm not sure why I'm getting this conditional evaluation error for all these permutations. I would have expected a syntax error or something... Is there a better way to do what I'm trying to do here (fail when there's specific text in a command's stdout and then print that stdout)?
(Ansible version is 1.6.10)
Upvotes: 1
Views: 1311
Reputation: 27486
It looks like I had some kind of syntax errors in the tasks yml file, but they weren't reported as "syntax error".
The problem seemed to be how I was "and"-ing conditions. I thought I could list them separately, but it seems that's not the case.
This task:
- debug: var=foo_result
when:
- foo_result.results is defined and "[Foo Bar Baz] Err Code abc123" in foo_result
Doesn't give me any errors. Ansible is not returning an error on this code, but the condition always evaluates to "true". however, I think that is a different problem, and I'll post a new question for that.
I've posted a follow-up question here: Ansible condition always evaluates to false
Upvotes: 0
Reputation: 13189
You only need to change the third task:
- debug: var=foo_result
failed_when: "'[Foo Bar Baz] Err Code abc123' in foo_result.stdout"
Works for me with ansible 1.8.4!
Upvotes: 2