I'd like to get an email sent whenever one of the hosts in ansible cannot be reached through SSH.I've tried to write a playbook to do this (see below). After gathering facts, any unreachable host is discarded, so my task sending the email is never executed for the unreachable hosts.-name: Check host accessibility  hosts: all  tasks:    - local_action: shell ssh deploy@{{ansible_hostname}} echo OK      register: check_ssh    - local_action: debug msg={{check_ssh.stdout}}      when: check_ssh.stdout != OKThanks a lot.
Ansible: send email on unreachable host
ansible
I've found that using a Python script is the easiest solution (easier than a callback plugin, at least on my ansible version 1.7):#!/usr/bin/env pythonfrom __future__ import print_functionimport ansible.inventoryimport ansible.runner                                                                                   from subprocess import Popen, PIPEimport sysTO = rootdef message(subject, body):    p = Popen([mail, -s, subject, TO], stdin=PIPE)                                                      p.communicate(input=body)                                                                               res = p.wait()                                                                                          if res != 0:        print(Failed to send message, file=sys.stderr)                                                def main():    im = ansible.inventory.Inventory()                                                                      runner = ansible.runner.Runner(                                                                             module_name='command',                                                                                  module_args='sh -c echo OK',                                                                          sudo=True,    )                                                                                                       run = runner.run()    nosudo = set(run[dark].keys())    runner = ansible.runner.Runner(        module_name='command',        module_args='sh -c echo OK',                                                                          sudo=False,        inventory=ansible.inventory.Inventory(list(nosudo)),                                                )       run = runner.run()                                                                                      nonet = set(run[dark].keys())     nosudo = nosudo - nonet    for host in nosudo:                                                                                         message(Host check: %s % host,                Cannot execute 'sudo -u root ...' as user 'deploy'.)                                      for host in nonet:        message(Host check: %s % host,                Cannot login into the machine.)if __name__ == '__main__':                                                                                  main()      