From 37899026bf7ec6ae70b01571cd0fb2e0173f8f9b Mon Sep 17 00:00:00 2001
From: Pierre Riteau <pierre@stackhpc.com>
Date: Wed, 22 May 2019 12:27:37 +0100
Subject: [PATCH] Add ansible_nodename (system hostname) to /etc/hosts

Kolla-Ansible populates /etc/hosts with overcloud hosts using their API
interface IP address. When configured correctly, this allows Nova to use
the API interface for live migration of instances between compute hosts.

The hostname used is from the `ansible_hostname` variable, which is a
short hostname generated by Ansible using the first dot as a delimiter.
However, Nova defaults to use the result of socket.gethostname() to
register nova-compute services.

In deployments where hostnames are set to FQDNs, for example when using
FreeIPA, nova-compute would try to reach the other compute node using
its FQDN (as registered in the Nova database), which was absent from
/etc/hosts. This can result in failures to live migrate instances if
DNS entries don't match.

This commit populates /etc/hosts with `ansible_nodename` (hostname as
reported by the system) in addition to `ansible_hostname`, if they are
different.

Change-Id: Id058aa1db8d60c979680e6a41f7f3e1c39f98235
Closes-Bug: #1830023
---
 ansible/roles/baremetal/tasks/pre-install.yml              | 3 ++-
 .../notes/add-nodename-to-etc-hosts-6360acc642ee3d49.yaml  | 7 +++++++
 2 files changed, 9 insertions(+), 1 deletion(-)
 create mode 100644 releasenotes/notes/add-nodename-to-etc-hosts-6360acc642ee3d49.yaml

diff --git a/ansible/roles/baremetal/tasks/pre-install.yml b/ansible/roles/baremetal/tasks/pre-install.yml
index f67300ed67..d24f1e01a4 100644
--- a/ansible/roles/baremetal/tasks/pre-install.yml
+++ b/ansible/roles/baremetal/tasks/pre-install.yml
@@ -24,7 +24,8 @@
         {% for host in groups['baremetal'] %}
         {% set api_interface = hostvars[host]['api_interface'] %}
         {% if host not in groups['bifrost'] or 'ansible_' + api_interface in hostvars[host] %}
-        {{ hostvars[host]['ansible_' + api_interface]['ipv4']['address'] }} {{ hostvars[host]['ansible_hostname'] }}
+        {% set hostnames = [hostvars[host]['ansible_nodename'], hostvars[host]['ansible_hostname']] %}
+        {{ hostvars[host]['ansible_' + api_interface]['ipv4']['address'] }} {{ hostnames | unique | join(' ') }}
         {% endif %}
         {% endfor %}
   become: True
diff --git a/releasenotes/notes/add-nodename-to-etc-hosts-6360acc642ee3d49.yaml b/releasenotes/notes/add-nodename-to-etc-hosts-6360acc642ee3d49.yaml
new file mode 100644
index 0000000000..84998c1c8b
--- /dev/null
+++ b/releasenotes/notes/add-nodename-to-etc-hosts-6360acc642ee3d49.yaml
@@ -0,0 +1,7 @@
+---
+fixes:
+  - |
+    Adds system hostnames to ``/etc/hosts``, if different from short hostnames.
+    This can fix live migration of Nova instances in some contexts. See `bug
+    1830023 <https://bugs.launchpad.net/kolla-ansible/+bug/1830023>`__ for
+    details.