Skip to content
This repository has been archived by the owner on Feb 29, 2024. It is now read-only.

Commit

Permalink
Fix dynamic inventory
Browse files Browse the repository at this point in the history
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 480aead)
  • Loading branch information
slagle authored and EmilienM committed Jul 22, 2018
1 parent 7901fb4 commit 18324fb
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 71 deletions.
24 changes: 18 additions & 6 deletions tripleo_common/inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ def __init__(self, configs=None, session=None, hclient=None,
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):
Expand Down Expand Up @@ -152,10 +153,9 @@ def _hosts(self, alist):
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}',
Expand Down Expand Up @@ -229,16 +229,26 @@ def list(self):
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
Expand All @@ -263,8 +273,10 @@ def list(self):
}
}

# 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

Expand Down
77 changes: 12 additions & 65 deletions tripleo_common/tests/test_inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,42 +189,17 @@ def _inventory_list(self, inventory):
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'}},
Expand All @@ -235,10 +210,9 @@ def _inventory_list(self, inventory):
'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',
Expand Down Expand Up @@ -285,42 +259,17 @@ def _try_alternative_args(self, ansible_ssh_user, session):

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'}},
Expand All @@ -330,10 +279,9 @@ def _try_alternative_args(self, ansible_ssh_user, session):
'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',
Expand Down Expand Up @@ -411,7 +359,6 @@ def _inventory_write_static(self, extra_vars=None):
'vars': {'ansible_ssh_user': ansible_ssh_user,
'bootstrap_server_id': 'a',
'role_name': 'CustomRole'}},
'_meta': {'hostvars': {}},
'overcloud': {'children': {'Compute': {},
'Controller': {},
'CustomRole': {}},
Expand All @@ -429,9 +376,9 @@ def _inventory_write_static(self, extra_vars=None):
'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',
Expand Down

0 comments on commit 18324fb

Please sign in to comment.