Files
openstack-ansible-openstack…/tasks/openstack_update_hosts_file.yml
Dmitriy Rabotyagov 88a9be604b 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
2024-02-15 12:26:17 +01:00

60 lines
2.4 KiB
YAML

---
# Copyright 2014, Rackspace US, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
- name: Generate hosts file records
run_once: true
set_fact:
_etc_hosts_content: |-
{% set records = [] %}
{% set _groups = groups['all'] %}
{% set _ = _groups.remove('localhost') %}
{% for item in _groups %}
{% set record = [] %}
{% set _target_rfc_name = item|replace('_', '-') %}
{% set _ans_hostname = hostvars[item]['ansible_facts']['hostname'] | default(_target_rfc_name) %}
{% set _ = record.append(hostvars[item]['ansible_host'] | default('127.0.0.1')) %}
{% set _ = record.append(_ans_hostname ~ '.' ~ openstack_domain) %}
{% set _ = record.append(_target_rfc_name) %}
{% if (_ans_hostname != _target_rfc_name) and (_target_rfc_name != item) %}
{% set _ = record.append(item) %}
{% set _ = record.append(_ans_hostname) %}
{% elif (_ans_hostname != _target_rfc_name) and (_target_rfc_name == item) %}
{% set _ = record.append(_ans_hostname) %}
{% elif (_ans_hostname == _target_rfc_name) and (_target_rfc_name != item) %}
{% set _ = record.append(item) %}
{% endif %}
{% set _ = records.append(record | join(' ')) %}
{% endfor %}
{{ records }}
- name: Update hosts file
blockinfile:
dest: /etc/hosts
block: "{{ (_etc_hosts_content + openstack_host_custom_hosts_records) | join('\n') }}"
marker: "### {mark} OPENSTACK-ANSIBLE MANAGED BLOCK ###"
when:
- openstack_host_manage_hosts_file | bool
- name: Update hosts file on deploy host
blockinfile:
dest: /etc/hosts
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
become: True
when:
- openstack_host_manage_deploy_hosts_file | bool