Files
ansible-inventories/get_rhel_insights_inventory_all_pages.yml

65 lines
2.4 KiB
YAML

---
- name: Gather all pages of system inventory from Red Hat Insights API
hosts: localhost
gather_facts: false
vars:
insights_api_url: "https://console.redhat.com/api/inventory/v1/hosts" # Red Hat Insights Inventory API endpoint
insights_api_token: "your_access_token" # Replace with your Red Hat API access token (offline token from console.redhat.com)
per_page: 50 # Number of items per page (adjust as needed)
all_hosts: [] # Accumulator for all host data
tasks:
- name: Fetch the first page to get total count
uri:
url: "{{ insights_api_url }}?per_page={{ per_page }}&page=1"
method: GET
headers:
Authorization: "Bearer {{ insights_api_token }}"
Accept: "application/json"
return_content: true
validate_certs: true
register: first_page_response
- name: Accumulate first page results
set_fact:
all_hosts: "{{ all_hosts + first_page_response.json.results }}"
- name: Calculate total pages
set_fact:
total_pages: "{{ ((first_page_response.json.total | float / per_page) | round(0, 'ceil')) | int }}"
- name: Fetch remaining pages
uri:
url: "{{ insights_api_url }}?per_page={{ per_page }}&page={{ item }}"
method: GET
headers:
Authorization: "Bearer {{ insights_api_token }}"
Accept: "application/json"
return_content: true
validate_certs: true
register: page_response
loop: "{{ range(2, total_pages + 1) | list }}"
when: total_pages > 1
- name: Accumulate all remaining page results
set_fact:
all_hosts: "{{ all_hosts + item.json.results }}"
loop: "{{ page_response.results }}"
when: total_pages > 1
- name: Display the full inventory
debug:
var: all_hosts
- name: Save full inventory to file
copy:
content: "{{ {'hosts': all_hosts} | to_nice_json }}" # Wrap in a dict for better structure
dest: /root/rhel_insights_inventory_all.json
# Notes:
# - Obtain an offline API token from https://console.redhat.com/openshift/token
# - Ensure your systems are registered with Red Hat Insights.
# - For production, secure the token with ansible-vault.
# - This handles pagination by calculating total pages from the first response and fetching accordingly.
# - Assumes the API response includes 'total' and 'results' fields.