Skip to content

Commit edd67e1

Browse files
committed
move /etc/hosts templating from cloud_init to bootstrap.yml
1 parent 08f7903 commit edd67e1

File tree

6 files changed

+22
-31
lines changed

6 files changed

+22
-31
lines changed

ansible/bootstrap.yml

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,8 @@
1818
tags: etc_hosts
1919
become: yes
2020
tasks:
21-
- name: Template /etc/hosts
22-
copy:
23-
content: "{{ etc_hosts_template }}"
24-
dest: /etc/hosts
25-
owner: root
26-
group: root
27-
mode: u=rw,og=r
21+
- import_role:
22+
name: etc_hosts
2823

2924
- hosts: cluster
3025
gather_facts: false

ansible/roles/etc_hosts/README.md

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

3-
This role provides documentation only.
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.
44

5-
Hosts in the `etc_hosts` groups get `/etc/hosts` created via `cloud-init`. 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.
5+
# Variables:
6+
7+
- `etc_hosts_template`: Template file to use. Default uses in-role template.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
etc_hosts_template: hosts.j2
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
- name: Template out /etc/hosts
2+
template:
3+
src: "{{ etc_hosts_template }}"
4+
dest: /etc/hosts
5+
owner: root
6+
group: root
7+
mode: 0644
8+
become: yes
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
2+
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
3+
4+
{% for hostname in groups['etc_hosts'] | sort -%}
5+
{{ hostvars[hostname]['ansible_host'] }} {{ hostname }}
6+
{% endfor -%}
Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,6 @@
11
cloud_init_output_path: "{{ lookup('env', 'APPLIANCES_ENVIRONMENT_ROOT') }}/cloud_init"
22

3-
etc_hosts_template: |
4-
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
5-
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
6-
7-
{% for hostname in groups['etc_hosts'] | sort -%}
8-
{{ hostvars[hostname]['ansible_host'] }} {{ hostname }}
9-
{% endfor -%}
10-
11-
cloud_init_userdata_etchosts:
12-
module: write_files
13-
group: etc_hosts
14-
template: |-
15-
# etc_hosts group:
16-
- path: /etc/hosts
17-
encoding: text/plain
18-
permissions: "0644"
19-
owner: root:root
20-
content: |
21-
{{ etc_hosts_template | indent(width=6, first=false) }}
22-
23-
cloud_init_userdata_templates_default: # provides indirection so environments could copy parts of default without having to rewrite the entire thing
24-
- "{{ cloud_init_userdata_etchosts }}"
3+
cloud_init_userdata_templates_default: [] # provides indirection so environments could copy parts of default without having to rewrite the entire thing
254

265
cloud_init_userdata_templates_extra: []
276
cloud_init_userdata_templates: "{{ cloud_init_userdata_templates_default + cloud_init_userdata_templates_extra }}"

0 commit comments

Comments
 (0)