-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfreeipa-api-inv.py
157 lines (139 loc) · 3.65 KB
/
freeipa-api-inv.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/usr/bin/env python
# This script uses the FreeIPA API to create an Ansible dynamic directory
# This is a shell script version of freeipa-api-inv.py
#
# DEPENDENCIES: before this script will work with AWX or Tower
# the python_freeipa module has to be installed
#
# Add this to your Docker image
# RUN pip install python_freeipa
#
# Set the following variables:
# freeipaserver : the FQDN of the FreeIPA/RHIdM server
# freeipauser : an unprivileged user account for connecting to the API
# freeipapassword : password for freeipauser
# This script uses the FreeIPA API to create an Ansible dynamic directory
from python_freeipa import Client
from argparse import ArgumentParser
import json
import urllib3
from os import environ as env
from sys import exit
# We don't need warnings
urllib3.disable_warnings()
parser = ArgumentParser(description="AWX FreeIPA API dynamic host inventory")
parser.add_argument(
'--list',
default=False,
dest="list",
action="store_true",
help="Produce a JSON consumable grouping of servers for Ansible"
)
parser.add_argument(
'--host',
default=None,
dest="host",
help="Generate additional host specific details for given host for Ansible"
)
parser.add_argument(
'-u',
'--user',
default=None,
dest="user",
help="username to log into FreeIPA API"
)
parser.add_argument(
'-w',
'--password',
default=None,
dest="password",
help="password to log into FreeIPA API"
)
parser.add_argument(
'-s',
'--server',
default=None,
dest="server",
help="hostname of FreeIPA server"
)
parser.add_argument(
'--ipa-version',
default='2.228',
dest="ipaversion",
help="version of FreeIPA server"
)
args = parser.parse_args()
# Hard code varibles here if required
freeipaserver = None
freeipauser = None
freeipapassword = None
if 'freeipaserver' in env:
freeipaserver = env['freeipaserver']
if 'freeipauser' in env:
freeipauser = env['freeipauser']
if 'freeipapassword' in env:
freeipapassword = env['freeipapassword']
if args.server:
freeipaserver = args.server
if args.user:
freeipauser = args.user
if args.password:
freeipapassword = args.password
if not freeipaserver:
exit("HALT: No FreeIPA server set")
if not freeipauser:
exit("HALT: No FreeIPA user set")
if not freeipapassword:
exit("HALT: No FreeIPA password set")
client = Client(
freeipaserver,
version='2.228',
verify_ssl=False
)
client.login(
freeipauser,
freeipapassword
)
if args.host:
# List host
result = client._request(
'host_show',
args.host,
{'all': True, 'raw': False}
)['result']
if 'usercertificate' in result:
del result['usercertificate']
print(json.dumps(result, indent=1))
elif args.list:
inventory = {}
hostvars = {}
result = client._request(
'hostgroup_find',
'',
{'all': True, 'raw': False}
)['result']
for hostgroup in result:
members = []
children = []
if 'member_host' in hostgroup:
members = [host for host in hostgroup['member_host']]
if 'member_hostgroup' in hostgroup:
children = hostgroup['member_hostgroup']
inventory[hostgroup['cn'][0]] = {
'hosts': [host for host in members],
'children': children
}
for member in members:
hostvars[member] = {}
inventory['_meta'] = {'hostvars': hostvars}
inv_string = json.dumps(inventory, indent=1, sort_keys=True)
print(inv_string)
else:
# For debugging
print("%s:%s@%s" %
(
freeipauser,
freeipapassword,
freeipaserver
)
)