From ccef8ebb234f07df1d72755bca9213b778efa6a0 Mon Sep 17 00:00:00 2001 From: Murat Aybars <39916128+aybarsm@users.noreply.github.com> Date: Fri, 12 Jul 2024 13:24:29 +0100 Subject: [PATCH] Role: new:: hosts (completed & tested) --- roles/hosts/defaults/main.yml | 17 +++++++++++++++++ roles/hosts/tasks/main.yml | 11 +++++++++++ roles/hosts/templates/etc/hosts.j2 | 15 +++++++++++++++ roles/hosts/vars/main.yml | 24 ++++++++++++++++++++++++ 4 files changed, 67 insertions(+) create mode 100644 roles/hosts/defaults/main.yml create mode 100644 roles/hosts/tasks/main.yml create mode 100644 roles/hosts/templates/etc/hosts.j2 create mode 100644 roles/hosts/vars/main.yml diff --git a/roles/hosts/defaults/main.yml b/roles/hosts/defaults/main.yml new file mode 100644 index 0000000..e6ff03b --- /dev/null +++ b/roles/hosts/defaults/main.yml @@ -0,0 +1,17 @@ +hosts__role_enabled: false + +hosts__dir: /etc +hosts__file: "{{ hosts__dir }}/hosts" +hosts__template: etc/hosts.j2 +hosts__backup: true + +# If enabled, the ansible inventory will be collected automatically +# and will be appended to the list of hosts, when ansible_host (ip address) is defined. +# ip: ansible_host, hostname: inventory_hostname_short, fqdn: inventory_hostname +hosts__auto_discovery: false +# Consult https://docs.ansible.com/ansible/latest/inventory_guide/intro_patterns.html for more information +hosts__auto_discovery_inventories: 'webservers:&atlanta' + +hosts__default: [] +hosts__group: [] +hosts__host: [] \ No newline at end of file diff --git a/roles/hosts/tasks/main.yml b/roles/hosts/tasks/main.yml new file mode 100644 index 0000000..6a2b787 --- /dev/null +++ b/roles/hosts/tasks/main.yml @@ -0,0 +1,11 @@ +--- +- name: Apply hosts configuration + become: true + ansible.builtin.template: + src: "{{ hosts__template }}" + dest: "{{ hosts__file }}" + backup: "{{ hosts__backup | default(omit) | bool }}" + when: + - hosts__role_enabled | default(false) | bool + - (hosts__all_ipv4 | default([]) | length > 0) or (hosts__all_ipv6 | default([]) | length > 0) + register: hosts__apply diff --git a/roles/hosts/templates/etc/hosts.j2 b/roles/hosts/templates/etc/hosts.j2 new file mode 100644 index 0000000..f7b03ec --- /dev/null +++ b/roles/hosts/templates/etc/hosts.j2 @@ -0,0 +1,15 @@ +127.0.0.1 localhost.localdomain localhost +{% for ipv4_host in hosts__all_ipv4 %} +{{ ipv4_host.ip }} {{ ipv4_host.fqdn }} {{ ipv4_host.hostname }} +{% endfor %} + +# The following lines are desirable for IPv6 capable hosts +::1 ip6-localhost ip6-loopback +fe00::0 ip6-localnet +ff00::0 ip6-mcastprefix +ff02::1 ip6-allnodes +ff02::2 ip6-allrouters +ff02::3 ip6-allhosts +{% for ipv6_host in hosts__all_ipv6 %} +{{ ipv6_host.ip }} {{ ipv6_host.fqdn }} {{ ipv6_host.hostname }} +{% endfor %} \ No newline at end of file diff --git a/roles/hosts/vars/main.yml b/roles/hosts/vars/main.yml new file mode 100644 index 0000000..a8e14e4 --- /dev/null +++ b/roles/hosts/vars/main.yml @@ -0,0 +1,24 @@ + +__hosts__auto_discovered: >- + {%- if (hosts__auto_discovery | default(false) | bool) and hosts__auto_discovery_inventories is defined and hosts__auto_discovery_inventories | length > 0 -%} + {%- set inventory_lookup = lookup('ansible.builtin.inventory_hostnames', hosts__auto_discovery_inventories) -%} + {%- if inventory_lookup | length > 0 -%} + {%- set inventory_hosts = inventory_lookup | split(',') -%} + {%- set discovered_hosts = dict(hostvars) | aybarsm.helper.only_with(inventory_hosts) | dict2items | selectattr('value.ansible_host', 'defined') -%} + {%- set ips = discovered_hosts | map(attribute='value.ansible_host') -%} + {%- set hostnames = discovered_hosts | map(attribute='value.inventory_hostname_short') -%} + {%- set fqdns = discovered_hosts | map(attribute='value.inventory_hostname') -%} + {%- set auto_discovered = {'ip': ips, 'hostname': hostnames, 'fqdn': fqdns} | aybarsm.helper.to_list_of_dicts -%} + {%- endif -%} + {%- else -%} + {%- set auto_discovered = [] -%} + {%- endif -%} + {{ auto_discovered }} + +hosts__all: "{{ (hosts__host + hosts__group + hosts__default + __hosts__auto_discovered) | + aybarsm.helper.replace_aliases({'fqdn': ['hostname']}) | + aybarsm.helper.selectattr([{'when': [['ip', 'defined'], ['hostname', 'defined'], ['fqdn', 'defined']]}]) | + aybarsm.helper.unique_recursive(['ip', 'hostname', 'fqdn']) | default([]) }}" + +hosts__all_ipv4: "{{ hosts__all | selectattr('ip', 'ansible.utils.ipv4') }}" +hosts__all_ipv6: "{{ hosts__all | selectattr('ip', 'ansible.utils.ipv6') }}" \ No newline at end of file