title: Ansible: Perform Cleanup on Task Failure
body: <p>I'm currently writing an Ansible play that follows this general format and is run via a cron job: </p>
  
  <pre><code>pre_tasks:
    -Configuration / package installation
  
  tasks:
    -Work with installed packages
  
  post_tasks:
    -Cleanup / uninstall packages
  </code></pre>
  
  <p>The problem with the above is that sometimes a command in the <code>tasks</code> section fails, and when it does the <code>post_tasks</code> section doesn't run, leaving the system in a messy state. Is it possible to force the commands in <code>post_tasks</code> to run even if a failure <strong>or fatal error</strong> occurs?</p>
  
  <p>My current approach is to apply <code>ignore_errors: yes</code> to everything under the <code>tasks</code> section, and then apply a <code>when:</code> conditional to each task to individually check if the prior command succeeded. </p>
  
  <p>This solution seems like a hack, but it gets worse because even with <code>ignore_errors: yes</code> 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. </p>
  
  <p>All I want is a guarantee that even if <code>tasks</code> fails, <code>post_tasks</code> will still run. I'm sure there is a way to do this without resorting to bash script wrappers.</p>