Files
openstack-ansible-openstack…/tasks/openstack_update_hosts_file.yml
Dmitriy Rabotyagov 96f2065e5d Use management_address instead of ansible_host for hosts files
In case where SSH address != management address current behavior of
hosts generation, is that a record pointing to the node SSH address
will be produced for metal hosts, as  ansible_host is set to the SSH
address.

This may result in an unexpected behavior, like live migrations
will be executed over the wrong network, as well as communication
can be simply firewalled.

Using `management_address` by default will ensure that hosts records
will be created to the internal network which is "designed" to
handle all internal communications.

Depends-On: https://review.opendev.org/c/openstack/openstack-ansible-lxc_hosts/+/970366
Change-Id: I8a80d444072817af7a1d6f90c7ba2871e41c2dfd
Signed-off-by: Dmitriy Rabotyagov <dmitriy.rabotyagov@cleura.com>
2025-12-10 18:33:16 +00:00

60 lines
2.5 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
ansible.builtin.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]['management_address'] | default(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
ansible.builtin.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
ansible.builtin.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