|
1 | 1 | --- |
| 2 | +# NOTE(mgoddard): Need to ensure that all hosts have facts available. |
| 3 | +- include_role: |
| 4 | + name: gather-facts-delegated |
| 5 | + tags: |
| 6 | + - gather-facts-delegated |
| 7 | + vars: |
| 8 | + gather_facts_delegated_limit_hosts: "{{ etc_hosts_hosts }}" |
| 9 | + when: |
| 10 | + - etc_hosts_gather_facts | default(true) |
| 11 | + - etc_hosts_hosts | length > 0 |
| 12 | + |
2 | 13 | - name: Ensure localhost in /etc/hosts |
3 | 14 | lineinfile: |
4 | 15 | dest: /etc/hosts |
|
26 | 37 | marker: "# {mark} ANSIBLE GENERATED HOSTS" |
27 | 38 | block: | |
28 | 39 | {% for host in etc_hosts_hosts %} |
29 | | - {% if hostvars[host].internal_net_name in hostvars[host].network_interfaces %} |
| 40 | + {% if hostvars[host]['etc_hosts_network'] in hostvars[host].network_interfaces %} |
30 | 41 | {% set hostnames = [hostvars[host].ansible_facts.nodename, hostvars[host].ansible_facts.hostname] %} |
31 | | - {{ hostvars[host].internal_net_name | net_ip(inventory_hostname=host) }} {{ hostnames | unique | join(' ') }} |
| 42 | + {{ hostvars[host]['etc_hosts_network'] | net_ip(inventory_hostname=host) }} {{ hostnames | unique | join(' ') }} |
32 | 43 | {% endif %} |
33 | 44 | {% endfor %} |
| 45 | + become: True |
| 46 | + when: |
| 47 | + - etc_hosts_hosts | length > 0 |
| 48 | + - etc_hosts_network | mandatory is truthy |
| 49 | + # Skip hosts that do not have a valid internal network interface. |
| 50 | + - etc_hosts_network in network_interfaces |
| 51 | + |
| 52 | +- name: Add custom entries to /etc/hosts |
| 53 | + blockinfile: |
| 54 | + dest: /etc/hosts |
| 55 | + marker: "# {mark} ANSIBLE GENERATED CUSTOM HOSTS" |
| 56 | + block: | |
34 | 57 | {% for item in custom_etc_hosts_entries | dict2items %} |
35 | 58 | {{ item.value }} {{ item.key }} |
36 | 59 | {% endfor %} |
37 | 60 | become: True |
38 | | - when: |
39 | | - # Skip hosts that do not have a valid internal network interface. |
40 | | - - internal_net_name in network_interfaces |
| 61 | + when: custom_etc_hosts_entries | length > 0 |
41 | 62 |
|
42 | 63 | # NOTE(osmanlicilegi): The distribution might come with cloud-init installed, and manage_etc_hosts |
43 | 64 | # configuration enabled. If so, it will override the file /etc/hosts from cloud-init templates at |
|
0 commit comments