ansible / awx

AWX provides a web-based user interface, REST API, and task engine built on top of Ansible. It is one of the upstream projects for Red Hat Ansible Automation Platform.
Other
13.7k stars 3.38k forks source link

Ability to Generate a Failed Host Summary Using Notification Attributes #6418

Open imareporter opened 4 years ago

imareporter commented 4 years ago
ISSUE TYPE
SUMMARY

An user is requesting the ability to generate a failed host summary using notification attributes (https://docs.ansible.com/ansible-tower/latest/html/installandreference/notification_parameters_supported.html). The failed host summary would be sent via email.

Ideally, they want a structure similar to this:

Failed Hosts:
<HOSTNAME> failed <TASK_NAME> in <PLAYBOOK_NAME>

There is a job_metadata attribute, which includes all target hosts and their status.

EXAMPLE:

{
  "test1.example.com": {
    "failed": true,
    "changed": 0,
    "dark": 1,
    "failures": 0,
    "ok": 0,
    "processed": 1,
    "skipped": 0,
    "rescued": 0,
    "ignored": 0
  },
  "test2.example.com": {
    "failed": true,
    "changed": 0,
    "dark": 1,
    "failures": 0,
    "ok": 0,
    "processed": 1,
    "skipped": 0,
    "rescued": 0,
    "ignored": 0
  }
}

However, it appears you can't query deeper into the JSON body with filters and grab the necessary information. At this point, all metadata about the job is displayed.

Is it possible to implement a failed host summary with the preceding structure? If not, is it possible to generate a new notification attribute with just failed host information?

Maybe something like this:

EXAMPLE BODY:

Failed Hosts:

{{ job_failed_hosts }}

JSON BODY:

{
  "failed_hosts": {
    "test1.example.com": {
      "failed_task_name": "Gathering Info",
      "playbook": "test.yml"
    },
    "test2.example.com": {
      "failed_task_name": "Gathering Data",
      "playbook": "test.yml"
    }
  }
}

EXAMPLE RENDERED EMAIL:

Failed Hosts:

{
  "failed_hosts": {
    "test1.example.com": {
      "failed_task_name": "Gathering Info",
      "playbook": "test.yml"
    },
    "test2.example.com": {
      "failed_task_name": "Gathering Data",
      "playbook": "test.yml"
    }
  }
}

Any guidance is much appreciated!

wenottingham commented 4 years ago

As of now, you can only use data that is on the job record. We'd have to see how interpolating into that data could be done.

arsenicks commented 2 years ago

Interested to see if we can help moving this forward ? We have the same requirement, we are using webhook but as far as I know it's the same data/vars we can get..

We tested a way filtering the content of job_metadata in a playbook, it worked but we can't use the same filter(from_json) because it's not a "native" jinja filter so it's not available to use in the notification template.. I'll put it here in case someone have a suggestion or another way of filtering the data.

- name: debug debug: msg: | List failed host: {% set job_details = job_metadata | from_json -%} {% for host in job_details.hosts -%} {% if job_details.hosts[host].failed -%} {{ host }} {% endif -%} {% endfor -%}