From f12d5b01c70a50762c501799d5c6bff20ff90f46 Mon Sep 17 00:00:00 2001 From: Ryan Groten Date: Fri, 18 Nov 2016 06:41:38 -0700 Subject: [PATCH] FreeIPA: Add support for nested hostgroups in FreeIPA (#14695) --- contrib/inventory/freeipa.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/contrib/inventory/freeipa.py b/contrib/inventory/freeipa.py index a2632621ca9..377326c7101 100755 --- a/contrib/inventory/freeipa.py +++ b/contrib/inventory/freeipa.py @@ -23,7 +23,7 @@ def initialize(): def list_groups(api): ''' - This function returns a list of all host groups. This function requires + This function prints a list of all host groups. This function requires one argument, the FreeIPA/IPA API object. ''' @@ -34,10 +34,16 @@ def list_groups(api): result = api.Command.hostgroup_find()['result'] for hostgroup in result: - inventory[hostgroup['cn'][0]] = { 'hosts': [host for host in hostgroup['member_host']]} + # Get direct and indirect members (nested hostgroups) of hostgroup + members = [] + if 'member_host' in hostgroup: + members = [host for host in hostgroup['member_host']] + if 'memberindirect_host' in hostgroup: + members += (host for host in hostgroup['memberindirect_host']) + inventory[hostgroup['cn'][0]] = {'hosts': [host for host in members]} - for host in hostgroup['member_host']: - hostvars[host] = {} + for member in members: + hostvars[member] = {} inventory['_meta'] = {'hostvars': hostvars} inv_string = json.dumps(inventory, indent=1, sort_keys=True)