Got some files mixed up. Whoopsie.
This commit is contained in:
@@ -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:
|
||||||
|
@@ -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
|
|
||||||
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
|
|
||||||
ansible.builtin.debug:
|
|
||||||
msg: |
|
|
||||||
Linode Inventory Configuration:
|
|
||||||
Output directory: {{ linode_inventory_output_dir }}
|
|
||||||
Output format: {{ inventory_format }}
|
|
||||||
AWX integration: {{ awx_integration }}
|
|
||||||
Include only running: {{ include_only_running }}
|
|
||||||
|
|
||||||
roles:
|
|
||||||
- role: linode_inventory
|
|
||||||
vars:
|
|
||||||
linode_api_token: "{{ linode_api_token }}"
|
|
||||||
|
|
||||||
post_tasks:
|
|
||||||
- name: Display next steps
|
|
||||||
ansible.builtin.debug:
|
|
||||||
msg: |
|
|
||||||
Inventory update complete!
|
|
||||||
|
|
||||||
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: Debug and use discovered Linode hosts
|
|
||||||
hosts: localhost
|
|
||||||
gather_facts: false
|
|
||||||
tasks:
|
|
||||||
- name: Check if inventory file exists
|
|
||||||
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
|
|
||||||
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:
|
ansible.builtin.set_fact:
|
||||||
dynamic_inventory: "{{ inventory_content.content | b64decode | from_json }}"
|
linode_api_token: "{{ linode_api_token | default(lookup('env', 'LINODE_API_TOKEN')) | default('') }}"
|
||||||
when: inventory_file_stat.stat.exists
|
|
||||||
|
|
||||||
- name: Display parsed inventory summary
|
- 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: Show script execution details
|
||||||
ansible.builtin.debug:
|
ansible.builtin.debug:
|
||||||
msg: |
|
msg: |
|
||||||
Inventory loaded successfully!
|
Script execution results:
|
||||||
Total hostvars: {{ dynamic_inventory._meta.hostvars | length }}
|
Return code: {{ linode_inventory_result.rc }}
|
||||||
Groups: {{ dynamic_inventory.keys() | reject('equalto', '_meta') | list }}
|
Stdout length: {{ linode_inventory_result.stdout | length }}
|
||||||
Hosts in hostvars: {{ dynamic_inventory._meta.hostvars.keys() | list }}
|
Stderr length: {{ linode_inventory_result.stderr | length }}
|
||||||
when: dynamic_inventory is defined
|
|
||||||
|
|
||||||
- name: Add discovered hosts to in-memory inventory
|
- name: Show stderr if present
|
||||||
ansible.builtin.add_host:
|
ansible.builtin.debug:
|
||||||
name: "{{ item.key }}"
|
msg: "Script stderr: {{ linode_inventory_result.stderr }}"
|
||||||
groups: discovered_linodes
|
when: linode_inventory_result.stderr | length > 0
|
||||||
ansible_host: "{{ item.value.ansible_host }}"
|
|
||||||
linode_id: "{{ item.value.linode_id }}"
|
- name: Show stdout if present
|
||||||
linode_region: "{{ item.value.linode_region }}"
|
ansible.builtin.debug:
|
||||||
linode_type: "{{ item.value.linode_type }}"
|
msg: "Script stdout: {{ linode_inventory_result.stdout }}"
|
||||||
linode_status: "{{ item.value.linode_status }}"
|
when: linode_inventory_result.stdout | length > 0
|
||||||
linode_tags: "{{ item.value.linode_tags }}"
|
|
||||||
is_debian: "{{ item.value.is_debian }}"
|
- name: Test API token directly
|
||||||
is_ubuntu: "{{ item.value.is_ubuntu }}"
|
ansible.builtin.uri:
|
||||||
is_k3s: "{{ item.value.is_k3s }}"
|
url: "https://api.linode.com/v4/linode/instances"
|
||||||
is_control_plane: "{{ item.value.is_control_plane }}"
|
method: GET
|
||||||
is_worker_node: "{{ item.value.is_worker_node }}"
|
headers:
|
||||||
tag_string: "{{ item.value.tag_string }}"
|
Authorization: "Bearer {{ linode_api_token }}"
|
||||||
loop: "{{ dynamic_inventory._meta.hostvars | dict2items }}"
|
Content-Type: "application/json"
|
||||||
|
return_content: yes
|
||||||
|
status_code: [200, 401, 403]
|
||||||
|
register: direct_api_test
|
||||||
|
delegate_to: localhost
|
||||||
|
|
||||||
|
- name: Display direct API test results
|
||||||
|
ansible.builtin.debug:
|
||||||
|
msg: |
|
||||||
|
Direct API test results:
|
||||||
|
Status: {{ direct_api_test.status }}
|
||||||
|
Response: {{ direct_api_test.json | default('No JSON response') }}
|
||||||
|
|
||||||
|
- name: Parse inventory JSON (only if stdout exists)
|
||||||
|
ansible.builtin.set_fact:
|
||||||
|
linode_inventory_data: "{{ linode_inventory_result.stdout | from_json }}"
|
||||||
when:
|
when:
|
||||||
- dynamic_inventory is defined
|
- linode_inventory_result.stdout | length > 0
|
||||||
- item.value.linode_status == "running"
|
- linode_inventory_result.rc == 0
|
||||||
|
|
||||||
- name: Display added hosts with tag information
|
- name: Set empty inventory if script failed
|
||||||
|
ansible.builtin.set_fact:
|
||||||
|
linode_inventory_data:
|
||||||
|
_meta:
|
||||||
|
hostvars: {}
|
||||||
|
all:
|
||||||
|
children: ['ungrouped']
|
||||||
|
ungrouped:
|
||||||
|
hosts: []
|
||||||
|
when: linode_inventory_data is not defined
|
||||||
|
|
||||||
|
- 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:
|
ansible.builtin.debug:
|
||||||
msg: |
|
msg: |
|
||||||
Added {{ groups['discovered_linodes'] | default([]) | length }} running Linode hosts to inventory
|
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 }}
|
||||||
|
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' }}
|
||||||
|
|
||||||
Host details:
|
- name: Show raw script output for debugging
|
||||||
{% for host in groups['discovered_linodes'] | default([]) %}
|
|
||||||
- {{ host }} ({{ hostvars[host]['ansible_host'] }})
|
|
||||||
Tags: {{ hostvars[host]['linode_tags'] | join(', ') }}
|
|
||||||
K3s: {{ hostvars[host]['is_k3s'] }}
|
|
||||||
Control Plane: {{ hostvars[host]['is_control_plane'] }}
|
|
||||||
Worker: {{ hostvars[host]['is_worker_node'] }}
|
|
||||||
{% endfor %}
|
|
||||||
|
|
||||||
- name: Test connection to discovered Linode hosts
|
|
||||||
hosts: discovered_linodes
|
|
||||||
gather_facts: false
|
|
||||||
vars:
|
|
||||||
ansible_user: phlux
|
|
||||||
ansible_ssh_common_args: '-o StrictHostKeyChecking=no -o ConnectTimeout=10'
|
|
||||||
tasks:
|
|
||||||
- name: Test connectivity
|
|
||||||
ansible.builtin.ping:
|
|
||||||
register: ping_result
|
|
||||||
ignore_errors: true
|
|
||||||
|
|
||||||
- name: Display connectivity status with tag info
|
|
||||||
ansible.builtin.debug:
|
ansible.builtin.debug:
|
||||||
msg: |
|
var: linode_inventory_result.stdout
|
||||||
{{ inventory_hostname }} ({{ ansible_host }}): {{ 'REACHABLE' if ping_result is succeeded else 'UNREACHABLE' }}
|
when: linode_inventory_result.stdout | length > 0
|
||||||
Tags: {{ linode_tags | join(', ') }}
|
|
||||||
Role: {{ 'Control Plane' if is_control_plane else 'Worker Node' if is_worker_node else 'Other' }}
|
|
||||||
|
|
||||||
# Example: Run tasks only on k3s control plane nodes
|
- name: Show discovered hosts
|
||||||
- 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:
|
ansible.builtin.debug:
|
||||||
msg: "This would run control plane specific commands on {{ inventory_hostname }}"
|
msg: "Host: {{ item.key }} ({{ item.value.ansible_host }}) - Region: {{ item.value.linode_region }} - Status: {{ item.value.linode_status }}"
|
||||||
when: is_control_plane | bool
|
loop: "{{ linode_inventory_data._meta.hostvars | dict2items }}"
|
||||||
|
loop_control:
|
||||||
|
label: "{{ item.key }}"
|
||||||
|
when: linode_inventory_data._meta.hostvars | length > 0
|
||||||
|
|
||||||
# Example: Run tasks only on k3s worker nodes
|
- name: Create static inventory file (optional)
|
||||||
- name: Example - Worker Node specific tasks
|
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: 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: Clean up temporary script
|
||||||
- name: Example - All K3s nodes
|
ansible.builtin.file:
|
||||||
hosts: discovered_linodes
|
path: "{{ linode_inventory_output_dir }}/linode_inventory.py"
|
||||||
gather_facts: false
|
state: absent
|
||||||
vars:
|
delegate_to: localhost
|
||||||
ansible_user: phlux
|
when: cleanup_temp_files | default(true)
|
||||||
tasks:
|
|
||||||
- name: K3s cluster task
|
|
||||||
ansible.builtin.debug:
|
|
||||||
msg: "This would run on all k3s nodes: {{ inventory_hostname }}"
|
|
||||||
when: is_k3s | bool
|
|
||||||
|
Reference in New Issue
Block a user