0

I need to query the REST API Jobs endpoint in Ansible Tower in my playbook.

I am using the following to retrieve the data

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

- name: Get last half hour of data
  debug:
    var: "{{item}}"
  when:
  - item['started'] > current_epoch_half_hour_back
  loop: "{{ jobs_data['json']['results'] }}"

I am only interested in looking at the last half hour of jobs data.

We keep around 2 weeks, which is around 15,000 pages of jobs data and when I use the above page=1 and page_size=200 it seems to starts on page 1 which is the oldest data and only seems to look at the first 200 pages thus missing the latest data entirely. RedHat says you can change the limit by modifying the following file /etc/tower/conf.d/<some file>.py but recommends that you not exceed 200.

Does anyone know how to get the query to start at the last page (newest data) instead of page 1 (oldest data)?

Don't want to hard code a page= values since I wouldn't know that exact value of the last page.

************************* UPDATE ON THE ISSUE *****************************

I added the negative order by clause for a reverse sort and it works. Thanks for the tip.

url: https://aap.prci.com/api/v2/jobs/?format=json&page=1&page_size=600&order_by=-{{'started'}}

However I need the last half hour of data only. For that I do the following

- set_fact:
    half_hour_back: "{{ '%Y-%m-%dT%H:%M:%S'|strftime((ansible_date_time.epoch|int) - (60*60*0.5)) }}"


- name: Add jobs data to dictionary
  set_fact:
    list_of_jobs_info: "{{ list_of_jobs_info + [{ 'job_id' : item['id'], 'job_name' : item['name'], 'job_status' : item['status'] }] }}"
  when:
    - item['status'] == "canceled"
    - item['started'] > half_hour_back
  loop: "{{ jobs_data['json']['results'] }}"

The " > half_hour_back " condition works in the Tower but not in AAP. Does anyone know if there has been a change in the API between the Tower and AAP. When I looked at the datatype of " started " it seems to be in the following format

"started": "2022-11-15T20:00:07.881083Z",

I suspect AAP API is more strict with datatype checking. Does anyone know how to get the correct format in the half_hour_back variable so that I can use it for comparison in the when condition?

abrahavt
  • 45
  • 6
  • 1
    Just a note to make the code better maintainable. The misleading variable name in `Authorization: "Bearer {{ lookup('env', 'TOWER_PASSWORD') }}"` should be better named as `TOWER_TOKEN` since that is what it is, a [Bearer Token](https://stackoverflow.com/questions/25838183/) and not a password. – U880D Jan 13 '23 at 22:07

1 Answers1

0

According the documentation about Ansible Tower REST API - Jobs you can just perform a Sorting via ?order_by= and which could be the id, started or other fields documented there. By doing this you could decrease the result set to a size where is no need for pagination anymore.

U880D
  • 8,601
  • 6
  • 24
  • 40