-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathipa_host.py
342 lines (304 loc) · 11 KB
/
ipa_host.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#!/usr/bin/python
# -*- coding: utf-8 -*-
DOCUMENTATION = '''
---
module: ipa_host
short_description: Manage FreeIPA host
description:
- Add, modify and delete an IPA host using IPA API
options:
fqdn:
description:
- Full qualified domain name.
- Can not be changed as it is the unique identifier.
required: true
aliases: ["name"]
description:
description:
- A description of this host.
required: false
force:
description:
- Force host name even if not in DNS.
required: false
ip_address:
description:
- Add the host to DNS with this IP address.
required: false
macddress:
description:
- List of Hardware MAC address(es) off this host.
- If option is omitted MAC addresses will not be checked or changed.
- If an empty list is passed all assigned MAC addresses will be removed.
- MAC addresses that are already assigned but not passed will be removed.
required: false
nshostlocation:
description:
- Host location (e.g. "Lab 2")
required: false
nshardwareplatform:
description:
- Host hardware platform (e.g. "Lenovo T61")
required: false
nsosversion:
description:
- Host operating system and version (e.g. "Fedora 9")
required: false
usercertificate:
description:
- List of Base-64 encoded server certificates.
- If option is ommitted certificates will not be checked or changed.
- If an emtpy list is passed all assigned certificates will be removed.
- Certificates already assigned but not passed will be removed.
required: false
state:
description: State to ensure
required: false
default: present
choices: ["present", "absent", "disabled"]
ipa_port:
description: Port of IPA server
required: false
default: 443
ipa_host:
description: IP or hostname of IPA server
required: false
default: ipa.example.com
ipa_user:
description: Administrative account used on IPA server
required: false
default: admin
ipa_pass:
description: Password of administrative user
required: true
ipa_prot:
description: Protocol used by IPA server
required: false
default: https
choices: ["http", "https"]
version_added: "2.2"
requirements:
- json
- requests
'''
EXAMPLES = '''
# Ensure host is present
- ipa_host:
name: host01.example.com
description: Example host
ip_address: 192.168.0.123
nshostlocation: Lab
nsosversion: CentOS 7
nshardwareplatform: Lenovo T61
macaddress:
- "08:00:27:E3:B1:2D"
- "52:54:00:BD:97:1E"
state: present
ipa_host: ipa.example.com
ipa_user: admin
ipa_pass: topsecret
# Ensure host is disabled
- ipa_host:
name: host01.example.com
state: disabled
ipa_host: ipa.example.com
ipa_user: admin
ipa_pass: topsecret
# Ensure that all usercertificates are removed
- ipa_host:
name: host01.example.com
usercertificate: []
ipa_host: ipa.example.com
ipa_user: admin
ipa_pass: topsecret
# Ensure host is absent
- ipa_host:
name: host01.example.com
state: absent
ipa_host: ipa.example.com
ipa_user: admin
ipa_pass: topsecret
'''
RETURN = '''
host:
description: Host as returned by IPA API.
returned: always
type: dict
host_diff:
description: List of options that differ and would be changed
returned: if check mode and a difference is found
type: list
'''
import json
import requests
class IPAClient:
def __init__(self, module, host, port, username, password, protocol):
self.host = host
self.port = port
self.username = username
self.password = password
self.protocol = protocol
self.headers = {'referer': self.get_base_url(),
'Content-Type': 'application/json',
'Accept': 'application/json'}
self.cookies = None
self.module = module
def get_base_url(self):
return '{prot}://{host}/ipa'.format(prot=self.protocol, host=self.host)
def get_json_url(self):
return '{base_url}/session/json'.format(base_url=self.get_base_url())
def login(self):
s = requests.session()
url = '{base_url}/session/login_password'.format(base_url=self.get_base_url())
data = dict(user=self.username, password=self.password)
headers = {'referer': self.get_base_url(),
'Content-Type': 'application/x-www-form-urlencoded',
'Accept': 'text/plain'}
try:
s = requests.post(url=url, data=data, headers=headers, verify=False)
s.raise_for_status()
except Exception as e:
self._fail('login', e)
self.cookies = s.cookies
def _fail(self, msg, e):
if 'message' in e:
err_string = e.get('message')
else:
err_string = e
self.module.fail_json(msg='{}: {}'.format(msg, err_string))
def _post_json(self, method, name, item=None):
if item is None:
item = {}
url = '{base_url}/session/json'.format(base_url=self.get_base_url())
data = {'method': method, 'params': [[name], item]}
try:
r = requests.post(url=url, data=json.dumps(data), headers=self.headers, cookies=self.cookies, verify=False)
r.raise_for_status()
except Exception as e:
self._fail('post {}'.format(method), e)
resp = json.loads(r.content)
err = resp.get('error')
if err is not None:
self._fail('repsonse {}'.format(method), err)
if 'result' in resp:
result = resp.get('result')
if 'result' in result:
result = result.get('result')
if isinstance(result, list):
if len(result) > 0:
return result[0]
return result
return None
def host_find(self, name):
return self._post_json(method='host_find', name=None, item={'all': True, 'fqdn': name})
def host_add(self, name, host):
return self._post_json(method='host_add', name=name, item=host)
def host_mod(self, name, host):
return self._post_json(method='host_mod', name=name, item=host)
def host_del(self, name):
return self._post_json(method='host_del', name=name)
def host_disable(self, name):
return self._post_json(method='host_disable', name=name)
def get_host_dict(description=None, force=None, ip_address=None, nshostlocation=None, nshardwareplatform=None,
nsosversion=None, usercertificate=None, macaddress=None):
data = {}
if description is not None:
data['description'] = description
if force is not None:
data['force'] = force
if ip_address is not None:
data['ip_address'] = ip_address
if nshostlocation is not None:
data['nshostlocation'] = nshostlocation
if nshardwareplatform is not None:
data['nshardwareplatform'] = nshardwareplatform
if nsosversion is not None:
data['nsosversion'] = nsosversion
if usercertificate is not None:
data['usercertificate'] = [{"__base64__": item} for item in usercertificate]
if macaddress is not None:
data['macaddress'] = macaddress
return data
def get_host_diff(ipa_host, module_host):
non_updateable_keys = ['force', 'ip_address']
data = []
for key in non_updateable_keys:
if key in module_host:
del module_host[key]
for key in module_host.keys():
ipa_value = ipa_host.get(key, None)
module_value = module_host.get(key, None)
if isinstance(ipa_value, list) and not isinstance(module_value, list):
module_value = [module_value]
if isinstance(ipa_value, list) and isinstance(module_value, list):
ipa_value = sorted(ipa_value)
module_value = sorted(module_value)
if ipa_value != module_value:
data.append(key)
return data
def ensure(module, client):
name = module.params['name']
state = module.params['state']
ipa_host = client.host_find(name=name)
module_host = get_host_dict(description=module.params['description'],
force=module.params['force'], ip_address=module.params['ip_address'],
nshostlocation=module.params['nshostlocation'],
nshardwareplatform=module.params['nshardwareplatform'],
nsosversion=module.params['nsosversion'],
usercertificate=module.params['usercertificate'],
macaddress=module.params['macaddress'])
changed = False
if state in ['present', 'enabled', 'disabled']:
if not ipa_host:
changed = True
if not module.check_mode:
ipa_host = client.host_add(name=name, host=module_host)
else:
diff = get_host_diff(ipa_host, module_host)
if len(diff) > 0:
changed = True
if not module.check_mode:
ipa_host = client.host_mod(name=name, host={key: module_host.get(key) for key in diff})
else:
if ipa_host:
changed = True
if not module.check_mode:
client.host_del(name=name)
return changed, ipa_host
def main():
module = AnsibleModule(
argument_spec=dict(
description=dict(type='str', required=False),
fqdn=dict(type='str', required=True, aliases=['name']),
force=dict(type='bool', required=False),
ip_address=dict(type='str', required=False),
nshostlocation=dict(type='str', required=False),
nshardwareplatform=dict(type='str', required=False),
nsosversion=dict(type='str', required=False),
usercertificate=dict(type='list', required=False),
macaddress=dict(type='list', required=False),
state=dict(type='str', required=False, default='present',
choices=['present', 'absent', 'enabled', 'disabled']),
ipa_prot=dict(type='str', required=False, default='https', choices=['http', 'https']),
ipa_host=dict(type='str', required=False, default='ipa.example.com'),
ipa_port=dict(type='int', required=False, default=443),
ipa_user=dict(type='str', required=False, default='admin'),
ipa_pass=dict(type='str', required=True, no_log=True),
),
supports_check_mode=True,
)
client = IPAClient(module=module,
host=module.params['ipa_host'],
port=module.params['ipa_port'],
username=module.params['ipa_user'],
password=module.params['ipa_pass'],
protocol=module.params['ipa_prot'])
try:
client.login()
changed, host = ensure(module, client)
module.exit_json(changed=changed, host=host)
except Exception as e:
module.fail_json(msg=e.message)
from ansible.module_utils.basic import AnsibleModule
if __name__ == '__main__':
main()