efficient way to get email notification about failed task in a playbook or role

Dear All,

is there any way to get failed task name captured in rescue section which can be email notified or submitted to slack system ?

looking for something which can be placed on “any_variable_for_failed_task” in below code.

  • name: Check host accessibility
    hosts: localhost
    user: deploy
    gather_facts: no
    tasks:
  • block:
  • command: echo OK 1
  • command: echo OK 2
  • command: echo OK 3
  • command: echo OK 4
  • command: echo OK 5
  • command: echo OK 6
  • command: ech OK 7
  • command: echo OK 8
  • command: echo OK 9
  • command: echo OK 10
    rescue:
  • name: send email when something goes wrong
    mail:
    host: localhost

you should have 'ansible_failed_task' and 'ansible_failed_result'
available for tasks in the rescue portion of a block

Thanks!

This is a really useful feature I didn’t know about.

After some digging I found the issue from a year ago: https://github.com/ansible/ansible/issues/27970

Looks like a time to finally document it.

Rastislav