forked from acoston/Ansible-EfficientIP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
efficientip.py
416 lines (361 loc) · 13 KB
/
efficientip.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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
#
# Copyright: Ansible Project
#
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
from __future__ import absolute_import, division, print_function
__metaclass__ = type
ANSIBLE_METADATA = {
'metadata_version': '1.1',
'status': ['preview'],
'supported_by': 'community'
}
DOCUMENTATION = '''
---
module: efficientip
version_added: "2.9"
short_description: Interface with EfficientIP
description:
- "Manages domains and records via the EfficientIP Rest API
options:
eip_server:
description:
- EfficientIP server URL
required: true
type: str
eip_username:
description:
- EfficientIP user account
required: true
type: str
eip_password:
description:
- EfficientIP user password
required: true
type: str
record:
description:
- The full DNS record to create or delete
type: str
space:
description:
- EfficientIP space name or ID
- If input "LIST" a list of spaces will be returned.
type: str
space_id:
description:
- EfficientIP space ID
type: str
subnet:
description:
- Subnet name
- If input "LIST" a list of subnets will be returned.
required: true
type: str
subnet_id:
description:
- Subnet ID
type: str
type:
description:
- The type of DNS record to create.
choices: [ 'A', 'CNAME' ]
type: str
required: true
hostaddr:
description:
- Record value (IP Address)
- If input "FIND_FREE" one free IP address will be returned
type: str
macaddr:
description:
- MAC address
type: str
alias_fqdn:
description:
- reference for CNAME
type: str
alias_value:
description:
- Value for CNAME
type: str
alias_ttl:
description:
- TTL for CNAME
type: str
classparam:
description:
- Classparameter for entry
type: str
classname:
description:
- Classname for entry
type: str
state:
description:
- whether the record should exist or not.
choices: [ 'present', 'absent' ]
default: present
type: str
'''
EXAMPLES = '''
- name: list space
efficientip:
eip_server: server.mydomain.com
eip_username: dummyusername
eip_password: dummypassword
space: LIST
- name: list usable subnet from a space
efficientip:
eip_server: server.mydomain.com
eip_username: dummyusername
eip_password: dummypassword
subnet: LIST
space: NY_space
classparam: 'metadata1=somedata'
-or-
classname: myclass
- name: find one free IP address on a subnet
efficientip:
eip_server: server.mydomain.com
eip_username: dummyusername
eip_password: dummypassword
hostaddr: FIND_FREE
subnet_id: 4
- name: add IP on space
efficientip:
eip_server: server.mydomain.com
eip_username: dummyusername
eip_password: dummypassword
type: A
state: present
space: NY_space
record: test.mydomain.com
hostaddr: 127.0.0.1
- name: delete IP on space
efficientip:
eip_server: server.mydomain.com
eip_username: dummyusername
eip_password: dummypassword
type: A
state: absent
space: NY_space
hostaddr: 127.0.0.1
- name: add CNAME
efficientip:
eip_server: server.mydomain.com
eip_username: dummyusername
eip_password: dummypassword
type: CNAME
state: present
alias_fqdn: alias.mydomain.net
alias_value: hostname.mydomain.net
alias_ttl: 600
- name: delete CNAME
efficientip:
eip_server: server.mydomain.com
eip_username: dummyusername
eip_password: dummypassword
type: CNAME
state: absent
alias_fqdn: alias.mydomain.net
alias_value: hostname.mydomain.net
alias_ttl: 600
'''
RETURN = r"""# """
import base64
import requests
from ansible.module_utils.basic import AnsibleModule
def req(base_url, ipm_auth_hdr, method, ipm_cmd, querystring):
try:
session = requests.request(method, base_url + ipm_cmd, params=querystring, headers=ipm_auth_hdr, verify=False, timeout=10)
except:
try:
if session:
req_status_code = session.status_code
req_output = {"error" : "EfficientIP Server unreachable"}
except:
req_output = {"error" : "EfficientIP Server unreachable"}
req_status_code = session.status_code
if req_status_code == 204:
req_output = {"output" : "no data" }
if req_status_code == 401:
req_output = {"error" : "check EfficientIP Server credential" }
if req_status_code == 400:
req_output = {"error" : "check EfficientIP Server or parameter format" }
if req_status_code == 500:
req_output = {"error" : "something went wrong" }
# status code handling: from there data are expected
req_output = session.json()
if req_status_code == 201:
ipm_check=True
ipm_changed=True
elif req_status_code == 200:
if ipm_cmd == 'rest/ip_delete' or ipm_cmd == 'rest/dns_rr_delete' :
ipm_check=True
ipm_changed=True
else:
ipm_check=True
ipm_changed=False
else:
ipm_check=False
ipm_changed=False
return (req_output, ipm_check, ipm_changed)
def ip_space_list(base_url, ipm_auth_hdr):
method = 'get'
ipm_cmd = 'rest/ip_site_list'
querystring = ''
return req(base_url, ipm_auth_hdr, method, ipm_cmd, querystring)
def ip_subnet_list(base_url, ipm_auth_hdr, space, classparam, classname):
method = 'get'
ipm_cmd = 'rest/ip_block_subnet_list'
if classparam is not None:
obj1, param1 = classparam.split('=')
querystring = {'TAGS' : 'network.'+ obj1 +'' , 'WHERE' : 'site_name = \''+ space +'\' AND is_terminal = \'1\' AND tag_network_'+ obj1 +' = \''+ param1 +'\''}
elif classname is not None:
querystring = {'WHERE' : 'site_name = \''+ space +'\' AND is_terminal = \'1\' AND subnet_class_name = \''+ classname +'\''}
else:
querystring = {'WHERE' : 'site_name = \''+ space +'\' AND is_terminal = \'1\''}
return req(base_url, ipm_auth_hdr, method, ipm_cmd, querystring)
def ip_address_add(base_url, ipm_auth_hdr, space, record, hostaddr, macaddr, classparam, classname):
method = 'post'
ipm_cmd = 'rest/ip_add'
querystring = {'site_name': space,'name': record,'hostaddr': hostaddr, 'mac_addr' : macaddr, 'ip_class_parameters' : classparam, 'ip_class_name': classname}
return req(base_url, ipm_auth_hdr, method, ipm_cmd, querystring)
def ip_address_delete(base_url, ipm_auth_hdr, space, hostaddr):
method = 'delete'
ipm_cmd = 'rest/ip_delete'
querystring = {'site_name': space,'hostaddr': hostaddr}
return req(base_url, ipm_auth_hdr, method, ipm_cmd, querystring)
def ip_address_find_free(base_url, ipm_auth_hdr, subnet_id):
method = 'get'
ipm_cmd = 'rpc/ip_find_free_address'
querystring = {'subnet_id' : subnet_id, 'max_find' : '1'}
return req(base_url, ipm_auth_hdr, method, ipm_cmd, querystring)
def dns_cname_add(base_url, ipm_auth_hdr, alias_fqdn, alias_value, alias_ttl):
method = 'post'
ipm_cmd = 'rest/dns_rr_add'
querystring = {'rr_type': 'cname','rr_name': alias_fqdn, 'value1' : alias_value, 'rr_ttl' : alias_ttl }
return req(base_url, ipm_auth_hdr, method, ipm_cmd, querystring)
def dns_cname_delete(base_url, ipm_auth_hdr, alias_fqdn, alias_value):
method = 'delete'
ipm_cmd = 'rest/dns_rr_delete'
querystring = {'rr_name': alias_fqdn, 'value1' : alias_value}
return req(base_url, ipm_auth_hdr, method, ipm_cmd ,querystring)
def main():
module = AnsibleModule(
argument_spec=dict(
eip_server=dict(type='str', required=True),
eip_username=dict(type='str', required=True),
eip_password=dict(type='str',required=True, no_log=True),
record=dict(type='str'),
space=dict(type='str'),
space_id=dict(type='str'),
subnet=dict(type='str'),
subnet_id=dict(type='str'),
type=dict(type='str', choices=['A', 'CNAME'], required=True),
hostaddr=dict(type='str'),
macaddr=dict(type='str'),
alias_fqdn=dict(type='str'),
alias_value=dict(type='str'),
alias_ttl=dict(type='str'),
classparam=dict(type='str'),
classname=dict(type='str'),
state=dict(type='str', choices=['present', 'absent'], default='present')
)
)
eip_server = module.params.get('eip_server')
eip_username = module.params.get('eip_username')
eip_password = module.params.get('eip_password')
record = module.params.get('record')
space = module.params.get('space')
space_id = module.params.get('space_id')
subnet = module.params.get('subnet')
subnet_id = module.params.get('subnet_id')
type=module.params.get('type')
hostaddr = module.params.get('hostaddr')
macaddr = module.params.get('macaddr')
alias_fqdn = module.params.get('alias_fqdn')
alias_value = module.params.get('alias_value')
alias_ttl = module.params.get('alias_ttl')
classparam = module.params.get('classparam')
classname = module.params.get('classname')
state = module.params.get('state')
ipm_auth_hdr = {
'X-IPM-Username': base64.b64encode(eip_username),
'X-IPM-Password': base64.b64encode(eip_password)
}
base_url = "https://{host}/".format(host=eip_server)
try:
if space == 'LIST':
result = ip_space_list(base_url, ipm_auth_hdr)
if result[1] == True:
data = []
for rows in result[0]:
data.append({ 'space_id': rows['site_id'], 'space' : rows['site_name'] })
req_output = { 'output' : data }
module.exit_json(changed=result[2], result=req_output)
else:
raise Exception()
if subnet == 'LIST':
result = ip_subnet_list(base_url, ipm_auth_hdr, space, classparam, classname)
if result[1] == True:
data = []
for rows in result[0]:
raw_network = int(rows['start_ip_addr'],16)
network = '.'.join( [ str((raw_network >> 8*i) % 256) for i in [3,2,1,0] ])
data.append({ 'ipm_subnet_size' : rows['subnet_size'], 'ipm_subnet_addr' : network, 'ipm_subnet_id' : rows['subnet_id'], 'ipm_subnet' : rows['subnet_name'] })
req_output = { 'output' : data }
module.exit_json(changed=result[2], result=req_output)
else:
raise Exception()
if type == 'A' and state == 'present':
result = ip_address_add(base_url, ipm_auth_hdr, space, record, hostaddr, macaddr, classparam, classname)
if result[1] == True:
req_output = {"output" : "entry added" }
module.exit_json(changed=result[2], result=req_output)
elif result[1] == False:
req_output = {"output" : result[0] }
module.exit_json(changed=result[2], result=req_output, failed=True)
else:
raise Exception()
if type == 'A' and state == 'absent':
result = ip_address_delete(base_url, ipm_auth_hdr, space, hostaddr)
if result[1] == True:
req_output = {"output" : "entry deleted" }
module.exit_json(changed=result[2], result=req_output)
elif result[1] == False:
req_output = {"output" : result[0] }
module.exit_json(changed=result[2], result=req_output, failed=True)
else:
raise Exception()
if hostaddr == 'FIND_FREE':
result = ip_address_find_free(base_url, ipm_auth_hdr, subnet_id)
if result[1] == True:
req_output = { 'output' : result[0][0]["hostaddr"]}
module.exit_json(changed=result[2], result=req_output)
else:
raise Exception()
if type == 'CNAME' and state == 'present' :
result = dns_cname_add(base_url, ipm_auth_hdr, alias_fqdn, alias_value ,alias_ttl)
if result[1] == True:
req_output = {"output" : "entry added" }
module.exit_json(changed=result[2], result=req_output)
elif result[1] == False:
req_output = {"output" : result[0] }
module.exit_json(changed=result[2], result=req_output, failed=True)
else:
raise Exception()
if type == 'CNAME' and state == 'absent':
result = dns_cname_delete(base_url, ipm_auth_hdr, alias_fqdn, alias_value)
if result[1] == True:
req_output = {"output" : "entry deleted" }
module.exit_json(changed=result[2], result=req_output)
elif result[1] == False:
req_output = {"output" : result[0] }
module.exit_json(changed=result[2], result=req_output, failed=True)
else:
raise Exception()
except Exception as kaboom:
module.fail_json(msg=str(kaboom))
if __name__ == '__main__':
main()