Fix dynamic inventory

Commit If834e5e60f79a7418cbc1441d2aaf85507ac7bd2 broke support for the
dynamic inventory since Ansible requires different formats for the hosts
value in groups.

This patch corrects the issue and restores the ability to use
tripleo-ansible-inventory as either a static or dynamic inventory
source.

Change-Id: I035bb878508ceab06279cbaf07969aafeb66b9e5
Story: 2001929
(cherry picked from commit 480aeadc1c)
This commit is contained in:
James Slagle 2018-04-27 10:39:58 -04:00 committed by Emilien Macchi
parent 7901fb43cb
commit 18324fb412
2 changed files with 30 additions and 71 deletions

View File

@ -106,6 +106,7 @@ class TripleoInventory(object):
self.ansible_ssh_user = ansible_ssh_user
self.plan_name = plan_name
self.stack_outputs = StackOutputs(self.plan_name, self.hclient)
self.hostvars = {}
@staticmethod
def get_roles_by_service(enabled_services):
@ -152,10 +153,9 @@ class TripleoInventory(object):
def list(self):
ret = OrderedDict({
'Undercloud': {
'hosts': {
'undercloud': {
'ansible_host': 'localhost'}},
'hosts': self._hosts(['undercloud']),
'vars': {
'ansible_host': 'localhost',
'ansible_connection': 'local',
# see https://github.com/ansible/ansible/issues/41808
'ansible_remote_tmp': '/tmp/ansible-${USER}',
@ -229,16 +229,26 @@ class TripleoInventory(object):
networks.update(hosts[name]['enabled_networks'])
children.append(role)
if self.hosts_format_dict:
hosts_format = hosts
else:
hosts_format = [h for h in hosts.keys()]
hosts_format.sort()
ret[role] = {
'hosts': hosts,
'hosts': hosts_format,
'vars': {
'ansible_ssh_user': self.ansible_ssh_user,
'bootstrap_server_id': role_node_id_map.get(
'bootstrap_server_id'),
'role_name': role,
}
}
self.hostvars.update(hosts)
if children:
vip_map = self.stack_outputs.get('VipMap', {})
vips = {(vip_name + "_vip"): vip
@ -263,8 +273,10 @@ class TripleoInventory(object):
}
}
# Prevent Ansible from repeatedly calling us to get empty host details
ret['_meta'] = {'hostvars': {}}
if not self.hosts_format_dict:
# Prevent Ansible from repeatedly calling us to get empty host
# details
ret['_meta'] = {'hostvars': self.hostvars}
return ret

View File

@ -189,42 +189,17 @@ class TestInventory(base.TestCase):
ansible_ssh_user = 'heat-admin'
expected = {
'Compute': {
'hosts': {
'cp-0': {
'ansible_host': 'y.y.y.1',
'ctlplane_ip': 'y.y.y.1',
'deploy_server_id': 'd',
'enabled_networks': ['ctlplane']}},
'hosts': ['cp-0'],
'vars': {'ansible_ssh_user': ansible_ssh_user,
'bootstrap_server_id': 'a',
'role_name': 'Compute'}},
'Controller': {
'hosts': {
'c-0': {
'ansible_host': 'x.x.x.1',
'ctlplane_ip': 'x.x.x.1',
'deploy_server_id': 'a',
'enabled_networks': ['ctlplane']},
'c-1': {
'ansible_host': 'x.x.x.2',
'ctlplane_ip': 'x.x.x.2',
'deploy_server_id': 'b',
'enabled_networks': ['ctlplane']},
'c-2': {
'ansible_host': 'x.x.x.3',
'ctlplane_ip': 'x.x.x.3',
'deploy_server_id': 'c',
'enabled_networks': ['ctlplane']}},
'hosts': ['c-0', 'c-1', 'c-2'],
'vars': {'ansible_ssh_user': ansible_ssh_user,
'bootstrap_server_id': 'a',
'role_name': 'Controller'}},
'CustomRole': {
'hosts': {
'cs-0': {
'ansible_host': 'z.z.z.1',
'ctlplane_ip': 'z.z.z.1',
'deploy_server_id': 'e',
'enabled_networks': ['ctlplane']}},
'hosts': ['cs-0'],
'vars': {'ansible_ssh_user': ansible_ssh_user,
'bootstrap_server_id': 'a',
'role_name': 'CustomRole'}},
@ -235,10 +210,9 @@ class TestInventory(base.TestCase):
'ctlplane_vip': 'x.x.x.4',
'redis_vip': 'x.x.x.6'}},
'Undercloud': {
'hosts': {
'undercloud': {
'ansible_host': 'localhost'}},
'hosts': ['undercloud'],
'vars': {'ansible_connection': 'local',
'ansible_host': 'localhost',
'ansible_remote_tmp': '/tmp/ansible-${USER}',
'auth_url': 'xyz://keystone.local',
'cacert': 'acacert',
@ -285,42 +259,17 @@ class TestInventory(base.TestCase):
expected = {
'Compute': {
'hosts': {
'cp-0': {
'ansible_host': 'y.y.y.1',
'ctlplane_ip': 'y.y.y.1',
'deploy_server_id': 'd',
'enabled_networks': ['ctlplane']}},
'hosts': ['cp-0'],
'vars': {'ansible_ssh_user': ansible_ssh_user,
'bootstrap_server_id': 'a',
'role_name': 'Compute'}},
'Controller': {
'hosts': {
'c-0': {
'ansible_host': 'x.x.x.1',
'ctlplane_ip': 'x.x.x.1',
'deploy_server_id': 'a',
'enabled_networks': ['ctlplane']},
'c-1': {
'ansible_host': 'x.x.x.2',
'ctlplane_ip': 'x.x.x.2',
'deploy_server_id': 'b',
'enabled_networks': ['ctlplane']},
'c-2': {
'ansible_host': 'x.x.x.3',
'ctlplane_ip': 'x.x.x.3',
'deploy_server_id': 'c',
'enabled_networks': ['ctlplane']}},
'hosts': ['c-0', 'c-1', 'c-2'],
'vars': {'ansible_ssh_user': ansible_ssh_user,
'bootstrap_server_id': 'a',
'role_name': 'Controller'}},
'CustomRole': {
'hosts': {
'cs-0': {
'ansible_host': 'z.z.z.1',
'ctlplane_ip': 'z.z.z.1',
'deploy_server_id': 'e',
'enabled_networks': ['ctlplane']}},
'hosts': ['cs-0'],
'vars': {'ansible_ssh_user': ansible_ssh_user,
'bootstrap_server_id': 'a',
'role_name': 'CustomRole'}},
@ -330,10 +279,9 @@ class TestInventory(base.TestCase):
'ctlplane_vip': 'x.x.x.4',
'redis_vip': 'x.x.x.6'}},
'Undercloud': {
'hosts': {
'undercloud': {
'ansible_host': 'localhost'}},
'hosts': ['undercloud'],
'vars': {'ansible_connection': 'local',
'ansible_host': 'localhost',
'ansible_remote_tmp': '/tmp/ansible-${USER}',
'auth_url': 'xyz://keystone.local',
'cacert': 'acacert',
@ -411,7 +359,6 @@ class TestInventory(base.TestCase):
'vars': {'ansible_ssh_user': ansible_ssh_user,
'bootstrap_server_id': 'a',
'role_name': 'CustomRole'}},
'_meta': {'hostvars': {}},
'overcloud': {'children': {'Compute': {},
'Controller': {},
'CustomRole': {}},
@ -429,9 +376,9 @@ class TestInventory(base.TestCase):
'vars': {'ansible_ssh_user': 'heat-admin'}},
'sh': {'children': {'CustomRole': {}},
'vars': {'ansible_ssh_user': 'heat-admin'}},
'Undercloud': {'hosts': {'undercloud': {
'ansible_host': 'localhost'}},
'Undercloud': {'hosts': {'undercloud': {}},
'vars': {'ansible_connection': 'local',
'ansible_host': 'localhost',
'ansible_remote_tmp':
'/tmp/ansible-${USER}',
'auth_url': 'xyz://keystone.local',