Reputation: 170390
When running ansible playbooks you do often encounter use-cases where a task (usually shell or command) is expected to return error codes.
So far the solution was to register the result and to add ignore_errors: true
and to decide later if it was a real error or not.
Now, there is one issue with this: it will mess the logging as you will see red error like
fatal: ...
...ignoring
Is there a way to avoid these so we would not have fake errors in the logs?
Upvotes: 6
Views: 8803
Reputation: 68239
failed_when
and changed_when
is to help you:
- shell: echo good | grep bad
register: res
failed_when: false
changed_when: false
This will always be good and green despite failed shell command.
You can also define a complex failed_when
statement based on registered variable.
Upvotes: 14
Reputation: 13235
The best way is https://stackoverflow.com/a/40430875/1849837 (use changed_when
and failed_when
). Note that it works from Ansible since 1.4.
For older Ansible version the only way I see (and use) is do not run potentially failing task at all. You may use conditions (when
) for that.
First you need to check the condition and assign the result of check to the variable
- name: "check if condition is met"
command: some condition that populates result
register: result
- name: "some operation"
command: operation that runs only if result is not failed.
when: result|failed
This includes some boilerplate code (you need artificial step to test the condition) but I see no better alternatives. I believe that this is better than polluting the logs with fatal: ... ...ignoring
.
Upvotes: -1