From adde03e6f617b94af12d485c91e9e5e909cfe12f Mon Sep 17 00:00:00 2001 From: Will Szumski Date: Tue, 14 Apr 2026 12:21:57 +0100 Subject: [PATCH] Extend etc-hosts support to all host groups Change-Id: I0dcd1f2c9a6e6e2b84cd602a476444695ed9c33c Signed-off-by: Will Szumski --- ansible/control-host-configure.yml | 1 + ansible/etc-hosts.yml | 9 +----- ansible/infra-vm-host-configure.yml | 1 + .../inventory/group_vars/overcloud/etc-hosts | 6 ++++ ansible/roles/etc-hosts/defaults/main.yml | 5 ++- ansible/roles/etc-hosts/tasks/etc-hosts.yml | 31 ++++++++++++++++--- ansible/seed-host-configure.yml | 1 + ansible/seed-hypervisor-host-configure.yml | 1 + 8 files changed, 41 insertions(+), 14 deletions(-) create mode 100644 ansible/inventory/group_vars/overcloud/etc-hosts diff --git a/ansible/control-host-configure.yml b/ansible/control-host-configure.yml index 6954d078e..b1e52464d 100644 --- a/ansible/control-host-configure.yml +++ b/ansible/control-host-configure.yml @@ -15,6 +15,7 @@ - import_playbook: "network.yml" - import_playbook: "firewall.yml" - import_playbook: "fail2ban.yml" +- import_playbook: "etc-hosts.yml" - import_playbook: "tuned.yml" - import_playbook: "sysctl.yml" - import_playbook: "time.yml" diff --git a/ansible/etc-hosts.yml b/ansible/etc-hosts.yml index 941e6c472..826d703f4 100644 --- a/ansible/etc-hosts.yml +++ b/ansible/etc-hosts.yml @@ -1,6 +1,6 @@ --- - name: Ensure /etc/hosts is configured - hosts: overcloud + hosts: seed-hypervisor:seed:overcloud:infra-vms:ansible-control max_fail_percentage: >- {{ etc_hosts_max_fail_percentage | default(host_configure_max_fail_percentage) | @@ -9,12 +9,5 @@ tags: - etc-hosts tasks: - # NOTE(mgoddard): Need to ensure that all hosts have facts available. - - import_role: - name: gather-facts-delegated - tags: - - gather-facts-delegated - when: etc_hosts_gather_facts | default(true) - - import_role: name: etc-hosts diff --git a/ansible/infra-vm-host-configure.yml b/ansible/infra-vm-host-configure.yml index cba2d997b..80de651da 100644 --- a/ansible/infra-vm-host-configure.yml +++ b/ansible/infra-vm-host-configure.yml @@ -15,6 +15,7 @@ - import_playbook: "network.yml" - import_playbook: "firewall.yml" - import_playbook: "fail2ban.yml" +- import_playbook: "etc-hosts.yml" - import_playbook: "tuned.yml" - import_playbook: "sysctl.yml" - import_playbook: "disable-glean.yml" diff --git a/ansible/inventory/group_vars/overcloud/etc-hosts b/ansible/inventory/group_vars/overcloud/etc-hosts new file mode 100644 index 000000000..29a71486b --- /dev/null +++ b/ansible/inventory/group_vars/overcloud/etc-hosts @@ -0,0 +1,6 @@ +--- +# Which hosts to add to /etc/hosts +etc_hosts_hosts: "{{ groups['overcloud'] }}" + +# Which network to use for the IP address in /etc/hosts +etc_hosts_network: "{{ internal_net_name }}" diff --git a/ansible/roles/etc-hosts/defaults/main.yml b/ansible/roles/etc-hosts/defaults/main.yml index 3d880182b..063efb61e 100644 --- a/ansible/roles/etc-hosts/defaults/main.yml +++ b/ansible/roles/etc-hosts/defaults/main.yml @@ -3,7 +3,10 @@ customize_etc_hosts: true # List of hosts to add to /etc/hosts. -etc_hosts_hosts: "{{ groups['overcloud'] }}" +etc_hosts_hosts: [] + +# Which network to use for host entries in /etc/hosts +etc_hosts_network: "{{ undef() }}" # Dictionary of custom /etc/hosts entries. # Each key is added as a hostname, diff --git a/ansible/roles/etc-hosts/tasks/etc-hosts.yml b/ansible/roles/etc-hosts/tasks/etc-hosts.yml index 59680a226..26ecd81d2 100644 --- a/ansible/roles/etc-hosts/tasks/etc-hosts.yml +++ b/ansible/roles/etc-hosts/tasks/etc-hosts.yml @@ -1,4 +1,15 @@ --- +# NOTE(mgoddard): Need to ensure that all hosts have facts available. +- include_role: + name: gather-facts-delegated + tags: + - gather-facts-delegated + vars: + gather_facts_delegated_limit_hosts: "{{ etc_hosts_hosts }}" + when: + - etc_hosts_gather_facts | default(true) + - etc_hosts_hosts | length > 0 + - name: Ensure localhost in /etc/hosts lineinfile: dest: /etc/hosts @@ -26,18 +37,28 @@ marker: "# {mark} ANSIBLE GENERATED HOSTS" block: | {% for host in etc_hosts_hosts %} - {% if hostvars[host].internal_net_name in hostvars[host].network_interfaces %} + {% if hostvars[host]['etc_hosts_network'] in hostvars[host].network_interfaces %} {% set hostnames = [hostvars[host].ansible_facts.nodename, hostvars[host].ansible_facts.hostname] %} - {{ hostvars[host].internal_net_name | net_ip(inventory_hostname=host) }} {{ hostnames | unique | join(' ') }} + {{ hostvars[host]['etc_hosts_network'] | net_ip(inventory_hostname=host) }} {{ hostnames | unique | join(' ') }} {% endif %} {% endfor %} + become: True + when: + - etc_hosts_hosts | length > 0 + - etc_hosts_network | mandatory is truthy + # Skip hosts that do not have a valid internal network interface. + - etc_hosts_network in network_interfaces + +- name: Add custom entries to /etc/hosts + blockinfile: + dest: /etc/hosts + marker: "# {mark} ANSIBLE GENERATED CUSTOM HOSTS" + block: | {% for item in custom_etc_hosts_entries | dict2items %} {{ item.value }} {{ item.key }} {% endfor %} become: True - when: - # Skip hosts that do not have a valid internal network interface. - - internal_net_name in network_interfaces + when: custom_etc_hosts_entries | length > 0 # NOTE(osmanlicilegi): The distribution might come with cloud-init installed, and manage_etc_hosts # configuration enabled. If so, it will override the file /etc/hosts from cloud-init templates at diff --git a/ansible/seed-host-configure.yml b/ansible/seed-host-configure.yml index bc899f361..bdaef4754 100644 --- a/ansible/seed-host-configure.yml +++ b/ansible/seed-host-configure.yml @@ -15,6 +15,7 @@ - import_playbook: "network.yml" - import_playbook: "firewall.yml" - import_playbook: "fail2ban.yml" +- import_playbook: "etc-hosts.yml" - import_playbook: "tuned.yml" - import_playbook: "sysctl.yml" - import_playbook: "ip-routing.yml" diff --git a/ansible/seed-hypervisor-host-configure.yml b/ansible/seed-hypervisor-host-configure.yml index 79fe3a6a0..9b7f55092 100644 --- a/ansible/seed-hypervisor-host-configure.yml +++ b/ansible/seed-hypervisor-host-configure.yml @@ -15,6 +15,7 @@ - import_playbook: "network.yml" - import_playbook: "firewall.yml" - import_playbook: "fail2ban.yml" +- import_playbook: "etc-hosts.yml" - import_playbook: "tuned.yml" - import_playbook: "sysctl.yml" - import_playbook: "ip-routing.yml"