Add debugging to figure out why this isn't working
This commit is contained in:
@@ -6,7 +6,6 @@
|
|||||||
|
|
||||||
vars:
|
vars:
|
||||||
# Override these variables as needed
|
# Override these variables as needed
|
||||||
linode_api_token: "{{ lookup('env', 'LINODE_API_TOKEN') }}"
|
|
||||||
linode_inventory_output_dir: "/tmp/linode_inventory"
|
linode_inventory_output_dir: "/tmp/linode_inventory"
|
||||||
inventory_format: "json" # or "ini"
|
inventory_format: "json" # or "ini"
|
||||||
awx_integration: true
|
awx_integration: true
|
||||||
@@ -18,12 +17,10 @@
|
|||||||
specific_tags: [] # e.g., ['production', 'web']
|
specific_tags: [] # e.g., ['production', 'web']
|
||||||
|
|
||||||
pre_tasks:
|
pre_tasks:
|
||||||
- name: Check for Linode API token
|
- name: Check for Linode API token (will be injected by AWX credential)
|
||||||
ansible.builtin.fail:
|
ansible.builtin.fail:
|
||||||
msg: "LINODE_API_TOKEN environment variable or linode_api_token variable must be set"
|
msg: "Linode API Token credential must be attached to this job template in AWX"
|
||||||
when:
|
when: linode_api_token is undefined or linode_api_token == ""
|
||||||
- linode_api_token is undefined or linode_api_token == ""
|
|
||||||
- lookup('env', 'LINODE_API_TOKEN') == ""
|
|
||||||
|
|
||||||
- name: Display configuration
|
- name: Display configuration
|
||||||
ansible.builtin.debug:
|
ansible.builtin.debug:
|
||||||
@@ -58,26 +55,59 @@
|
|||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
# Optional: Run against discovered Linode hosts
|
# Optional: Run against discovered Linode hosts
|
||||||
- name: Example task against discovered Linode hosts
|
- name: Debug and use discovered Linode hosts
|
||||||
hosts: localhost
|
hosts: localhost
|
||||||
gather_facts: false
|
gather_facts: false
|
||||||
tasks:
|
tasks:
|
||||||
- name: Load dynamic inventory
|
- name: Check if inventory file exists
|
||||||
ansible.builtin.include_vars:
|
ansible.builtin.stat:
|
||||||
file: "{{ linode_inventory_output_dir | default('/tmp/linode_inventory') }}/{{ linode_inventory_output_file | default('linode_inventory.json') }}"
|
path: "{{ linode_inventory_output_dir | default('/tmp/linode_inventory') }}/{{ linode_inventory_output_file | default('linode_inventory.json') }}"
|
||||||
name: dynamic_inventory
|
register: inventory_file_stat
|
||||||
|
|
||||||
|
- name: Display inventory file status
|
||||||
|
ansible.builtin.debug:
|
||||||
|
msg: |
|
||||||
|
Inventory file path: {{ linode_inventory_output_dir | default('/tmp/linode_inventory') }}/{{ linode_inventory_output_file | default('linode_inventory.json') }}
|
||||||
|
File exists: {{ inventory_file_stat.stat.exists }}
|
||||||
|
File size: {{ inventory_file_stat.stat.size | default(0) }} bytes
|
||||||
|
|
||||||
|
- name: Load and display inventory contents
|
||||||
|
ansible.builtin.slurp:
|
||||||
|
src: "{{ linode_inventory_output_dir | default('/tmp/linode_inventory') }}/{{ linode_inventory_output_file | default('linode_inventory.json') }}"
|
||||||
|
register: inventory_content
|
||||||
|
when: inventory_file_stat.stat.exists
|
||||||
|
|
||||||
|
- name: Parse inventory JSON
|
||||||
|
ansible.builtin.set_fact:
|
||||||
|
dynamic_inventory: "{{ inventory_content.content | b64decode | from_json }}"
|
||||||
|
when: inventory_file_stat.stat.exists
|
||||||
|
|
||||||
|
- name: Display parsed inventory summary
|
||||||
|
ansible.builtin.debug:
|
||||||
|
msg: |
|
||||||
|
Inventory loaded successfully!
|
||||||
|
Total hostvars: {{ dynamic_inventory._meta.hostvars | length }}
|
||||||
|
Groups: {{ dynamic_inventory.keys() | reject('equalto', '_meta') | list }}
|
||||||
|
Hosts in hostvars: {{ dynamic_inventory._meta.hostvars.keys() | list }}
|
||||||
|
when: dynamic_inventory is defined
|
||||||
|
|
||||||
- name: Add discovered hosts to in-memory inventory
|
- name: Add discovered hosts to in-memory inventory
|
||||||
ansible.builtin.add_host:
|
ansible.builtin.add_host:
|
||||||
name: "{{ item.key }}"
|
name: "{{ item.key }}"
|
||||||
groups: "{{ group_names | default(['discovered_linodes']) }}"
|
groups: discovered_linodes
|
||||||
ansible_host: "{{ item.value.ansible_host }}"
|
ansible_host: "{{ item.value.ansible_host }}"
|
||||||
linode_id: "{{ item.value.linode_id }}"
|
linode_id: "{{ item.value.linode_id }}"
|
||||||
linode_region: "{{ item.value.linode_region }}"
|
linode_region: "{{ item.value.linode_region }}"
|
||||||
linode_type: "{{ item.value.linode_type }}"
|
linode_type: "{{ item.value.linode_type }}"
|
||||||
linode_status: "{{ item.value.linode_status }}"
|
linode_status: "{{ item.value.linode_status }}"
|
||||||
loop: "{{ dynamic_inventory._meta.hostvars | dict2items }}"
|
loop: "{{ dynamic_inventory._meta.hostvars | dict2items }}"
|
||||||
when: item.value.linode_status == "running"
|
when:
|
||||||
|
- dynamic_inventory is defined
|
||||||
|
- item.value.linode_status == "running"
|
||||||
|
|
||||||
|
- name: Display added hosts
|
||||||
|
ansible.builtin.debug:
|
||||||
|
msg: "Added {{ groups['discovered_linodes'] | default([]) | length }} running Linode hosts to inventory"
|
||||||
|
|
||||||
- name: Test connection to discovered Linode hosts
|
- name: Test connection to discovered Linode hosts
|
||||||
hosts: discovered_linodes
|
hosts: discovered_linodes
|
||||||
|
@@ -58,12 +58,18 @@
|
|||||||
Groups created: {{ linode_inventory_data.keys() | reject('equalto', '_meta') | list | length }}
|
Groups created: {{ linode_inventory_data.keys() | reject('equalto', '_meta') | list | length }}
|
||||||
Inventory saved to: {{ temp_inventory_path }}
|
Inventory saved to: {{ temp_inventory_path }}
|
||||||
|
|
||||||
|
- name: Show raw script output for debugging
|
||||||
|
ansible.builtin.debug:
|
||||||
|
var: linode_inventory_result.stdout
|
||||||
|
when: linode_inventory_result.stdout | length > 0
|
||||||
|
|
||||||
- name: Show discovered hosts
|
- name: Show discovered hosts
|
||||||
ansible.builtin.debug:
|
ansible.builtin.debug:
|
||||||
msg: "Host: {{ item.key }} ({{ item.value.ansible_host }}) - Region: {{ item.value.linode_region }} - Status: {{ item.value.linode_status }}"
|
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: "{{ linode_inventory_data._meta.hostvars | dict2items }}"
|
||||||
loop_control:
|
loop_control:
|
||||||
label: "{{ item.key }}"
|
label: "{{ item.key }}"
|
||||||
|
when: linode_inventory_data._meta.hostvars | length > 0
|
||||||
|
|
||||||
- name: Create static inventory file (optional)
|
- name: Create static inventory file (optional)
|
||||||
ansible.builtin.template:
|
ansible.builtin.template:
|
||||||
@@ -73,15 +79,6 @@
|
|||||||
when: inventory_format == "ini"
|
when: inventory_format == "ini"
|
||||||
delegate_to: localhost
|
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
|
- name: Clean up temporary script
|
||||||
ansible.builtin.file:
|
ansible.builtin.file:
|
||||||
path: "{{ linode_inventory_output_dir }}/linode_inventory.py"
|
path: "{{ linode_inventory_output_dir }}/linode_inventory.py"
|
||||||
|
Reference in New Issue
Block a user