diff --git a/roles/hostsfile/defaults/main.yaml b/roles/hostsfile/defaults/main.yaml new file mode 100644 index 0000000..eb65313 --- /dev/null +++ b/roles/hostsfile/defaults/main.yaml @@ -0,0 +1,32 @@ +--- +hostsfile_loopback: + 127.0.0.1: + - localhost + - localhost.localdomain + - localhost4 + - localhost4.localdomain4 + ::1: + - localhost + - localhost.localdomain + - localhost6 + - localhost6.localdomain4 + - ip6-localhost + - ip6-loopback + +hostsfile_self: + "{{ ansible_default_ipv4.address }}": + - "{{ ansible_fqdn }}" + "{{ ansible_default_ipv6.address }}": + - "{{ ansible_fqdn }}" + +hostsfile_routers: + ff02::1: + - ip6-allnodes + ff02::2: + - ip6-allrouters + +hostsfile_hosts: {} + +hostsfile_combined: "{{ hostsfile_loopback + | combine(hostsfile_hosts) + | combine(hostsfile_self) }}" diff --git a/roles/hostsfile/tasks/main.yaml b/roles/hostsfile/tasks/main.yaml new file mode 100644 index 0000000..71b95e5 --- /dev/null +++ b/roles/hostsfile/tasks/main.yaml @@ -0,0 +1,8 @@ +--- +- name: configure hosts file + template: + src: hosts.j2 + dest: /etc/hosts + owner: root + group: root + mode: 0644 diff --git a/roles/hostsfile/templates/hosts.j2 b/roles/hostsfile/templates/hosts.j2 new file mode 100644 index 0000000..abbbac8 --- /dev/null +++ b/roles/hostsfile/templates/hosts.j2 @@ -0,0 +1,33 @@ +# {{ ansible_managed }} + +{% macro render(hosts, comment=None) %} +{% if hosts is defined and hosts | length %} +{% if comment is not none %} +# {{ comment }} +{% endif %} +{% for ip, names in hosts.items() | default({}) %} +{{ ip }} {{ names | join(' ') }} +{% endfor %} +{% endif %} +{% endmacro %} + +{{ render(hostsfile_loopback, comment="Loopback") }} +{{ render(hostsfile_routers, comment="Routers") }} + +# Self +{% if 'address' in ansible_default_ipv4 %} +{{ ansible_default_ipv4.address }} {{ inventory_hostname }} {{ inventory_hostname_short }} +{% endif %} +{% if 'address' in ansible_default_ipv6 %} +{{ ansible_default_ipv6.address }} {{ inventory_hostname }} {{ inventory_hostname_short }} +{% endif %} + +# Gateways +{% if 'gateway' in ansible_default_ipv4 %} +{{ ansible_default_ipv4.gateway }} gateway +{% endif %} +{% if 'gateway' in ansible_default_ipv6 %} +{{ ansible_default_ipv6.gateway }} gateway6 +{% endif %} + +{{ render(hostsfile_hosts, comment="Hosts") }}