Files
ewnix-automation/roles/inventory/linode/tasks/main.yml

84 lines
3.1 KiB
YAML

---
# Main tasks for linode_inventory role
- name: Validate Linode API token
ansible.builtin.fail:
msg: "LINODE_API_TOKEN environment variable must be set"
when: lookup('env', 'LINODE_API_TOKEN') == ""
- 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: Display API response summary
ansible.builtin.debug:
msg: |
Found {{ linode_response.json.data | length }} Linode instances
Running instances: {{ linode_response.json.data | selectattr('status', 'equalto', 'running') | list | length }}
- 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.label }}"
- name: Display discovered hosts summary
ansible.builtin.debug:
msg: |
🎉 Successfully added {{ groups['linode_all'] | default([]) | length }} Linode hosts to inventory!
📋 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 %}
📂 Groups available for targeting:
{% for group in groups.keys() | sort if group.startswith(('tag_', 'region_', 'type_', 'status_')) %}
- {{ group }}: {{ groups[group] | length }} hosts
{% endfor %}
💡 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
✅ Hosts are now available for subsequent job templates in this workflow!