From f758dfa626424769fd7cefacf9940a27d338693b Mon Sep 17 00:00:00 2001
From: Jakob Meng <code@jakobmeng.de>
Date: Tue, 31 Jan 2023 19:49:35 +0100
Subject: [PATCH] Added workaround for non-determinism in ansible-inventory
 output

YAML output of ansible-inventory does not guarantee which group will
actually populate hosts with all their host vars.

Change-Id: Ia7d46898b8e91bafff05873be2b3c92bc85d83d2
---
 ci/roles/inventory/tasks/main.yml | 25 +++++++++++++++++++------
 1 file changed, 19 insertions(+), 6 deletions(-)

diff --git a/ci/roles/inventory/tasks/main.yml b/ci/roles/inventory/tasks/main.yml
index 3c459563..d7fc44f1 100644
--- a/ci/roles/inventory/tasks/main.yml
+++ b/ci/roles/inventory/tasks/main.yml
@@ -239,12 +239,25 @@
           assert:
             that:
               - inventory.all.children.RegionOne.hosts.keys() | sort == ['ansible_server1', 'ansible_server2'] | sort
-              - inventory.all.children.RegionOne.hosts.ansible_server1.ansible_host == '10.6.6.150'
-              - "'10.7.7.' in inventory.all.children.RegionOne.hosts.ansible_server2.ansible_host"
-              - inventory.all.children.RegionOne.hosts.ansible_server1.ci_compose_id
-                == inventory.all.children.RegionOne.hosts.ansible_server1.openstack.id
-              - inventory.all.children.RegionOne.hosts.ansible_server1.ci_compose_project_id
-                == inventory.all.children.RegionOne.hosts.ansible_server1.openstack.project_id
+              - ansible_server1.ansible_host == '10.6.6.150'
+              - "'10.7.7.' in ansible_server2.ansible_host"
+              - ansible_server1.ci_compose_id == ansible_server1.openstack.id
+              - ansible_server1.ci_compose_project_id == ansible_server1.openstack.project_id
+          vars:
+            ansible_server1: "{{
+                (inventory.all.children.values()
+                 | map(attribute='hosts', default={})
+                 | map(attribute='ansible_server1', default={})
+                 | reject('equalto', {})
+                 | list
+                )[0] }}"
+            ansible_server2: "{{
+                (inventory.all.children.values()
+                 | map(attribute='hosts', default={})
+                 | map(attribute='ansible_server2', default={})
+                 | reject('equalto', {})
+                 | list
+                )[0] }}"
 
         - name: Find Ansible's cache file
           ansible.builtin.find: