Handle child groups when building ceph spec from inventory

If the inventory contains the following:

  CephStorage:
    children:
      overcloud_CephStorage: {}
  overcloud_CephStorage:
    hosts:
      ceph-0:

and I want to get a list of hosts from the CephStorage group,
it is wrong to return that CephStorage has no hosts. Instead
since all hosts in the overcloud_CephStorage group are children
of the CephStorage group, I should return the hosts of the
child group. I.e. the hosts of CephStorage above are [ceph-0].

Closes-Bug: #1998649
Change-Id: I0405f6729b492af011531642a2ee374bb210fa82
(cherry picked from commit 04c33a4747)
This commit is contained in:
John Fulton 2022-12-02 21:04:32 +00:00
parent 005c4e11e4
commit 37fc1a9d11
1 changed files with 27 additions and 9 deletions

View File

@ -165,24 +165,42 @@ SERVICE_MAP = {
def get_inventory_hosts_to_ips(inventory, roles, fqdn=False):
"""Return a map of hostnames to IP addresses, e.g.
"""Returns map of hostnames to IP addresses for groups in roles list
{'oc0-ceph-0': '192.168.24.13',
'oc0-compute-0': '192.168.24.21',
'oc0-controller-0': '192.168.24.23',
'oc0-controller-1': '192.168.24.15',
'oc0-controller-2': '192.168.24.7'}
Uses ansible inventory as source
Uses the ansible inventory as source. If the inventory has:
CephStorage:
children:
overcloud_CephStorage: {}
overcloud_CephStorage:
hosts:
ceph-0:
ansible_host: 192.168.24.13
Then the hosts of the CephStorage group (from the roles list)
are ['ceph-0'] because overcloud_CephStorage is a child group.
Does not handle if one group has both children and hosts, but only
needs to handle the types of groups generated in tripleo inventory.
"""
hosts_to_ips = {}
for key in inventory:
if key in roles:
for host in inventory[key]['hosts']:
ip = inventory[key]['hosts'][host]['ansible_host']
if fqdn:
hostname = inventory[key]['hosts'][host]['canonical_hostname']
else:
hostname = host
hosts_to_ips[hostname] = ip
if 'children' in inventory[key] and 'hosts' not in inventory[key]:
# e.g. if CephStorage has children (e.g. overcloud_CephStorage)
# then set the key to overcloud_CephStorage so we get its hosts
key = [k for k, v in inventory[key]['children'].items()][0]
if 'hosts' in inventory[key]:
for host in inventory[key]['hosts']:
ip = inventory[key]['hosts'][host]['ansible_host']
if fqdn:
hostname = inventory[key]['hosts'][host]['canonical_hostname']
else:
hostname = host
hosts_to_ips[hostname] = ip
return hosts_to_ips