-
Notifications
You must be signed in to change notification settings - Fork 0
/
ansible-freeipa-inventory.py
executable file
·54 lines (39 loc) · 1.14 KB
/
ansible-freeipa-inventory.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/usr/bin/python
#
# you may want to install (free)ipa-python package
#
# skeleton provided by Rob Crittenden
# fleshed out by Tomasz Torcz <[email protected]>
#
from ipalib import api
from ipalib import errors
import json
import sys
def groups_find(api):
inventory = {}
inventory["ungrouped"] = { "hosts" : [] }
# TODO ask only for fqdn and memberof_hostgroup (attrlist?)
for host in api.Command["host_find"]()["result"]:
host_fqdn = host["fqdn"][0]
try:
for group in host['memberof_hostgroup']:
if not group in inventory:
# first occurence of this group
inventory[group] = { "hosts" : [] }
inventory[group]["hosts"].append(host_fqdn)
except KeyError:
# no groups
inventory["ungrouped"]["hosts"].append(host_fqdn)
print json.dumps(inventory)
def host_find(api, host):
# we have no specific to pass
host_vars = {}
print json.dumps(host_vars)
if __name__ == "__main__":
api.bootstrap(context='iparest')
api.finalize()
api.Backend.rpcclient.connect()
if len(sys.argv) == 2 and (sys.argv[1] == "--list"):
groups_find(api)
elif len(sys.argv) == 3 and (sys.argv[1] == "--host"):
host_find(api, sys.argv[2])