Reworked script and playbook to include tags
This commit is contained in:
@@ -100,14 +100,31 @@
|
|||||||
linode_region: "{{ item.value.linode_region }}"
|
linode_region: "{{ item.value.linode_region }}"
|
||||||
linode_type: "{{ item.value.linode_type }}"
|
linode_type: "{{ item.value.linode_type }}"
|
||||||
linode_status: "{{ item.value.linode_status }}"
|
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 }}"
|
loop: "{{ dynamic_inventory._meta.hostvars | dict2items }}"
|
||||||
when:
|
when:
|
||||||
- dynamic_inventory is defined
|
- dynamic_inventory is defined
|
||||||
- item.value.linode_status == "running"
|
- item.value.linode_status == "running"
|
||||||
|
|
||||||
- name: Display added hosts
|
- name: Display added hosts with tag information
|
||||||
ansible.builtin.debug:
|
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
|
- name: Test connection to discovered Linode hosts
|
||||||
hosts: discovered_linodes
|
hosts: discovered_linodes
|
||||||
@@ -121,6 +138,45 @@
|
|||||||
register: ping_result
|
register: ping_result
|
||||||
ignore_errors: true
|
ignore_errors: true
|
||||||
|
|
||||||
- name: Display connectivity status
|
- name: Display connectivity status with tag info
|
||||||
ansible.builtin.debug:
|
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
|
||||||
|
@@ -94,6 +94,7 @@ class LinodeInventory:
|
|||||||
inventory['ungrouped']['hosts'].append(hostname)
|
inventory['ungrouped']['hosts'].append(hostname)
|
||||||
|
|
||||||
# Host variables
|
# Host variables
|
||||||
|
linode_tags = instance.get('tags', [])
|
||||||
inventory['_meta']['hostvars'][hostname] = {
|
inventory['_meta']['hostvars'][hostname] = {
|
||||||
'ansible_host': primary_ip,
|
'ansible_host': primary_ip,
|
||||||
'linode_id': instance['id'],
|
'linode_id': instance['id'],
|
||||||
@@ -103,14 +104,22 @@ class LinodeInventory:
|
|||||||
'linode_status': instance['status'],
|
'linode_status': instance['status'],
|
||||||
'linode_ipv4': instance.get('ipv4', []),
|
'linode_ipv4': instance.get('ipv4', []),
|
||||||
'linode_ipv6': instance.get('ipv6'),
|
'linode_ipv6': instance.get('ipv6'),
|
||||||
'linode_tags': instance.get('tags', []),
|
'linode_tags': linode_tags,
|
||||||
'linode_specs': instance.get('specs', {}),
|
'linode_specs': instance.get('specs', {}),
|
||||||
'linode_hypervisor': instance.get('hypervisor'),
|
'linode_hypervisor': instance.get('hypervisor'),
|
||||||
'linode_created': instance.get('created'),
|
'linode_created': instance.get('created'),
|
||||||
'linode_updated': instance.get('updated'),
|
'linode_updated': instance.get('updated'),
|
||||||
'linode_group': instance.get('group', ''),
|
'linode_group': instance.get('group', ''),
|
||||||
'linode_image': instance.get('image', {}).get('id', '') if isinstance(instance.get('image'), dict) else '',
|
'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
|
# Group by region
|
||||||
|
Reference in New Issue
Block a user