Ansible Tower – Pass ‘ansible_failed_result’ Variable to Notification

ansibleansible-tower

I'm trying to find a way to use the native Ansible and Tower failure variables and notifications respectively to send an email with the result of a job's execution output.

So I have a playbook that does something like this:

  rescue:
    - debug:
        msg: "Failure:\n\n{{ansible_failed_result | to_nice_json}}"

Which gives me a nice output in Tower

However I'd also like to be able to pass that same output from Tower to an email notification so that I can see in an email the output of a job without having to log into Tower. This means I know from my email right away if it's a connection timeout or something more serious.

I've tried using the customize notifications in Tower and have tried the 'job.job_explanation' field, however that always returns empty.

I tried running the job with increased verbosity, but that didn't work either.

Any ideas of how to do this?
At this point I'm not sure I'm even on the right track using the job_explanation field so any help would be much appreciated!

Thanks

Best Answer

In case anyone else stumbles across the same issue, here's how I did it very simply

  tasks:
    - block:
        - name: Fail Task
          fail:
            msg: 'Failed'
          when: true

      rescue:
      - name: set stats
        set_stats:
           data:
             msg_body_1: "This was a test"

You can then use {{ job.artifacts }} in the Tower notifications message section to return the variable you set.

Related Topic