How to do proper error handling in ansible

ansibledeployment

From what I can tell, ansible only does error handling at the level of tasks, which is really not enough for what I want it to be able to do.

Specifically, my use case is a run-of-the-mill deployment, so what I need to do is:

  • stop the server
  • backup necessary resources
  • try to deploy (which involves a large number of tasks which must occur in order)
    • if that failed, rollback to the previous version (which, again, is a bunch of tasks with a strict ordering)
  • start the server

All that's fine and dandy, except the error-handling approach which I hoped would work…

- include: deploy.yml
  ignore_errors: yes
  register: deploy
- include: rollback.yml
  when: deploy | failed

…Doesn't.

Now, I can understand why it wouldn't- the include task would just test whether it could include the file, not whether the file it included could run to completion. I've made my peace with that.

Unfortunately, this leaves me in something of a pickle. The automatic error checking of every operation, combined with the library of high-level operations, is the entire reason I use ansible. But, in the absence of a mature recovery mechanism, being notified of failure ceases to be useful.

Anyone have (sane) workarounds? I could add a handler to every single task under that include and try to make it work that way, but… Really? That can't be the solution- or, rather, I can't continue to have faith in humanity and allow for that to be a solution.

Best Answer

http://www.ansible.com/blog/ansible-2.0-launch

Blocks introduce the concept of exception handling to playbooks, and were modeled after the try/except/finally structure of Python (and many other languages). This eases development of playbooks and tasks, where task failures can be caught and dealt with in a single playbook much more simply than before.