From 603c4fd221230729deef477eb6d04cd2722563fc Mon Sep 17 00:00:00 2001 From: Grok Date: Sat, 9 Aug 2025 13:22:18 +0000 Subject: [PATCH] Initial commit of Ansible inventory playbooks --- esxi_inventory_playbook.yml | 32 ++++++++++ fetch_sdp_inventory.yml | 72 +++++++++++++++++++++++ get_esxi_inventory.yml | 32 ++++++++++ get_rhel_insights_inventory.yml | 38 ++++++++++++ get_rhel_insights_inventory_all_pages.yml | 64 ++++++++++++++++++++ get_rhv_inventory.yml | 37 ++++++++++++ host_discovery.yml | 41 +++++++++++++ proxmox_inventory.yml | 51 ++++++++++++++++ 8 files changed, 367 insertions(+) create mode 100644 esxi_inventory_playbook.yml create mode 100644 fetch_sdp_inventory.yml create mode 100644 get_esxi_inventory.yml create mode 100644 get_rhel_insights_inventory.yml create mode 100644 get_rhel_insights_inventory_all_pages.yml create mode 100644 get_rhv_inventory.yml create mode 100644 host_discovery.yml create mode 100644 proxmox_inventory.yml diff --git a/esxi_inventory_playbook.yml b/esxi_inventory_playbook.yml new file mode 100644 index 0000000..5d1d374 --- /dev/null +++ b/esxi_inventory_playbook.yml @@ -0,0 +1,32 @@ +--- +- name: Gather VM inventory from ESXi hypervisor + hosts: localhost + gather_facts: false + vars: + esxi_hostname: "your_esxi_host_ip" # Replace with your ESXi host IP or hostname + esxi_username: "root" # Replace with your ESXi username + esxi_password: "your_password" # Replace with your ESXi password (use ansible-vault for security) + validate_certs: false # Set to true if you have valid certs + + tasks: + - name: Gather information about all VMs on the ESXi host + community.vmware.vmware_vm_info: + hostname: "{{ esxi_hostname }}" + username: "{{ esxi_username }}" + password: "{{ esxi_password }}" + validate_certs: "{{ validate_certs }}" + register: vm_info + + - name: Display the VM inventory + debug: + var: vm_info + + - name: Save VM inventory to file + copy: + content: "{{ vm_info | to_nice_json }}" + dest: /root/esxi_vm_inventory.json + +# Notes: +# - Install the community.vmware collection: ansible-galaxy collection install community.vmware +# - Ensure pyvmomi is installed on the control machine: pip install pyvmomi +# - For production, secure passwords with ansible-vault or use environment variables. diff --git a/fetch_sdp_inventory.yml b/fetch_sdp_inventory.yml new file mode 100644 index 0000000..15a1b1c --- /dev/null +++ b/fetch_sdp_inventory.yml @@ -0,0 +1,72 @@ +--- +- 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 }}" diff --git a/get_esxi_inventory.yml b/get_esxi_inventory.yml new file mode 100644 index 0000000..5d1d374 --- /dev/null +++ b/get_esxi_inventory.yml @@ -0,0 +1,32 @@ +--- +- name: Gather VM inventory from ESXi hypervisor + hosts: localhost + gather_facts: false + vars: + esxi_hostname: "your_esxi_host_ip" # Replace with your ESXi host IP or hostname + esxi_username: "root" # Replace with your ESXi username + esxi_password: "your_password" # Replace with your ESXi password (use ansible-vault for security) + validate_certs: false # Set to true if you have valid certs + + tasks: + - name: Gather information about all VMs on the ESXi host + community.vmware.vmware_vm_info: + hostname: "{{ esxi_hostname }}" + username: "{{ esxi_username }}" + password: "{{ esxi_password }}" + validate_certs: "{{ validate_certs }}" + register: vm_info + + - name: Display the VM inventory + debug: + var: vm_info + + - name: Save VM inventory to file + copy: + content: "{{ vm_info | to_nice_json }}" + dest: /root/esxi_vm_inventory.json + +# Notes: +# - Install the community.vmware collection: ansible-galaxy collection install community.vmware +# - Ensure pyvmomi is installed on the control machine: pip install pyvmomi +# - For production, secure passwords with ansible-vault or use environment variables. diff --git a/get_rhel_insights_inventory.yml b/get_rhel_insights_inventory.yml new file mode 100644 index 0000000..84c32ac --- /dev/null +++ b/get_rhel_insights_inventory.yml @@ -0,0 +1,38 @@ +--- +- name: Gather 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) + # Optional parameters for filtering + per_page: 50 + page: 1 + + tasks: + - name: Query Red Hat Insights API for host inventory + uri: + url: "{{ insights_api_url }}?per_page={{ per_page }}&page={{ page }}" + method: GET + headers: + Authorization: "Bearer {{ insights_api_token }}" + Accept: "application/json" + return_content: true + validate_certs: true # Set to false if needed, but not recommended + register: api_response + + - name: Display the inventory response + debug: + var: api_response.json + + - name: Save inventory to file + copy: + content: "{{ api_response.json | to_nice_json }}" + dest: /root/rhel_insights_inventory.json + +# Notes: +# - Obtain an offline API token from https://console.redhat.com/openshift/token +# - Ensure your systems are registered with Red Hat Insights for them to appear in the inventory. +# - For production, secure the token with ansible-vault or environment variables. +# - Install any required modules if needed (uri is built-in). +# - This fetches a paginated list of hosts; adjust 'per_page' and 'page' or loop for full inventory. diff --git a/get_rhel_insights_inventory_all_pages.yml b/get_rhel_insights_inventory_all_pages.yml new file mode 100644 index 0000000..4fef0d5 --- /dev/null +++ b/get_rhel_insights_inventory_all_pages.yml @@ -0,0 +1,64 @@ +--- +- 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. diff --git a/get_rhv_inventory.yml b/get_rhv_inventory.yml new file mode 100644 index 0000000..822b9b1 --- /dev/null +++ b/get_rhv_inventory.yml @@ -0,0 +1,37 @@ +--- +- name: Gather VM inventory from Red Hat Virtualization (RHV) + hosts: localhost + gather_facts: false + vars: + rhv_url: "https://your_rhv_manager_fqdn/ovirt-engine/api" # Replace with your RHV Manager URL + rhv_username: "admin@internal" # Replace with your RHV username + rhv_password: "your_password" # Replace with your RHV password (use ansible-vault for security) + rhv_insecure: true # Set to false if you have valid certs + rhv_ca_file: "/path/to/ca.crt" # Optional: Path to CA certificate file + + tasks: + - name: Gather information about all VMs in RHV + ovirt.ovirt.ovirt_vm_info: + auth: + url: "{{ rhv_url }}" + username: "{{ rhv_username }}" + password: "{{ rhv_password }}" + insecure: "{{ rhv_insecure }}" + ca_file: "{{ rhv_ca_file | default(omit) }}" + pattern: "name=*" + register: vm_info + + - name: Display the VM inventory + debug: + var: vm_info + + - name: Save VM inventory to file + copy: + content: "{{ vm_info | to_nice_json }}" + dest: /root/rhv_vm_inventory.json + +# Notes: +# - Install the ovirt.ovirt collection: ansible-galaxy collection install ovirt.ovirt +# - Ensure required Python libraries are installed: pip install ovirt-engine-sdk-python +# - For production, secure passwords with ansible-vault or use environment variables. +# - This assumes you have access to the RHV Manager API. diff --git a/host_discovery.yml b/host_discovery.yml new file mode 100644 index 0000000..8b8f0f3 --- /dev/null +++ b/host_discovery.yml @@ -0,0 +1,41 @@ +--- +- name: Host Discovery with Nmap Probing Port 22 + hosts: localhost + connection: local + vars: + networks: + - 192.168.1.0/24 # Add your networks here + output_file: discovered_hosts.json + tasks: + - name: Install nmap if not present + become: true + ansible.builtin.apt: + name: nmap + state: present + update_cache: true + - name: Perform Nmap scan on each network + ansible.builtin.shell: + cmd: nmap -sT -p 22 -Pn --open -R -oG - {{ item }} + register: scans + loop: "{{ networks }}" + changed_when: false + - name: Combine all scan outputs + ansible.builtin.set_fact: + all_output: "{{ scans.results | map(attribute='stdout') | join('\n') }}" + - name: Extract host lines + ansible.builtin.set_fact: + host_lines: "{{ all_output.splitlines() | select('search', '^Host:') | list }}" + - name: Parse hosts into list + ansible.builtin.set_fact: + hosts_list: "{{ hosts_list | default([]) + [{'ip': ip, 'hostname': hostname}] }}" + loop: "{{ host_lines }}" + vars: + line: "{{ item }}" + host_part: "{{ line.split('\t')[0] }}" + parts: "{{ host_part.split(' ') }}" + ip: "{{ parts[1] }}" + hostname: "{{ parts[2] | replace('(', '') | replace(')', '') }}" + - name: Save to JSON file + ansible.builtin.copy: + content: "{{ {'hosts': hosts_list} | to_nice_json }}" + dest: "{{ output_file }}" diff --git a/proxmox_inventory.yml b/proxmox_inventory.yml new file mode 100644 index 0000000..b0765d9 --- /dev/null +++ b/proxmox_inventory.yml @@ -0,0 +1,51 @@ +--- +- name: Gather Full Machine Inventory from Proxmox Nodes + hosts: proxmox_nodes + become: true + gather_facts: true + tasks: + - name: Gather VM list + command: qm list + register: vm_list + + - name: Gather Container list + command: pct list + register: ct_list + + - name: Gather storage status + command: pvesm status + register: storage_status + + - name: Gather node status + command: pvesh get /nodes/$(hostname)/status + register: node_status + + - name: Gather hardware info + command: pveversion -v + register: hardware_info + + - name: Save inventory to file + copy: + content: | + Node: {{ inventory_hostname }} + Ansible Facts: {{ ansible_facts | to_nice_yaml }} + + VMs: + {{ vm_list.stdout }} + + Containers: + {{ ct_list.stdout }} + + Storage Status: + {{ storage_status.stdout }} + + Node Status: + {{ node_status.stdout | from_json | to_nice_yaml }} + + Hardware Info: + {{ hardware_info.stdout }} + dest: /tmp/proxmox_inventory_{{ inventory_hostname }}.txt + + - name: Debug + debug: + msg: "Inventory saved to /tmp/proxmox_inventory_{{ inventory_hostname }}.txt"