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
This commit is contained in:
James Slagle 2018-04-27 10:39:58 -04:00
parent 79af8c05ba
commit 480aeadc1c
2 changed files with 30 additions and 71 deletions

View File

@ -108,6 +108,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):
@ -154,10 +155,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',
'auth_url': self.auth_url,
'cacert': self.cacert,
@ -228,16 +228,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
@ -262,8 +272,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',
'auth_url': 'xyz://keystone.local',
'cacert': 'acacert',
'os_auth_token': 'atoken',
@ -284,42 +258,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'}},
@ -329,10 +278,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',
'auth_url': 'xyz://keystone.local',
'cacert': 'acacert',
'os_auth_token':
@ -409,7 +357,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': {}},
@ -427,9 +374,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',
'auth_url': 'xyz://keystone.local',
'cacert': 'acacert',
'os_auth_token': 'atoken',