From e07d0004358686a80ff4da365b099301f443ad95 Mon Sep 17 00:00:00 2001 From: Kevin Thompson Date: Thu, 7 Aug 2025 13:31:07 -0500 Subject: [PATCH] Reworked script and playbook to include tags --- playbooks/inventory/linode.yaml | 64 +++++++++++++++++-- .../linode/files/linode_inventory.py | 13 +++- 2 files changed, 71 insertions(+), 6 deletions(-) diff --git a/playbooks/inventory/linode.yaml b/playbooks/inventory/linode.yaml index 4ea8bfa..a3f8f94 100644 --- a/playbooks/inventory/linode.yaml +++ b/playbooks/inventory/linode.yaml @@ -100,14 +100,31 @@ linode_region: "{{ item.value.linode_region }}" linode_type: "{{ item.value.linode_type }}" linode_status: "{{ item.value.linode_status }}" + linode_tags: "{{ item.value.linode_tags }}" + is_debian: "{{ item.value.is_debian }}" + is_ubuntu: "{{ item.value.is_ubuntu }}" + 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: Display added hosts + - name: Display added hosts with tag information ansible.builtin.debug: - msg: "Added {{ groups['discovered_linodes'] | default([]) | length }} running Linode hosts to inventory" + msg: | + Added {{ groups['discovered_linodes'] | default([]) | length }} running Linode hosts to inventory + + Host details: + {% 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 @@ -121,6 +138,45 @@ register: ping_result ignore_errors: true - - name: Display connectivity status + - name: Display connectivity status with tag info ansible.builtin.debug: - msg: "{{ inventory_hostname }} ({{ ansible_host }}): {{ 'REACHABLE' if ping_result is succeeded else 'UNREACHABLE' }}" + msg: | + {{ inventory_hostname }} ({{ ansible_host }}): {{ 'REACHABLE' if ping_result is succeeded else 'UNREACHABLE' }} + 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: Example - Control Plane specific tasks + hosts: discovered_linodes + gather_facts: false + vars: + ansible_user: root + 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 + +# Example: Run tasks only on k3s worker nodes +- name: Example - Worker Node specific tasks + hosts: discovered_linodes + gather_facts: false + vars: + ansible_user: root + 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 + +# Example: Run tasks on all k3s nodes (control plane + workers) +- name: Example - All K3s nodes + hosts: discovered_linodes + gather_facts: false + vars: + ansible_user: root + tasks: + - name: K3s cluster task + ansible.builtin.debug: + msg: "This would run on all k3s nodes: {{ inventory_hostname }}" + when: is_k3s | bool diff --git a/roles/inventory/linode/files/linode_inventory.py b/roles/inventory/linode/files/linode_inventory.py index 1b939e0..961679a 100644 --- a/roles/inventory/linode/files/linode_inventory.py +++ b/roles/inventory/linode/files/linode_inventory.py @@ -94,6 +94,7 @@ class LinodeInventory: inventory['ungrouped']['hosts'].append(hostname) # Host variables + linode_tags = instance.get('tags', []) inventory['_meta']['hostvars'][hostname] = { 'ansible_host': primary_ip, 'linode_id': instance['id'], @@ -103,14 +104,22 @@ class LinodeInventory: 'linode_status': instance['status'], 'linode_ipv4': instance.get('ipv4', []), 'linode_ipv6': instance.get('ipv6'), - 'linode_tags': instance.get('tags', []), + 'linode_tags': linode_tags, 'linode_specs': instance.get('specs', {}), 'linode_hypervisor': instance.get('hypervisor'), 'linode_created': instance.get('created'), 'linode_updated': instance.get('updated'), 'linode_group': instance.get('group', ''), 'linode_image': instance.get('image', {}).get('id', '') if isinstance(instance.get('image'), dict) else '', - 'linode_backups': instance.get('backups', {}).get('enabled', False) if isinstance(instance.get('backups'), dict) else False + 'linode_backups': instance.get('backups', {}).get('enabled', False) if isinstance(instance.get('backups'), dict) else False, + # Add individual tag variables for easy access + 'is_debian': 'Debian' in linode_tags, + 'is_ubuntu': 'Ubuntu' in linode_tags, + 'is_k3s': 'k3s' in linode_tags, + 'is_control_plane': 'control-plane' in linode_tags, + 'is_worker_node': 'worker-node' in linode_tags, + # Tag string for easy filtering + 'tag_string': ','.join(linode_tags).lower(), } # Group by region