From 46bd05250d5c6497da35d8a490336a433915358e Mon Sep 17 00:00:00 2001
From: Mark Goddard <mark@stackhpc.com>
Date: Fri, 4 Jun 2021 17:05:24 +0100
Subject: [PATCH] baremetal: fix /etc/hosts generation when api_interface has
 dashes

Interface names with dashes can cause problems in Ansible since dashes
are replaced with underscores when referencing facts. In the baremetal
role we reference the fact for api_interface without replacing dashes
with underscores. This may result in host entries being omitted from
/etc/hosts.

This change fixes the issue.

Change-Id: I667adc7d8a7dbd20dbfa293f389e02355f8275bb
Related-Bug: #1927357
---
 ansible/roles/baremetal/tasks/pre-install.yml             | 4 ++--
 releasenotes/notes/etc-hosts-dashes-37d0dc07c8fc881f.yaml | 7 +++++++
 2 files changed, 9 insertions(+), 2 deletions(-)
 create mode 100644 releasenotes/notes/etc-hosts-dashes-37d0dc07c8fc881f.yaml

diff --git a/ansible/roles/baremetal/tasks/pre-install.yml b/ansible/roles/baremetal/tasks/pre-install.yml
index 4dc3142d3e..6c4d9bfecc 100644
--- a/ansible/roles/baremetal/tasks/pre-install.yml
+++ b/ansible/roles/baremetal/tasks/pre-install.yml
@@ -28,7 +28,7 @@
     marker: "# {mark} ANSIBLE GENERATED HOSTS"
     block: |
         {% for host in groups['baremetal'] %}
-        {% set api_interface = hostvars[host]['api_interface'] %}
+        {% set api_interface = hostvars[host]['api_interface'] | replace('-', '_') %}
         {% if host not in groups['bifrost'] or 'ansible_' + api_interface in hostvars[host] %}
         {% set hostnames = [hostvars[host]['ansible_nodename'], hostvars[host]['ansible_hostname']] %}
         {{ 'api' | kolla_address(host) }} {{ hostnames | unique | join(' ') }}
@@ -39,7 +39,7 @@
     - customize_etc_hosts | bool
     # Skip hosts in the bifrost group that do not have a valid api_interface.
     - inventory_hostname not in groups['bifrost'] or
-      'ansible_' + hostvars[inventory_hostname]['api_interface'] in hostvars[inventory_hostname]
+      'ansible_' + hostvars[inventory_hostname]['api_interface'] | replace('-', '_') in hostvars[inventory_hostname]
 
 # 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/releasenotes/notes/etc-hosts-dashes-37d0dc07c8fc881f.yaml b/releasenotes/notes/etc-hosts-dashes-37d0dc07c8fc881f.yaml
new file mode 100644
index 0000000000..da5dbc9260
--- /dev/null
+++ b/releasenotes/notes/etc-hosts-dashes-37d0dc07c8fc881f.yaml
@@ -0,0 +1,7 @@
+---
+fixes:
+  - |
+    Fixes an issue when generating ``/etc/hosts`` during ``kolla-ansible
+    bootstrap-servers`` when one or more hosts has an ``api_interface`` with
+    dashes (``-``) in its name. `LP#1927357
+    <https://bugs.launchpad.net/kolla-ansible/+bug/1927357>`__
-- 
GitLab