0

I am trying to detect all cancelled jobs in Ansible AWX / Tower by querying the API on a periodic basis. Trying to figure out how to do it. Here is what I have.

- name: Pull Jobs Data
    uri:
      url: https://ansible2.xrxi.com/api/v2/jobs/?format=json&page=1&page_size=1000
      headers:
        Content-Type: application/json
        Authorization: "Bearer {{ lookup('env', 'TOWER_PASSWORD') }}"
    register: jobs_data

- name: Add jobs data to dictionary - Cancelled jobs
    set_fact:
      list_of_jobs_info: "{{ list_of_jobs_info + [{ 'job_id' : item['id'], 'job_name' : item['name'], 'job_status' : item['status'],  'job_cancel' : item['canceled_on'], 'job_template_id' : item['summary_fields']['job_template']['id'], 'job_template_name' : item['summary_fields']['job_template']['name'] }] }}"
    when:
    - item['canceled_on'] is defined
    - item['summary_fields']['job_template'] is defined
    - item['job_status'] == "Failed"
    loop: "{{ jobs_data['json']['results'] }}"

    register : subset_data

  - debug:
      var: subset_data

Currently it seems to be failing on item['job_status'] == "Failed".

U880D
  • 8,601
  • 6
  • 24
  • 40
abrahavt
  • 45
  • 6
  • Just a note to make the code better maintainable. The variable name in `Authorization: "Bearer {{ lookup('env', 'TOWER_PASSWORD') }}"` should be `TOWER_TOKEN` since that is what it is, a [Bearer Token](https://stackoverflow.com/questions/25838183/). – U880D Dec 28 '22 at 06:19
  • if this answer good for you please accept it – Ben.S Dec 28 '22 at 16:23
  • Changing to 'TOWER_TOKEN' did not work. Not sure why. – abrahavt Jan 13 '23 at 17:56

1 Answers1

1

Try to switch

  • from - item['job_status'] == "Failed"
  • to - item['status'] == "Failed"

since it's look like the item['job_status'] is not defined.

U880D
  • 8,601
  • 6
  • 24
  • 40
Ben.S
  • 708
  • 1
  • 5
  • 24
  • 1
    Right, that's the case according [Tower API Reference Guide - Jobs - List Jobs](https://docs.ansible.com/ansible-tower/latest/html/towerapi/api_ref.html#/Jobs/Jobs_jobs_list) "_**GET** `/api/v2/jobs/` List Jobs_" and where results and job data structure fields are documented. – U880D Dec 28 '22 at 06:25