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
ignore_errors: yes
If it was one task, I could do something like ( from ansible error catching)
- 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?
If you use ignore_errors, ansible will continue attempting to run tasks against that host. The default workflow is to fail, then ignore that host for the rest of the playbook. Then at the end, admin researches why the hosts failed, fixes them, then reruns the playbook against the ones that failed. The .
Boolean that allows you to ignore task failures and continue with play. It does not affect connection errors. Boolean that allows you to ignore task failures due to an unreachable host and continue with the play.
You can achieve similar effect by just using the --step flag e.g: ansible-playbook playbook. yml --step . The step asks you on before executing each task and you could choose (N)o/(y)es/(c)ontinue . With this approach you selectively execute tasks when needed and also continue from point where it failed, after fixes.
These directives are written at the same level as the hosts: directive. Here are subset of the keys that can be used is described: any_errors_fatal : This Boolean directive is used to instruct Ansible to treat any failure as a fatal error to prevent any further tasks from being attempted.
Use Fail module.
- 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:
- name: Set flag set_fact: flag = failed when: "'FAILED' in command_result.stderr"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With