Reattempting Linode dynamic inventories.

This commit is contained in:
Kevin Thompson
2025-08-07 12:47:06 -05:00
parent 9e07592c4d
commit 1b3bdeb740
9 changed files with 430 additions and 55 deletions

View File

@@ -1,11 +0,0 @@
---
- name: Get Linode inventory using local module
linode_inventory:
register: linode_result
- name: Write inventory to file
copy:
dest: /tmp/linode_inventory.json
content: "{{ linode_result | to_nice_json }}"
mode: '0644'

View File

@@ -0,0 +1,97 @@
---
# Main tasks for linode_inventory role
- name: Validate required variables
ansible.builtin.assert:
that:
- linode_api_token is defined
- linode_api_token | length > 0
fail_msg: "linode_api_token must be defined and not empty"
quiet: true
- name: Ensure output directory exists
ansible.builtin.file:
path: "{{ linode_inventory_output_dir }}"
state: directory
mode: '0755'
delegate_to: localhost
- name: Check if Python requests module is available
ansible.builtin.command: python3 -c "import requests"
register: python_requests_check
failed_when: false
changed_when: false
delegate_to: localhost
- name: Install Python requests if not available
ansible.builtin.pip:
name: requests
state: present
when: python_requests_check.rc != 0
delegate_to: localhost
- name: Copy Linode inventory script
ansible.builtin.copy:
src: linode_inventory.py
dest: "{{ linode_inventory_output_dir }}/linode_inventory.py"
mode: '0755'
delegate_to: localhost
- name: Execute Linode inventory script
ansible.builtin.command:
cmd: python3 {{ linode_inventory_output_dir }}/linode_inventory.py --list
environment:
LINODE_API_TOKEN: "{{ linode_api_token }}"
register: linode_inventory_result
delegate_to: localhost
changed_when: true
- name: Parse inventory JSON
ansible.builtin.set_fact:
linode_inventory_data: "{{ linode_inventory_result.stdout | from_json }}"
- name: Save inventory to file
ansible.builtin.copy:
content: "{{ linode_inventory_data | to_nice_json }}"
dest: "{{ temp_inventory_path }}"
mode: '0644'
delegate_to: localhost
- name: Display inventory summary
ansible.builtin.debug:
msg: |
Linode Dynamic Inventory Summary:
Total hosts discovered: {{ linode_inventory_data._meta.hostvars | length }}
Groups created: {{ linode_inventory_data.keys() | reject('equalto', '_meta') | list | length }}
Inventory saved to: {{ temp_inventory_path }}
- name: Show discovered hosts
ansible.builtin.debug:
msg: "Host: {{ item.key }} ({{ item.value.ansible_host }}) - Region: {{ item.value.linode_region }} - Status: {{ item.value.linode_status }}"
loop: "{{ linode_inventory_data._meta.hostvars | dict2items }}"
loop_control:
label: "{{ item.key }}"
- name: Create static inventory file (optional)
ansible.builtin.template:
src: inventory.ini.j2
dest: "{{ linode_inventory_output_dir }}/linode_static_inventory.ini"
mode: '0644'
when: inventory_format == "ini"
delegate_to: localhost
# AWX/Tower specific tasks
- name: Create inventory update script for AWX
ansible.builtin.template:
src: awx_inventory_update.sh.j2
dest: "{{ linode_inventory_output_dir }}/awx_inventory_update.sh"
mode: '0755'
delegate_to: localhost
when: awx_integration | default(false)
- name: Clean up temporary script
ansible.builtin.file:
path: "{{ linode_inventory_output_dir }}/linode_inventory.py"
state: absent
delegate_to: localhost
when: cleanup_temp_files | default(true)