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