From 5be093ac5a7b6fb85a4448cd9d0fd1e94f622061 Mon Sep 17 00:00:00 2001
From: Mark Goddard <mark@stackhpc.com>
Date: Mon, 8 Jul 2019 19:06:43 +0100
Subject: [PATCH] Fix nova deploy with Ansible<2.8

Due to a bug in ansible, kolla-ansible deploy currently fails in nova
with the following error when used with ansible earlier than 2.8:

TASK [nova : Waiting for nova-compute services to register themselves]
*********
task path:
/home/zuul/src/opendev.org/openstack/kolla-ansible/ansible/roles/nova/tasks/discover_computes.yml:30
fatal: [primary]: FAILED! => {
    "failed": true,
    "msg": "The field 'vars' has an invalid value, which
        includes an undefined variable. The error was:
        'nova_compute_services' is undefined\n\nThe error
        appears to have been in
        '/home/zuul/src/opendev.org/openstack/kolla-ansible/ansible/roles/nova/tasks/discover_computes.yml':
        line 30, column 3, but may\nbe elsewhere in the file
        depending on the exact syntax problem.\n\nThe
        offending line appears to be:\n\n\n- name: Waiting
        for nova-compute services to register themselves\n ^
            here\n"
}

Example:
http://logs.openstack.org/00/669700/1/check/kolla-ansible-centos-source/81b65b9/primary/logs/ansible/deploy

This was caused by
https://review.opendev.org/#/q/I2915e2610e5c0b8d67412e7ec77f7575b8fe9921,
which hits upon an ansible bug described here:
https://github.com/markgoddard/ansible-experiments/tree/master/05-referencing-registered-var-do-until.

We can work around this by not using an intermediary variable.

Change-Id: I58f8fd0a6e82cb614e02fef6e5b271af1d1ce9af
Closes-Bug: #1835817
---
 .../roles/nova/tasks/discover_computes.yml    | 25 ++++++++++---------
 1 file changed, 13 insertions(+), 12 deletions(-)

diff --git a/ansible/roles/nova/tasks/discover_computes.yml b/ansible/roles/nova/tasks/discover_computes.yml
index 37d4ed0c97..9dd8dded5a 100644
--- a/ansible/roles/nova/tasks/discover_computes.yml
+++ b/ansible/roles/nova/tasks/discover_computes.yml
@@ -28,17 +28,6 @@
   delegate_to: "{{ groups['nova-api'][0] }}"
 
 - name: Waiting for nova-compute services to register themselves
-  vars:
-    # A list containing the 'Host' field of compute services that have
-    # registered themselves.  Don't exclude compute services that are disabled
-    # since these could have been explicitly disabled by the operator. While we
-    # could exclude services that are down, the nova-manage cell_v2
-    # discover_hosts does not do this so let's not block on it here.
-    found_compute_service_hosts: >-
-      {{ nova_compute_services.stdout |
-         from_json |
-         map(attribute='Host') |
-         list }}
   become: true
   command: >
     docker exec kolla_toolbox openstack
@@ -60,7 +49,19 @@
   delay: 10
   until:
     - nova_compute_services is success
-    - found_compute_service_hosts is superset(expected_compute_service_hosts)
+    # A list containing the 'Host' field of compute services that have
+    # registered themselves.  Don't exclude compute services that are disabled
+    # since these could have been explicitly disabled by the operator. While we
+    # could exclude services that are down, the nova-manage cell_v2
+    # discover_hosts does not do this so let's not block on it here.
+    # NOTE(mgoddard): Cannot factor this out into an intermediary variable
+    # before ansible 2.8, due to
+    # https://bugs.launchpad.net/kolla-ansible/+bug/1835817.
+    - (nova_compute_services.stdout |
+       from_json |
+       map(attribute='Host') |
+       list)
+      is superset(expected_compute_service_hosts)
 
 # TODO(yoctozepto): no need to do --by-service if ironic not used
 - name: Discover nova hosts