Resolve custom hosts records on hosts

In order to save quite some time during the role execution, we apply
run_once when defining /etc/hosts content.

While this good for "static" content which is based on the inventory,
resolving openstack_host_custom_hosts_records once may lead to
unexpected behaviour. For instance, if host record depends on some specific
group or other variables defined in host/group variables.

In order to resolve it accordingly, we merge status and custom records together
in blockinfile, which allows to resolve record individually for each host
and not having penalty for most usecases.

Change-Id: I48274de908fc6dc4a2e22a789e8355c7ba263599
(cherry picked from commit 88a9be604b)
This commit is contained in:
Dmitriy Rabotyagov 2024-02-15 12:26:17 +01:00 committed by Dmitriy Rabotyagov
parent 26f13a7a10
commit 5b593b3a4d
1 changed files with 3 additions and 3 deletions

View File

@ -17,7 +17,7 @@
run_once: true
set_fact:
_etc_hosts_content: |-
{% set records = openstack_host_custom_hosts_records %}
{% set records = [] %}
{% set _groups = groups['all'] %}
{% set _ = _groups.remove('localhost') %}
{% for item in _groups %}
@ -42,7 +42,7 @@
- name: Update hosts file
blockinfile:
dest: /etc/hosts
block: "{{ _etc_hosts_content | join('\n') }}"
block: "{{ (_etc_hosts_content + openstack_host_custom_hosts_records) | join('\n') }}"
marker: "### {mark} OPENSTACK-ANSIBLE MANAGED BLOCK ###"
when:
- openstack_host_manage_hosts_file | bool
@ -50,7 +50,7 @@
- name: Update hosts file on deploy host
blockinfile:
dest: /etc/hosts
block: "{{ _etc_hosts_content | join('\n') }}"
block: "{{ (_etc_hosts_content + openstack_host_custom_hosts_records) | join('\n') }}"
marker: "### {mark} OPENSTACK-ANSIBLE {{ lookup('env', 'OSA_CONFIG_DIR') }} MANAGED BLOCK ###"
run_once: True
delegate_to: localhost