Question:
I am learning Ansible. I have a playbook to clean up resources, and I want the playbook to ignore every error and keep going on till the end , and then fail at the end if there were errors.
I can ignore errors with
1 2 |
ignore_errors: yes |
If it was one task, I could do something like ( from ansible error catching)
1 2 3 4 5 6 7 8 9 |
- name: this command prints FAILED when it fails command: /usr/bin/example-command -x -y -z register: command_result ignore_errors: True - name: fail the play if the previous command did not succeed fail: msg="the command failed" when: "'FAILED' in command_result.stderr" |
How do I fail at the end ? I have several tasks, what would my “When” condition be?
Answer:
Use Fail module.
- Use ignore_errors with every task that you need to ignore in case of errors.
- Set a flag (say, result = false) whenever there is a failure in any task execution
- At the end of the playbook, check if flag is set, and depending on that, fail the execution
123 - fail: msg="The execution has failed because of errors."when: flag == "failed"
Update:
Use register to store the result of a task like you have shown in your example. Then, use a task like this:
1 2 3 4 |
- name: Set flag set_fact: flag = failed when: "'FAILED' in command_result.stderr" |