title: Execute task (or handler) if any task failed
body: <p>I am using Ansible to deploy a Django website into my servers (production, staging, etc), and I would like to get a notification (via slack in this case) if and only if any task fails.</p>
  
  <p>I can only figure out how to do it if a specified task fails (so I guess I could add a handler to all tasks), but intuition tells me there has to be an easier and more elegant option.</p>
  
  <p>Basically what I am thinking of is:</p>
  
  <pre><code>---
  - hosts: "{{hosts_to_deploy}}"
  
  - tasks: 
  
     [...]
  
    - name: notify slack of deploy failure
      local_action:
        module: slack
        token: "{{slack_token}}"
        msg: "Deploy failed on {{inventory_hostname}}"
      when: # any task failed
  </code></pre>
  
  <p>I have been diving into the Ansible documentation, specially in the <a href="http://docs.ansible.com/ansible/playbooks_error_handling.html" rel="noreferrer">error handling</a> section, and answers here at SO, but I'm struggling to find an answer to my question. So any help will be much appreciated.</p>