Rewrite the linode inventory stuff...this is a pain in the ass.
This commit is contained in:
@@ -1,214 +1,83 @@
|
||||
---
|
||||
# Main tasks for linode_inventory role
|
||||
|
||||
- name: Set API token (AWX credential injection takes precedence)
|
||||
ansible.builtin.set_fact:
|
||||
linode_api_token: "{{ linode_api_token | default(lookup('env', 'LINODE_API_TOKEN')) | default('') }}"
|
||||
- name: Validate Linode API token
|
||||
ansible.builtin.fail:
|
||||
msg: "LINODE_API_TOKEN environment variable must be set"
|
||||
when: lookup('env', 'LINODE_API_TOKEN') == ""
|
||||
|
||||
- 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: Fetch Linode instances
|
||||
ansible.builtin.uri:
|
||||
url: "https://api.linode.com/v4/linode/instances"
|
||||
method: GET
|
||||
headers:
|
||||
Authorization: "Bearer {{ lookup('env', 'LINODE_API_TOKEN') }}"
|
||||
Content-Type: "application/json"
|
||||
return_content: yes
|
||||
status_code: 200
|
||||
register: linode_response
|
||||
|
||||
- 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: Parse inventory JSON (only if stdout exists)
|
||||
ansible.builtin.set_fact:
|
||||
linode_inventory_data: "{{ linode_inventory_result.stdout | from_json }}"
|
||||
when:
|
||||
- linode_inventory_result.stdout | length > 0
|
||||
- linode_inventory_result.rc == 0
|
||||
|
||||
- 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: Display inventory summary
|
||||
- name: Display API response summary
|
||||
ansible.builtin.debug:
|
||||
msg: |
|
||||
Linode Dynamic Inventory Summary:
|
||||
Total hosts discovered: {{ linode_inventory_data._meta.hostvars | length }}
|
||||
Groups created: {{ linode_inventory_data.keys() | reject('equalto', '_meta') | list | length }}
|
||||
Found {{ linode_response.json.data | length }} Linode instances
|
||||
Running instances: {{ linode_response.json.data | selectattr('status', 'equalto', 'running') | list | length }}
|
||||
|
||||
- name: Show discovered hosts
|
||||
ansible.builtin.debug:
|
||||
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 }}"
|
||||
- name: Add Linode hosts to in-memory inventory
|
||||
ansible.builtin.add_host:
|
||||
name: "{{ item.label }}"
|
||||
groups:
|
||||
- linode_all
|
||||
- "region_{{ item.region | replace('-', '_') }}"
|
||||
- "type_{{ item.type | replace('-', '_') | replace('.', '_') }}"
|
||||
- "status_{{ item.status }}"
|
||||
- "{% for tag in item.tags %}tag_{{ tag | replace('-', '_') | replace(' ', '_') | replace('.', '_') }}{% if not loop.last %},{% endif %}{% endfor %}"
|
||||
ansible_host: "{{ item.ipv4[0] }}"
|
||||
ansible_user: "phlux"
|
||||
ansible_ssh_common_args: "-o StrictHostKeyChecking=no"
|
||||
linode_id: "{{ item.id }}"
|
||||
linode_label: "{{ item.label }}"
|
||||
linode_region: "{{ item.region }}"
|
||||
linode_type: "{{ item.type }}"
|
||||
linode_status: "{{ item.status }}"
|
||||
linode_tags: "{{ item.tags }}"
|
||||
linode_ipv4: "{{ item.ipv4 }}"
|
||||
linode_ipv6: "{{ item.ipv6 | default('') }}"
|
||||
# Convenience boolean flags
|
||||
is_k3s: "{{ 'k3s' in item.tags }}"
|
||||
is_control_plane: "{{ 'control-plane' in item.tags }}"
|
||||
is_worker_node: "{{ 'worker-node' in item.tags }}"
|
||||
is_debian: "{{ 'Debian' in item.tags }}"
|
||||
is_ubuntu: "{{ 'Ubuntu' in item.tags }}"
|
||||
loop: "{{ linode_response.json.data }}"
|
||||
when:
|
||||
- item.status == "running"
|
||||
- item.ipv4 | length > 0
|
||||
loop_control:
|
||||
label: "{{ item.key }}"
|
||||
when: linode_inventory_data._meta.hostvars | length > 0
|
||||
label: "{{ item.label }}"
|
||||
|
||||
- name: Debug current directory and Git status
|
||||
ansible.builtin.shell: |
|
||||
echo "Current directory: $(pwd)" &&
|
||||
echo "Directory contents:" &&
|
||||
ls -la &&
|
||||
echo "Git status:" &&
|
||||
git status 2>&1 || echo "Not a git repository" &&
|
||||
echo "Git remote:" &&
|
||||
git remote -v 2>&1 || echo "No git remotes"
|
||||
register: debug_git_info
|
||||
delegate_to: localhost
|
||||
ignore_errors: true
|
||||
|
||||
- name: Initialize Git if needed and configure
|
||||
ansible.builtin.shell: |
|
||||
if [ ! -d ".git" ]; then
|
||||
echo "Not in a git repository, checking for git in parent directories"
|
||||
git_root=$(git rev-parse --show-toplevel 2>/dev/null || echo "")
|
||||
if [ -n "$git_root" ]; then
|
||||
cd "$git_root"
|
||||
echo "Found git repository at: $git_root"
|
||||
else
|
||||
echo "No git repository found"
|
||||
exit 0
|
||||
fi
|
||||
fi &&
|
||||
git config user.email "awx@ewnix.net" &&
|
||||
git config user.name "AWX Automation" &&
|
||||
git remote set-url origin git@git.ewnix.net:phlux/ewnix-automation.git &&
|
||||
echo "Git configured successfully"
|
||||
register: git_config_result
|
||||
delegate_to: localhost
|
||||
ignore_errors: true
|
||||
|
||||
- name: Check SSH configuration and keys
|
||||
ansible.builtin.shell: |
|
||||
echo "=== SSH configuration ==="
|
||||
echo "SSH_AUTH_SOCK: ${SSH_AUTH_SOCK:-not set}"
|
||||
echo "=== SSH keys available ==="
|
||||
ssh-add -l 2>/dev/null || echo "No SSH agent or no keys loaded"
|
||||
echo "=== SSH config ==="
|
||||
ls -la ~/.ssh/ 2>/dev/null || echo "No .ssh directory"
|
||||
echo "=== Test SSH to git.ewnix.net ==="
|
||||
timeout 10 ssh -T git@git.ewnix.net -o ConnectTimeout=5 -o StrictHostKeyChecking=no 2>&1 || echo "SSH test completed"
|
||||
register: ssh_debug
|
||||
delegate_to: localhost
|
||||
ignore_errors: true
|
||||
|
||||
- name: Create inventory directory
|
||||
ansible.builtin.file:
|
||||
path: "inventory"
|
||||
state: directory
|
||||
mode: '0755'
|
||||
delegate_to: localhost
|
||||
|
||||
- name: Create JSON inventory file
|
||||
ansible.builtin.copy:
|
||||
content: "{{ linode_inventory_data | to_nice_json }}"
|
||||
dest: "inventory/linode_hosts.json"
|
||||
mode: '0644'
|
||||
delegate_to: localhost
|
||||
when: linode_inventory_data is defined
|
||||
|
||||
- name: Check if inventory file was created
|
||||
ansible.builtin.stat:
|
||||
path: "inventory/linode_hosts.json"
|
||||
register: inventory_file_check
|
||||
delegate_to: localhost
|
||||
|
||||
- name: Attempt to commit and push inventory
|
||||
ansible.builtin.shell: |
|
||||
git_root=$(git rev-parse --show-toplevel 2>/dev/null || echo "")
|
||||
if [ -n "$git_root" ]; then
|
||||
cd "$git_root" &&
|
||||
echo "Working in git repository: $git_root" &&
|
||||
git add inventory/linode_hosts.json &&
|
||||
if git diff --staged --quiet; then
|
||||
echo "No changes to commit"
|
||||
else
|
||||
git commit -m "Update Linode inventory - $(date '+%Y-%m-%d %H:%M:%S') [AWX]" &&
|
||||
echo "Committed successfully, attempting push..." &&
|
||||
timeout 30 git push origin HEAD &&
|
||||
echo "Push successful!"
|
||||
fi
|
||||
else
|
||||
echo "No git repository found - inventory file created but not committed"
|
||||
fi
|
||||
register: git_commit_result
|
||||
delegate_to: localhost
|
||||
ignore_errors: true
|
||||
when:
|
||||
- linode_inventory_data is defined
|
||||
- push_to_git | default(true)
|
||||
- inventory_file_check.stat.exists
|
||||
|
||||
- name: Display comprehensive debug information
|
||||
- name: Display discovered hosts summary
|
||||
ansible.builtin.debug:
|
||||
msg: |
|
||||
=== COMPREHENSIVE DEBUG RESULTS ===
|
||||
🎉 Successfully added {{ groups['linode_all'] | default([]) | length }} Linode hosts to inventory!
|
||||
|
||||
Current Directory Info:
|
||||
{% if debug_git_info is defined and debug_git_info.stdout is defined %}
|
||||
{{ debug_git_info.stdout }}
|
||||
{% else %}
|
||||
Debug info not available
|
||||
{% endif %}
|
||||
📋 Hosts discovered:
|
||||
{% for host in groups['linode_all'] | default([]) %}
|
||||
- {{ host }} ({{ hostvars[host]['ansible_host'] }})
|
||||
Region: {{ hostvars[host]['linode_region'] }}
|
||||
Type: {{ hostvars[host]['linode_type'] }}
|
||||
Tags: {{ hostvars[host]['linode_tags'] | join(', ') }}
|
||||
{% endfor %}
|
||||
|
||||
SSH Configuration:
|
||||
{% if ssh_debug is defined and ssh_debug.stdout is defined %}
|
||||
{{ ssh_debug.stdout }}
|
||||
{% else %}
|
||||
SSH debug not available
|
||||
{% endif %}
|
||||
📂 Groups available for targeting:
|
||||
{% for group in groups.keys() | sort if group.startswith(('tag_', 'region_', 'type_', 'status_')) %}
|
||||
- {{ group }}: {{ groups[group] | length }} hosts
|
||||
{% endfor %}
|
||||
|
||||
Git Configuration:
|
||||
{% if git_config_result is defined and git_config_result.stdout is defined %}
|
||||
{{ git_config_result.stdout }}
|
||||
{% else %}
|
||||
Git config not available
|
||||
{% endif %}
|
||||
💡 Use these groups in your job templates:
|
||||
- tag_k3s: All k3s cluster nodes
|
||||
- tag_control_plane: Control plane nodes only
|
||||
- tag_worker_node: Worker nodes only
|
||||
- region_us_southeast: All hosts in us-southeast
|
||||
|
||||
Git Commit/Push Results:
|
||||
{% if git_commit_result is defined and git_commit_result.stdout is defined %}
|
||||
{{ git_commit_result.stdout }}
|
||||
{% else %}
|
||||
Git commit result not available
|
||||
{% endif %}
|
||||
|
||||
Inventory File Status:
|
||||
- Exists: {{ inventory_file_check.stat.exists if inventory_file_check is defined else 'Unknown' }}
|
||||
{% if inventory_file_check is defined and inventory_file_check.stat.exists %}
|
||||
- Size: {{ inventory_file_check.stat.size }} bytes
|
||||
{% endif %}
|
||||
|
||||
- name: Clean up temporary script
|
||||
ansible.builtin.file:
|
||||
path: "{{ linode_inventory_output_dir }}/linode_inventory.py"
|
||||
state: absent
|
||||
delegate_to: localhost
|
||||
when: cleanup_temp_files | default(true)
|
||||
✅ Hosts are now available for subsequent job templates in this workflow!
|
||||
|
Reference in New Issue
Block a user