2015-01-07 01:29:19 +01:00
|
|
|
#!/usr/bin/env python
|
2014-10-11 03:26:17 +02:00
|
|
|
|
2015-01-20 23:45:41 +01:00
|
|
|
import argparse
|
2014-10-11 03:26:17 +02:00
|
|
|
from ipalib import api
|
2015-01-20 23:45:41 +01:00
|
|
|
import json
|
|
|
|
|
2017-05-30 22:08:09 +02:00
|
|
|
|
2015-01-20 23:45:41 +01:00
|
|
|
def initialize():
|
|
|
|
'''
|
|
|
|
This function initializes the FreeIPA/IPA API. This function requires
|
2017-01-28 00:20:31 +01:00
|
|
|
no arguments. A kerberos key must be present in the users keyring in
|
2015-01-20 23:45:41 +01:00
|
|
|
order for this to work.
|
|
|
|
'''
|
|
|
|
|
|
|
|
api.bootstrap(context='cli')
|
|
|
|
api.finalize()
|
2015-04-09 04:33:38 +02:00
|
|
|
try:
|
|
|
|
api.Backend.rpcclient.connect()
|
|
|
|
except AttributeError:
|
2017-05-30 22:08:09 +02:00
|
|
|
# FreeIPA < 4.0 compatibility
|
2015-04-09 04:33:38 +02:00
|
|
|
api.Backend.xmlclient.connect()
|
2017-01-28 00:45:23 +01:00
|
|
|
|
2015-01-20 23:45:41 +01:00
|
|
|
return api
|
|
|
|
|
2017-05-30 22:08:09 +02:00
|
|
|
|
2015-01-20 23:45:41 +01:00
|
|
|
def list_groups(api):
|
|
|
|
'''
|
2016-11-18 14:41:38 +01:00
|
|
|
This function prints a list of all host groups. This function requires
|
2015-01-20 23:45:41 +01:00
|
|
|
one argument, the FreeIPA/IPA API object.
|
|
|
|
'''
|
|
|
|
|
|
|
|
inventory = {}
|
2017-05-30 22:08:09 +02:00
|
|
|
hostvars = {}
|
|
|
|
meta = {}
|
2015-01-20 23:45:41 +01:00
|
|
|
|
|
|
|
result = api.Command.hostgroup_find()['result']
|
|
|
|
|
|
|
|
for hostgroup in result:
|
2016-11-18 14:41:38 +01:00
|
|
|
# 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:
|
2017-01-28 00:45:23 +01:00
|
|
|
members += (host for host in hostgroup['memberindirect_host'])
|
2016-11-18 14:41:38 +01:00
|
|
|
inventory[hostgroup['cn'][0]] = {'hosts': [host for host in members]}
|
|
|
|
|
|
|
|
for member in members:
|
|
|
|
hostvars[member] = {}
|
2015-01-20 23:45:41 +01:00
|
|
|
|
|
|
|
inventory['_meta'] = {'hostvars': hostvars}
|
|
|
|
inv_string = json.dumps(inventory, indent=1, sort_keys=True)
|
2015-08-28 08:18:13 +02:00
|
|
|
print(inv_string)
|
2017-01-28 00:45:23 +01:00
|
|
|
|
2015-01-20 23:45:41 +01:00
|
|
|
return None
|
|
|
|
|
2017-05-30 22:08:09 +02:00
|
|
|
|
2015-01-20 23:45:41 +01:00
|
|
|
def parse_args():
|
|
|
|
'''
|
|
|
|
This function parses the arguments that were passed in via the command line.
|
|
|
|
This function expects no arguments.
|
|
|
|
'''
|
|
|
|
|
|
|
|
parser = argparse.ArgumentParser(description='Ansible FreeIPA/IPA '
|
|
|
|
'inventory module')
|
|
|
|
group = parser.add_mutually_exclusive_group(required=True)
|
|
|
|
group.add_argument('--list', action='store_true',
|
|
|
|
help='List active servers')
|
|
|
|
group.add_argument('--host', help='List details about the specified host')
|
|
|
|
|
|
|
|
return parser.parse_args()
|
|
|
|
|
2017-05-30 22:08:09 +02:00
|
|
|
|
2015-01-20 23:45:41 +01:00
|
|
|
def print_host(host):
|
|
|
|
'''
|
2017-01-28 00:20:31 +01:00
|
|
|
This function is really a stub, it could return variables to be used in
|
|
|
|
a playbook. However, at this point there are no variables stored in
|
2015-01-20 23:45:41 +01:00
|
|
|
FreeIPA/IPA.
|
|
|
|
|
|
|
|
This function expects one string, this hostname to lookup variables for.
|
|
|
|
'''
|
|
|
|
|
2015-08-28 08:18:13 +02:00
|
|
|
print(json.dumps({}))
|
2015-01-20 23:45:41 +01:00
|
|
|
|
|
|
|
return None
|
|
|
|
|
2017-05-30 22:08:09 +02:00
|
|
|
|
2015-01-20 23:45:41 +01:00
|
|
|
if __name__ == '__main__':
|
|
|
|
args = parse_args()
|
2014-10-11 03:26:17 +02:00
|
|
|
|
2015-01-20 23:45:41 +01:00
|
|
|
if args.host:
|
|
|
|
print_host(args.host)
|
|
|
|
elif args.list:
|
|
|
|
api = initialize()
|
|
|
|
list_groups(api)
|