Skip to content

Commit 04a4188

Browse files
authored
Merge pull request #277 from stackhpc/feat/etc_hosts
Make etc_hosts role more flexible
2 parents 9f4ef8e + bd6526f commit 04a4188

File tree

3 files changed

+17
-6
lines changed

3 files changed

+17
-6
lines changed

ansible/roles/etc_hosts/README.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
# etc_hosts
22

3-
Hosts in the `etc_hosts` groups have `/etc/hosts` created. The generated file defines all hosts in this group using `ansible_host` as the IP address and `inventory_hostname` as the canonical hostname. This may need overriding for multi-homed hosts. See `environments/common/inventory/group_vars/all/cloud_init.yml` for configuration.
3+
Hosts in the `etc_hosts` groups have `/etc/hosts` created with entries of the format `IP_address canonical_hostname [alias]`.
44

5-
# Variables:
5+
By default, an entry is created for each host in this group, using `ansible_host` as the IP_address and `inventory_hostname` as the canonical hostname. This may need overriding for multi-homed hosts or hosts with multiple aliases.
66

7-
- `etc_hosts_template`: Template file to use. Default uses in-role template.
7+
# Variables
8+
9+
- `etc_hosts_template`: Template file to use. Default is the in-role template.
10+
- `etc_hosts_hostvars`: A list of variable names, used (in the order supplied) to create the entry for each host. Default is `['ansible_host', 'inventory_hostname']`
11+
- `etc_hosts_extra_hosts`: String (possibly multi-line) defining additional hosts to add to `/etc/hosts`. Default is empty string.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
11
etc_hosts_template: hosts.j2
2+
etc_hosts_hostvars:
3+
- ansible_host
4+
- inventory_hostname
5+
etc_hosts_extra_hosts: ''
Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
22
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
33

4-
{% for hostname in groups['etc_hosts'] | sort -%}
5-
{{ hostvars[hostname]['ansible_host'] }} {{ hostname }}
6-
{% endfor -%}
4+
{% for inventory_hostname in groups['etc_hosts'] | sort -%}
5+
{{ hostvars[inventory_hostname] | json_query('[' + ( etc_hosts_hostvars | join(', ') ) + ']' ) | join(' ')}}
6+
{% endfor %}
7+
{% if etc_hosts_extra_hosts != '' %}
8+
{{ etc_hosts_extra_hosts }}
9+
{% endif %}

0 commit comments

Comments
 (0)