Skip to content

Commit

Permalink
Role: new:: hosts (completed & tested)
Browse files Browse the repository at this point in the history
  • Loading branch information
aybarsm committed Jul 12, 2024
1 parent d279b80 commit ccef8eb
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 0 deletions.
17 changes: 17 additions & 0 deletions roles/hosts/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -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: []
11 changes: 11 additions & 0 deletions roles/hosts/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -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
15 changes: 15 additions & 0 deletions roles/hosts/templates/etc/hosts.j2
Original file line number Diff line number Diff line change
@@ -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 %}
24 changes: 24 additions & 0 deletions roles/hosts/vars/main.yml
Original file line number Diff line number Diff line change
@@ -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') }}"

0 comments on commit ccef8eb

Please sign in to comment.