91 lines
3.0 KiB
YAML
91 lines
3.0 KiB
YAML
---
|
|
# Main tasks for linode_inventory role
|
|
|
|
- name: Set API token (AWX credential injection takes precedence)
|
|
ansible.builtin.set_fact:
|
|
linode_api_token: "{{ linode_api_token | default(lookup('env', 'LINODE_API_TOKEN')) | default('') }}"
|
|
|
|
- name: Validate required variables
|
|
ansible.builtin.assert:
|
|
that:
|
|
- linode_api_token is defined
|
|
- linode_api_token | length > 0
|
|
fail_msg: |
|
|
Linode API token not found.
|
|
For AWX: Attach a Linode API Token credential to your job template
|
|
For local: Set LINODE_API_TOKEN environment variable or pass linode_api_token variable
|
|
quiet: true
|
|
|
|
- name: Ensure output directory exists
|
|
ansible.builtin.file:
|
|
path: "{{ linode_inventory_output_dir }}"
|
|
state: directory
|
|
mode: '0755'
|
|
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)
|