Got some files mixed up. Whoopsie.

This commit is contained in:
2025-08-07 13:39:56 -05:00
parent 0b0ae30967
commit 033d61cfe2
2 changed files with 128 additions and 171 deletions

View File

@@ -32,7 +32,7 @@
Include only running: {{ include_only_running }} Include only running: {{ include_only_running }}
roles: roles:
- role: roles/inventory/linode - role: linode_inventory
vars: vars:
linode_api_token: "{{ linode_api_token }}" linode_api_token: "{{ linode_api_token }}"
@@ -130,7 +130,7 @@
hosts: discovered_linodes hosts: discovered_linodes
gather_facts: false gather_facts: false
vars: vars:
ansible_user: root ansible_user: phlux
ansible_ssh_common_args: '-o StrictHostKeyChecking=no -o ConnectTimeout=10' ansible_ssh_common_args: '-o StrictHostKeyChecking=no -o ConnectTimeout=10'
tasks: tasks:
- name: Test connectivity - name: Test connectivity
@@ -150,7 +150,7 @@
hosts: discovered_linodes hosts: discovered_linodes
gather_facts: false gather_facts: false
vars: vars:
ansible_user: root ansible_user: phlux
tasks: tasks:
- name: Control plane specific task - name: Control plane specific task
ansible.builtin.debug: ansible.builtin.debug:
@@ -162,7 +162,7 @@
hosts: discovered_linodes hosts: discovered_linodes
gather_facts: false gather_facts: false
vars: vars:
ansible_user: root ansible_user: phlux
tasks: tasks:
- name: Worker node specific task - name: Worker node specific task
ansible.builtin.debug: ansible.builtin.debug:
@@ -174,7 +174,7 @@
hosts: discovered_linodes hosts: discovered_linodes
gather_facts: false gather_facts: false
vars: vars:
ansible_user: root ansible_user: phlux
tasks: tasks:
- name: K3s cluster task - name: K3s cluster task
ansible.builtin.debug: ansible.builtin.debug:

View File

@@ -1,182 +1,139 @@
--- ---
- name: Update Linode Dynamic Inventory # Main tasks for linode_inventory role
hosts: localhost
gather_facts: true
connection: local
vars: - name: Set API token (AWX credential injection takes precedence)
# Override these variables as needed ansible.builtin.set_fact:
linode_inventory_output_dir: "/tmp/linode_inventory" linode_api_token: "{{ linode_api_token | default(lookup('env', 'LINODE_API_TOKEN')) | default('') }}"
inventory_format: "json" # or "ini"
awx_integration: true
cleanup_temp_files: false
# Optional filters - name: Validate required variables
include_only_running: false ansible.builtin.assert:
specific_regions: [] # e.g., ['us-east', 'us-west'] that:
specific_tags: [] # e.g., ['production', 'web'] - 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
pre_tasks: - name: Ensure output directory exists
- name: Check for Linode API token (will be injected by AWX credential) ansible.builtin.file:
ansible.builtin.fail: path: "{{ linode_inventory_output_dir }}"
msg: "Linode API Token credential must be attached to this job template in AWX" state: directory
when: linode_api_token is undefined or linode_api_token == "" mode: '0755'
delegate_to: localhost
- name: Display configuration - name: Copy Linode inventory script
ansible.builtin.debug: ansible.builtin.copy:
msg: | src: linode_inventory.py
Linode Inventory Configuration: dest: "{{ linode_inventory_output_dir }}/linode_inventory.py"
Output directory: {{ linode_inventory_output_dir }} mode: '0755'
Output format: {{ inventory_format }} delegate_to: localhost
AWX integration: {{ awx_integration }}
Include only running: {{ include_only_running }}
roles: - name: Execute Linode inventory script
- role: linode_inventory ansible.builtin.command:
vars: cmd: python3 {{ linode_inventory_output_dir }}/linode_inventory.py --list
linode_api_token: "{{ linode_api_token }}" environment:
LINODE_API_TOKEN: "{{ linode_api_token }}"
register: linode_inventory_result
delegate_to: localhost
changed_when: true
post_tasks: - name: Show script execution details
- name: Display next steps ansible.builtin.debug:
ansible.builtin.debug: msg: |
msg: | Script execution results:
Inventory update complete! Return code: {{ linode_inventory_result.rc }}
Stdout length: {{ linode_inventory_result.stdout | length }}
Stderr length: {{ linode_inventory_result.stderr | length }}
Next steps for AWX integration: - name: Show stderr if present
1. Copy the inventory script to your SCM repository ansible.builtin.debug:
2. Create a custom inventory source in AWX msg: "Script stderr: {{ linode_inventory_result.stderr }}"
3. Point it to the linode_inventory.py script when: linode_inventory_result.stderr | length > 0
4. Set up the Linode API credential
Files created: - name: Show stdout if present
- JSON inventory: {{ linode_inventory_output_dir }}/{{ linode_inventory_output_file }} ansible.builtin.debug:
{% if inventory_format == "ini" %} msg: "Script stdout: {{ linode_inventory_result.stdout }}"
- INI inventory: {{ linode_inventory_output_dir }}/linode_static_inventory.ini when: linode_inventory_result.stdout | length > 0
{% endif %}
# Optional: Run against discovered Linode hosts - name: Test API token directly
- name: Debug and use discovered Linode hosts ansible.builtin.uri:
hosts: localhost url: "https://api.linode.com/v4/linode/instances"
gather_facts: false method: GET
tasks: headers:
- name: Check if inventory file exists Authorization: "Bearer {{ linode_api_token }}"
ansible.builtin.stat: Content-Type: "application/json"
path: "{{ linode_inventory_output_dir | default('/tmp/linode_inventory') }}/{{ linode_inventory_output_file | default('linode_inventory.json') }}" return_content: yes
register: inventory_file_stat status_code: [200, 401, 403]
register: direct_api_test
delegate_to: localhost
- name: Display inventory file status - name: Display direct API test results
ansible.builtin.debug: ansible.builtin.debug:
msg: | msg: |
Inventory file path: {{ linode_inventory_output_dir | default('/tmp/linode_inventory') }}/{{ linode_inventory_output_file | default('linode_inventory.json') }} Direct API test results:
File exists: {{ inventory_file_stat.stat.exists }} Status: {{ direct_api_test.status }}
File size: {{ inventory_file_stat.stat.size | default(0) }} bytes Response: {{ direct_api_test.json | default('No JSON response') }}
- name: Load and display inventory contents - name: Parse inventory JSON (only if stdout exists)
ansible.builtin.slurp: ansible.builtin.set_fact:
src: "{{ linode_inventory_output_dir | default('/tmp/linode_inventory') }}/{{ linode_inventory_output_file | default('linode_inventory.json') }}" linode_inventory_data: "{{ linode_inventory_result.stdout | from_json }}"
register: inventory_content when:
when: inventory_file_stat.stat.exists - linode_inventory_result.stdout | length > 0
- linode_inventory_result.rc == 0
- name: Parse inventory JSON - name: Set empty inventory if script failed
ansible.builtin.set_fact: ansible.builtin.set_fact:
dynamic_inventory: "{{ inventory_content.content | b64decode | from_json }}" linode_inventory_data:
when: inventory_file_stat.stat.exists _meta:
hostvars: {}
all:
children: ['ungrouped']
ungrouped:
hosts: []
when: linode_inventory_data is not defined
- name: Display parsed inventory summary - name: Save inventory to file
ansible.builtin.debug: ansible.builtin.copy:
msg: | content: "{{ linode_inventory_data | to_nice_json }}"
Inventory loaded successfully! dest: "{{ temp_inventory_path }}"
Total hostvars: {{ dynamic_inventory._meta.hostvars | length }} mode: '0644'
Groups: {{ dynamic_inventory.keys() | reject('equalto', '_meta') | list }} delegate_to: localhost
Hosts in hostvars: {{ dynamic_inventory._meta.hostvars.keys() | list }}
when: dynamic_inventory is defined
- name: Add discovered hosts to in-memory inventory - name: Display inventory summary
ansible.builtin.add_host: ansible.builtin.debug:
name: "{{ item.key }}" msg: |
groups: discovered_linodes Linode Dynamic Inventory Summary:
ansible_host: "{{ item.value.ansible_host }}" Total hosts discovered: {{ linode_inventory_data._meta.hostvars | length }}
linode_id: "{{ item.value.linode_id }}" Groups created: {{ linode_inventory_data.keys() | reject('equalto', '_meta') | list | length }}
linode_region: "{{ item.value.linode_region }}" Inventory saved to: {{ temp_inventory_path }}
linode_type: "{{ item.value.linode_type }}" API Token status: {{ 'Set (' + (linode_api_token[:8] + '...' if linode_api_token | length > 8 else linode_api_token) + ')' if linode_api_token is defined else 'NOT SET' }}
linode_status: "{{ item.value.linode_status }}"
linode_tags: "{{ item.value.linode_tags }}"
is_debian: "{{ item.value.is_debian }}"
is_ubuntu: "{{ item.value.is_ubuntu }}"
is_k3s: "{{ item.value.is_k3s }}"
is_control_plane: "{{ item.value.is_control_plane }}"
is_worker_node: "{{ item.value.is_worker_node }}"
tag_string: "{{ item.value.tag_string }}"
loop: "{{ dynamic_inventory._meta.hostvars | dict2items }}"
when:
- dynamic_inventory is defined
- item.value.linode_status == "running"
- name: Display added hosts with tag information - name: Show raw script output for debugging
ansible.builtin.debug: ansible.builtin.debug:
msg: | var: linode_inventory_result.stdout
Added {{ groups['discovered_linodes'] | default([]) | length }} running Linode hosts to inventory when: linode_inventory_result.stdout | length > 0
Host details: - name: Show discovered hosts
{% for host in groups['discovered_linodes'] | default([]) %} ansible.builtin.debug:
- {{ host }} ({{ hostvars[host]['ansible_host'] }}) msg: "Host: {{ item.key }} ({{ item.value.ansible_host }}) - Region: {{ item.value.linode_region }} - Status: {{ item.value.linode_status }}"
Tags: {{ hostvars[host]['linode_tags'] | join(', ') }} loop: "{{ linode_inventory_data._meta.hostvars | dict2items }}"
K3s: {{ hostvars[host]['is_k3s'] }} loop_control:
Control Plane: {{ hostvars[host]['is_control_plane'] }} label: "{{ item.key }}"
Worker: {{ hostvars[host]['is_worker_node'] }} when: linode_inventory_data._meta.hostvars | length > 0
{% endfor %}
- name: Test connection to discovered Linode hosts - name: Create static inventory file (optional)
hosts: discovered_linodes ansible.builtin.template:
gather_facts: false src: inventory.ini.j2
vars: dest: "{{ linode_inventory_output_dir }}/linode_static_inventory.ini"
ansible_user: phlux mode: '0644'
ansible_ssh_common_args: '-o StrictHostKeyChecking=no -o ConnectTimeout=10' when: inventory_format == "ini"
tasks: delegate_to: localhost
- name: Test connectivity
ansible.builtin.ping:
register: ping_result
ignore_errors: true
- name: Display connectivity status with tag info - name: Clean up temporary script
ansible.builtin.debug: ansible.builtin.file:
msg: | path: "{{ linode_inventory_output_dir }}/linode_inventory.py"
{{ inventory_hostname }} ({{ ansible_host }}): {{ 'REACHABLE' if ping_result is succeeded else 'UNREACHABLE' }} state: absent
Tags: {{ linode_tags | join(', ') }} delegate_to: localhost
Role: {{ 'Control Plane' if is_control_plane else 'Worker Node' if is_worker_node else 'Other' }} when: cleanup_temp_files | default(true)
# Example: Run tasks only on k3s control plane nodes
- name: Example - Control Plane specific tasks
hosts: discovered_linodes
gather_facts: false
vars:
ansible_user: phlux
tasks:
- name: Control plane specific task
ansible.builtin.debug:
msg: "This would run control plane specific commands on {{ inventory_hostname }}"
when: is_control_plane | bool
# Example: Run tasks only on k3s worker nodes
- name: Example - Worker Node specific tasks
hosts: discovered_linodes
gather_facts: false
vars:
ansible_user: phlux
tasks:
- name: Worker node specific task
ansible.builtin.debug:
msg: "This would run worker node specific commands on {{ inventory_hostname }}"
when: is_worker_node | bool
# Example: Run tasks on all k3s nodes (control plane + workers)
- name: Example - All K3s nodes
hosts: discovered_linodes
gather_facts: false
vars:
ansible_user: phlux
tasks:
- name: K3s cluster task
ansible.builtin.debug:
msg: "This would run on all k3s nodes: {{ inventory_hostname }}"
when: is_k3s | bool