Files
ansible-inventories/fetch_sdp_inventory.yml

73 lines
2.4 KiB
YAML

---
- name: Fetch Machine Inventory from ServiceDesk Plus
hosts: localhost
connection: local
vars:
sdp_url: "https://your-servicedesk-instance.com" # Replace with your ServiceDesk Plus URL
technician_key: "YOUR_TECHNICIAN_KEY" # Replace with your API key
output_file: "sdp_inventory.json" # File to save the inventory
row_count: 100 # Number of records per page
tasks:
- name: Fetch initial page and total count
ansible.builtin.uri:
url: "{{ sdp_url }}/api/v3/assets?TECHNICIAN_KEY={{ technician_key }}"
method: POST
body_format: json
body: '{{ input_data | to_json }}'
return_content: yes
status_code: 200
vars:
input_data:
list_info:
row_count: "{{ row_count }}"
start_index: 1
sort_field: "name"
sort_order: "asc"
get_total_count: true
register: initial_response
- name: Set facts for total count and initial assets
ansible.builtin.set_fact:
total_count: "{{ initial_response.json.list_info.total_count | int }}"
all_assets: "{{ initial_response.json.assets | default([]) }}"
- name: Calculate number of pages
ansible.builtin.set_fact:
pages: "{{ ((total_count + row_count - 1) / row_count) | int }}"
- name: Fetch remaining pages
ansible.builtin.uri:
url: "{{ sdp_url }}/api/v3/assets?TECHNICIAN_KEY={{ technician_key }}"
method: POST
body_format: json
body: '{{ input_data | to_json }}'
return_content: yes
status_code: 200
vars:
input_data:
list_info:
row_count: "{{ row_count }}"
start_index: "{{ (item - 1) * row_count + 1 }}"
sort_field: "name"
sort_order: "asc"
get_total_count: true
loop: "{{ range(2, pages | int + 1) | list }}"
register: page_responses
when: pages > 1
- name: Append remaining assets to all_assets
ansible.builtin.set_fact:
all_assets: "{{ all_assets + item.json.assets | default([]) }}"
loop: "{{ page_responses.results }}"
when: pages > 1
- name: Save full inventory to file
ansible.builtin.copy:
content: "{{ {'assets': all_assets} | to_json }}"
dest: "{{ output_file }}"
- name: Display result
ansible.builtin.debug:
msg: "Full machine inventory fetched and saved to {{ output_file }}"