Several changes to include SSHing as the proper user with the right key

This commit is contained in:
2025-08-07 13:36:33 -05:00
parent e07d000435
commit 0b0ae30967
4 changed files with 177 additions and 124 deletions

9
inventory/hosts Normal file
View File

@@ -0,0 +1,9 @@
[local]
localhost ansible_connection=local
[linode:children]
# Dynamic groups will be populated by the inventory script
[all:vars]
ansible_user=phlux
ansible_ssh_common_args='-o StrictHostKeyChecking=no'

View File

@@ -97,6 +97,7 @@ class LinodeInventory:
linode_tags = instance.get('tags', []) linode_tags = instance.get('tags', [])
inventory['_meta']['hostvars'][hostname] = { inventory['_meta']['hostvars'][hostname] = {
'ansible_host': primary_ip, 'ansible_host': primary_ip,
'ansible_user': 'phlux', # Set default SSH user
'linode_id': instance['id'], 'linode_id': instance['id'],
'linode_label': instance['label'], 'linode_label': instance['label'],
'linode_region': instance['region'], 'linode_region': instance['region'],

View File

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

View File

@@ -18,5 +18,5 @@
{% endfor %} {% endfor %}
[all:vars] [all:vars]
ansible_user=root ansible_user=phlux
ansible_ssh_common_args='-o StrictHostKeyChecking=no' ansible_ssh_common_args='-o StrictHostKeyChecking=no'