Debian Codegraph @

ansible_collections/f5networks/f5_modules/plugins/modules/bigip_sys_global.py

raw symbols SCIP
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3 #
4 # Copyright: (c) 2016, F5 Networks Inc.
5 # GNU General Public License v3.0 (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
6
7 from __future__ import absolute_import, division, print_function
8 __metaclass__ = type
9
10 DOCUMENTATION = r'''
11 ---
12 module: bigip_sys_global
13 short_description: Manage BIG-IP global settings
14 description:
15 - Manage BIG-IP global settings.
16 version_added: "1.0.0"
17 options:
18 banner_text:
19 description:
20 - Specifies the text to present in the advisory banner.
21 type: str
22 console_timeout:
23 description:
24 - Specifies the number of seconds of inactivity before the system logs
25 off a user that is logged on.
26 type: int
27 gui_audit:
28 description:
29 - C(true) or C(false), specifies whether or not system GUI log audit messages.
30 type: bool
31 version_added: "1.23.0"
32 gui_setup:
33 description:
34 - C(true) or C(false), the Setup utility in the browser-based
35 Configuration utility.
36 type: bool
37 lcd_display:
38 description:
39 - When C(true), specifies the system menu displays on the
40 LCD screen on the front of the unit. This setting has no effect
41 when used on the VE platform.
42 type: bool
43 mgmt_dhcp:
44 description:
45 - Specifies whether or not to enable DHCP client on the management
46 interface.
47 type: bool
48 net_reboot:
49 description:
50 - When C(true), specifies the next time you reboot the system,
51 the system boots to an ISO image on the network, rather than an
52 internal media drive.
53 type: bool
54 quiet_boot:
55 description:
56 - When C(true), specifies the system suppresses informational
57 text on the console during the boot cycle. When C(no), the
58 system presents messages and informational text on the console during
59 the boot cycle.
60 type: bool
61 security_banner:
62 description:
63 - Specifies whether the system displays an advisory message on the
64 login screen.
65 type: bool
66 state:
67 description:
68 - The state of the variable on the system. When C(present), guarantees
69 an existing variable is set to C(value).
70 type: str
71 choices:
72 - present
73 default: present
74 extends_documentation_fragment: f5networks.f5_modules.f5
75 author:
76 - Tim Rupp (@caphrim007)
77 - Wojciech Wypior (@wojtek0806)
78 '''
79
80 EXAMPLES = r'''
81 - name: Disable the setup utility
82 bigip_sys_global:
83 gui_setup: false
84 provider:
85 password: secret
86 server: lb.mydomain.com
87 user: admin
88 delegate_to: localhost
89 '''
90
91 RETURN = r'''
92 banner_text:
93 description: The new text to present in the advisory banner.
94 returned: changed
95 type: str
96 sample: This is a corporate device. Do not touch.
97 console_timeout:
98 description:
99 - The new number of seconds of inactivity before the system
100 logs off a user that is logged on.
101 returned: changed
102 type: int
103 sample: 600
104 gui_audit:
105 description: The new setting for GUI auditing.
106 returned: changed
107 type: bool
108 sample: true
109 gui_setup:
110 description: The new setting for the Setup utility.
111 returned: changed
112 type: bool
113 sample: true
114 lcd_display:
115 description: The new setting for displaying the system menu on the LCD.
116 returned: changed
117 type: bool
118 sample: true
119 mgmt_dhcp:
120 description: The new setting for whether the mgmt interface should use DHCP or not.
121 returned: changed
122 type: bool
123 sample: true
124 net_reboot:
125 description: The new setting for whether the system should boot to an ISO on the network or not.
126 returned: changed
127 type: bool
128 sample: true
129 quiet_boot:
130 description:
131 - The new setting for whether the system should suppress information to
132 the console during boot or not.
133 returned: changed
134 type: bool
135 sample: true
136 security_banner:
137 description:
138 - The new setting for whether the system should display an advisory message
139 on the login screen or not.
140 returned: changed
141 type: bool
142 sample: true
143 '''
144 from datetime import datetime
145 from ansible.module_utils.basic import AnsibleModule
146
147 from ..module_utils.bigip import F5RestClient
148 from ..module_utils.common import (
149 F5ModuleError, AnsibleF5Parameters, f5_argument_spec, flatten_boolean
150 )
151 from ..module_utils.icontrol import tmos_version
152 from ..module_utils.teem import send_teem
153
154
155 class Parameters(AnsibleF5Parameters):
156 api_map = {
157 'guiSecurityBanner': 'security_banner',
158 'guiSecurityBannerText': 'banner_text',
159 'guiAudit': 'gui_audit',
160 'guiSetup': 'gui_setup',
161 'lcdDisplay': 'lcd_display',
162 'mgmtDhcp': 'mgmt_dhcp',
163 'netReboot': 'net_reboot',
164 'quietBoot': 'quiet_boot',
165 'consoleInactivityTimeout': 'console_timeout',
166 }
167
168 api_attributes = [
169 'guiSecurityBanner',
170 'guiSecurityBannerText',
171 'guiAudit',
172 'guiSetup',
173 'lcdDisplay',
174 'mgmtDhcp',
175 'netReboot',
176 'quietBoot',
177 'consoleInactivityTimeout',
178 ]
179
180 returnables = [
181 'security_banner',
182 'banner_text',
183 'gui_audit',
184 'gui_setup',
185 'lcd_display',
186 'mgmt_dhcp',
187 'net_reboot',
188 'quiet_boot',
189 'console_timeout',
190 ]
191
192 updatables = [
193 'security_banner',
194 'banner_text',
195 'gui_audit',
196 'gui_setup',
197 'lcd_display',
198 'mgmt_dhcp',
199 'net_reboot',
200 'quiet_boot',
201 'console_timeout',
202 ]
203
204 @property
205 def security_banner(self):
206 return flatten_boolean(self._values['security_banner'])
207
208 @property
209 def gui_audit(self):
210 return flatten_boolean(self._values['gui_audit'])
211
212 @property
213 def gui_setup(self):
214 return flatten_boolean(self._values['gui_setup'])
215
216 @property
217 def lcd_display(self):
218 return flatten_boolean(self._values['lcd_display'])
219
220 @property
221 def mgmt_dhcp(self):
222 return flatten_boolean(self._values['mgmt_dhcp'])
223
224 @property
225 def net_reboot(self):
226 return flatten_boolean(self._values['net_reboot'])
227
228 @property
229 def quiet_boot(self):
230 return flatten_boolean(self._values['quiet_boot'])
231
232
233 class ApiParameters(Parameters):
234 pass
235
236
237 class ModuleParameters(Parameters):
238 pass
239
240
241 class Changes(Parameters):
242 def to_return(self):
243 result = {}
244 try:
245 for returnable in self.returnables:
246 result[returnable] = getattr(self, returnable)
247 result = self._filter_params(result)
248 except Exception:
249 pass
250 return result
251
252
253 class UsableChanges(Changes):
254 @property
255 def security_banner(self):
256 if self._values['security_banner'] is None:
257 return None
258 if self._values['security_banner'] == 'yes':
259 return 'enabled'
260 return 'disabled'
261
262 @property
263 def gui_audit(self):
264 if self._values['gui_audit'] is None:
265 return None
266 if self._values['gui_audit'] == 'yes':
267 return 'enabled'
268 return 'disabled'
269
270 @property
271 def gui_setup(self):
272 if self._values['gui_setup'] is None:
273 return None
274 if self._values['gui_setup'] == 'yes':
275 return 'enabled'
276 return 'disabled'
277
278 @property
279 def lcd_display(self):
280 if self._values['lcd_display'] is None:
281 return None
282 if self._values['lcd_display'] == 'yes':
283 return 'enabled'
284 return 'disabled'
285
286 @property
287 def mgmt_dhcp(self):
288 if self._values['mgmt_dhcp'] is None:
289 return None
290 if self._values['mgmt_dhcp'] == 'yes':
291 return 'enabled'
292 return 'disabled'
293
294 @property
295 def net_reboot(self):
296 if self._values['net_reboot'] is None:
297 return None
298 if self._values['net_reboot'] == 'yes':
299 return 'enabled'
300 return 'disabled'
301
302 @property
303 def quiet_boot(self):
304 if self._values['quiet_boot'] is None:
305 return None
306 if self._values['quiet_boot'] == 'yes':
307 return 'enabled'
308 return 'disabled'
309
310
311 class ReportableChanges(Changes):
312 @property
313 def security_banner(self):
314 return flatten_boolean(self._values['security_banner'])
315
316 @property
317 def gui_audit(self):
318 return flatten_boolean(self._values['gui_audit'])
319
320 @property
321 def gui_setup(self):
322 return flatten_boolean(self._values['gui_setup'])
323
324 @property
325 def lcd_display(self):
326 return flatten_boolean(self._values['lcd_display'])
327
328 @property
329 def mgmt_dhcp(self):
330 return flatten_boolean(self._values['mgmt_dhcp'])
331
332 @property
333 def net_reboot(self):
334 return flatten_boolean(self._values['net_reboot'])
335
336 @property
337 def quiet_boot(self):
338 return flatten_boolean(self._values['quiet_boot'])
339
340
341 class Difference(object):
342 def __init__(self, want, have=None):
343 self.want = want
344 self.have = have
345
346 def compare(self, param):
347 try:
348 result = getattr(self, param)
349 return result
350 except AttributeError:
351 return self.__default(param)
352
353 def __default(self, param):
354 want = getattr(self.want, param)
355 try:
356 have = getattr(self.have, param)
357 if want != have:
358 return want
359 except AttributeError:
360 return want
361
362
363 class ModuleManager(object):
364 def __init__(self, *args, **kwargs):
365 self.module = kwargs.get('module', None)
366 self.client = F5RestClient(**self.module.params)
367 self.want = ModuleParameters(params=self.module.params)
368 self.have = ApiParameters()
369 self.changes = UsableChanges()
370
371 def _set_changed_options(self):
372 changed = {}
373 for key in Parameters.returnables:
374 if getattr(self.want, key) is not None:
375 changed[key] = getattr(self.want, key)
376 if changed:
377 self.changes = UsableChanges(params=changed)
378
379 def _update_changed_options(self):
380 diff = Difference(self.want, self.have)
381 updatables = Parameters.updatables
382 changed = dict()
383 for k in updatables:
384 change = diff.compare(k)
385 if change is None:
386 continue
387 else:
388 if isinstance(change, dict):
389 changed.update(change)
390 else:
391 changed[k] = change
392 if changed:
393 self.changes = UsableChanges(params=changed)
394 return True
395 return False
396
397 def should_update(self):
398 result = self._update_changed_options()
399 if result:
400 return True
401 return False
402
403 def exec_module(self):
404 start = datetime.now().isoformat()
405 version = tmos_version(self.client)
406 result = dict()
407
408 changed = self.present()
409
410 reportable = ReportableChanges(params=self.changes.to_return())
411 changes = reportable.to_return()
412 result.update(**changes)
413 result.update(dict(changed=changed))
414 self._announce_deprecations(result)
415 send_teem(start, self.client, self.module, version)
416 return result
417
418 def _announce_deprecations(self, result):
419 warnings = result.pop('__warnings', [])
420 for warning in warnings:
421 self.module.deprecate(
422 msg=warning['msg'],
423 version=warning['version']
424 )
425
426 def present(self):
427 return self.update()
428
429 def update(self):
430 self.have = self.read_current_from_device()
431 if not self.should_update():
432 return False
433 if self.module.check_mode:
434 return True
435 self.update_on_device()
436 return True
437
438 def read_current_from_device(self):
439 uri = "https://{0}:{1}/mgmt/tm/sys/global-settings/".format(
440 self.client.provider['server'],
441 self.client.provider['server_port'],
442 )
443 resp = self.client.api.get(uri)
444 try:
445 response = resp.json()
446 except ValueError as ex:
447 raise F5ModuleError(str(ex))
448
449 if 'code' in response and response['code'] == 400:
450 if 'message' in response:
451 raise F5ModuleError(response['message'])
452 else:
453 raise F5ModuleError(resp.content)
454 return ApiParameters(params=response)
455
456 def update_on_device(self):
457 params = self.changes.api_params()
458 uri = "https://{0}:{1}/mgmt/tm/sys/global-settings/".format(
459 self.client.provider['server'],
460 self.client.provider['server_port'],
461 )
462 resp = self.client.api.patch(uri, json=params)
463 try:
464 response = resp.json()
465 except ValueError as ex:
466 raise F5ModuleError(str(ex))
467
468 if 'code' in response and response['code'] == 400:
469 if 'message' in response:
470 raise F5ModuleError(response['message'])
471 else:
472 raise F5ModuleError(resp.content)
473
474
475 class ArgumentSpec(object):
476 def __init__(self):
477 self.supports_check_mode = True
478 self.states = ['present']
479 argument_spec = dict(
480 security_banner=dict(
481 type='bool'
482 ),
483 banner_text=dict(),
484 gui_audit=dict(
485 type='bool'
486 ),
487 gui_setup=dict(
488 type='bool'
489 ),
490 lcd_display=dict(
491 type='bool'
492 ),
493 mgmt_dhcp=dict(
494 type='bool'
495 ),
496 net_reboot=dict(
497 type='bool'
498 ),
499 quiet_boot=dict(
500 type='bool'
501 ),
502 console_timeout=dict(
503 type='int'
504 ),
505 state=dict(
506 default='present', choices=['present']
507 )
508 )
509 self.argument_spec = {}
510 self.argument_spec.update(f5_argument_spec)
511 self.argument_spec.update(argument_spec)
512
513
514 def main():
515 spec = ArgumentSpec()
516
517 module = AnsibleModule(
518 argument_spec=spec.argument_spec,
519 supports_check_mode=spec.supports_check_mode
520 )
521
522 try:
523 mm = ModuleManager(module=module)
524 results = mm.exec_module()
525 module.exit_json(**results)
526 except F5ModuleError as ex:
527 module.fail_json(msg=str(ex))
528
529
530 if __name__ == '__main__':
531 main()