Search code examples
error-handlingansibleansible-awx

How to know in which task an ansible play failed when launched using AWX API?


I intend to launch ansible jobs on AWX using AWX api and get a call back from the ansible playbook to be informed about the result of the play.

To do so I'm using the /api/v2/job_templates/<job-template-id>/launch/ with some extra_vars in the body to pass parameters to my play.

{
    "extra_vars": {
        "target": "w.x.y.z",  (put here a real IP)
        "directory_name_1": "dir1",
        "directory_name_2": "dir2",
        "file_name": "file1"  (or "subdir/file1" to make it fails)
    }
}

I've also configured a webhook notification in the job-template with the default customization: {{ job_metadata }}

I've put here the play I'm using which is super simple, it creates 2 directories and one file in the first directory.

- hosts: "{{ target }}"
  name: fbplay
  tasks:
    - name: Create dummy directory 1
      file:
        path: "{{directory_name_1}}"
        state: directory
    - name: Create dummy directory 2
      file:
        path: "{{directory_name_2}}"
        state: directory
    - name: Create dummy file in directory
      file:
        path: "{{directory_name_1}}/{{file_name}}"
        state: touch
        mode: u=rw,g=r,o=r

All of this work great and in case of error 4 tasks will be executed on the target machine:

TASK [Gathering Facts] *********************************************************
TASK [Create dummy directory 1] ************************************************
TASK [Create dummy directory 2] ************************************************
TASK [Create dummy file in directory] ******************************************

...but here is my question with regard to error handling: How can I indicate in the call back which task failed in case of error ?

In fact I can know if the play failed or not getting, in case of success:

  "hosts": {
    "w.x.y.z": {
      "failed": false,
      "changed": 1,
      "dark": 0,
      "failures": 0,
      "ok": 4,
      "processed": 1,
      "skipped": 0,
      "rescued": 0,
      "ignored": 0
    }
  }

and in case of failure:

  "hosts": {
    "w.x.y.z": {
      "failed": true,
      "changed": 0,
      "dark": 0,
      "failures": 1,
      "ok": 3,
      "processed": 1,
      "skipped": 0,
      "rescued": 0,
      "ignored": 0
    }
  }

But I cannot get the exact task that failed (in this case the last one by passing a filename containing a sub-directory that does not exist for example).

I'm a newbie on AWX & ansible and I'm fighting with what I thought would be a relatively simple point... so any hints or ideas is welcome.

Thx beforehand.


Solution

  • in case it helps someone, I actually confirm that ARA does what I was looking for above which is to display if using its web client (or provide through an API) exactly which task failed in each playbook you ran.