How to do proper error handling in ansible?
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.
Related videos on Youtube
Parthian Shot
What I'm not Unless I explicitly state otherwise below, you must assume I have no expertise / formal training in any topic. This includes, but is not limited to, assuming I have any legal, medical, or structural engineering, electrician, or plumber training whatsoever. I have no such training, nor do I hold any certificates or accolades in the aforementioned. If you take my advice and it ends badly, you do so at your own risk and I can provide no warranty or guarantee as to my advice's efficacy, merchantability, or fitness for a particular purpose. My advice is provided "as is". If you take my advice and it ends well, I am "happy for you", unless you are using the advice I have given to "be a dick", in which case I am at best "ambivalent". I do not understand why licenses feel the need to put "as is" in quotes, but I like punctuation (especially parentheticals and ellipses...), so I've decided to "adopt" that "arbitrary style" for the "duration" o"f" "this paragraph". What I am I'm a computer engineer and computer scientist (with degrees 'n' everfin'). I wouldn't call myself an "expert" in any topic, but I like computers and am reasonably good at getting thinking machines to do what I want them to do. Currently working as an SRE at $silicon_valley_company writing Ansible and Salt deployments targeting cloud infrastructure. Personal opinion? After writing a lot of Ansible, I've reached the conclusion that no one should use Ansible. I've yet to come to a formal opinion about Salt. Languages My favorite languages are bash, C, C++, Python, Verilog, Perl, and Haskell, but of those I'm only fluent in the first 6. I'm also fluent in Matlab (well... GNU Octave, really), Java and VHDL. And I like Scala better than vanilla Java- except when it comes to the collections framework- because mixins. LaTeX is also fun, and I can use it wonderfully, but ``fluent'' implies I know a lot about the language constructs, which I don't, so I'm not. Platforms If it's an operating system whose name is a registered Bell of AT&T trademark Corporation, or based on the aforementioned, I like it. Also like FreeBSD a lot (ran it on my main machine without a GUI for the duration of 2014). But I also quite like OSX. I know how to use Windows effectively, but that O/S is just too GUI-oriented to make me happy. And, no, Powershell doesn't solve the problem. If I wanted to write DCL, I'd be running VMS. CentOS and Debian also float my dirigible quite nicely.
Updated on September 18, 2022Comments
-
Parthian Shot over 1 year
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.-
Parthian Shot over 8 yearsI suppose I could just make two calls to
ansible-playbook
within a bash script, but I thought I should check whether there was a pure ansible way of doing things, first. -
eco almost 7 yearsHere's what you really want for recent versions of ansible: docs.ansible.com/ansible/playbooks_blocks.html
-
Parthian Shot over 8 yearsGood to know... Yet another reason to switch to version 2. <grumble> Well, that, and the fact that they don't version the documentation. </grumble>