Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible: Perform Cleanup on Task Failure

I'm currently writing an Ansible play that follows this general format and is run via a cron job:

pre_tasks:   -Configuration / package installation  tasks:   -Work with installed packages  post_tasks:   -Cleanup / uninstall packages 

The problem with the above is that sometimes a command in the tasks section fails, and when it does the post_tasks section doesn't run, leaving the system in a messy state. Is it possible to force the commands in post_tasks to run even if a failure or fatal error occurs?

My current approach is to apply ignore_errors: yes to everything under the tasks section, and then apply a when: conditional to each task to individually check if the prior command succeeded.

This solution seems like a hack, but it gets worse because even with ignore_errors: yes set, if a Fatal error is encountered for a task the entire play will still immediately fail, so I have to also run a cron'd bash script to manually check on things after reach play execution.

All I want is a guarantee that even if tasks fails, post_tasks will still run. I'm sure there is a way to do this without resorting to bash script wrappers.

like image 324
Mark Avatar asked May 26 '14 17:05

Mark


People also ask

How do I skip failed tasks in Ansible?

Ignoring failed commands By default Ansible stops executing tasks on a host when a task fails on that host. You can use ignore_errors to continue on in spite of the failure. The ignore_errors directive only works when the task is able to run and returns a value of 'failed'.

What happens when one playbook fails a task?

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.

How can Error Handling be done in Ansible?

Ansible normally has defaults that make sure to check the return codes of commands and modules and it fails fast – forcing an error to be dealt with unless you decide otherwise. Sometimes a command that returns different than 0 isn't an error.

What is Changed_when in Ansible?

Ansible changed_when property or parameters is defined to deal with the output of the specific task once a task is triggered on the remote node and based on the return code or the output, we can determine whether the task should be reported in the ansible statistic or need to use the trigger to handle the condition and ...


2 Answers

This feature became available in Ansible 2.0:

This is the documentation for the new stanza markers block, rescue, and always.

like image 167
Marc Tamsky Avatar answered Sep 22 '22 00:09

Marc Tamsky


You should use handlers (http://docs.ansible.com/ansible/playbooks_intro.html) and set:

force_handlers: true

Please give a look to KubeNow's integration test (https://github.com/kubenow/KubeNow/blob/master/test/integration-test.yml).

like image 22
Marco Capuccini Avatar answered Sep 19 '22 00:09

Marco Capuccini