简体   繁体   中英

Question about Pagination in Ansible API playbook query

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM