Files
ewnix-automation/roles/inventory/linode/tasks/main.yml
2025-08-07 14:29:52 -05:00

215 lines
7.0 KiB
YAML

---
# 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 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: 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
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 }}
- 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 }}"
loop_control:
label: "{{ item.key }}"
when: linode_inventory_data._meta.hostvars | length > 0
- 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
ansible.builtin.debug:
msg: |
=== COMPREHENSIVE DEBUG RESULTS ===
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 %}
SSH Configuration:
{% if ssh_debug is defined and ssh_debug.stdout is defined %}
{{ ssh_debug.stdout }}
{% else %}
SSH debug not available
{% endif %}
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 %}
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)