Debian Codegraph @

ansible_collections/dellemc/openmanage/plugins/modules/ome_device_local_access_configuration.py

raw symbols SCIP
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 #
5 # Dell OpenManage Ansible Modules
6 # Version 9.3.0
7 # Copyright (C) 2022-2025 Dell Inc. or its subsidiaries. All Rights Reserved.
8
9 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
10 #
11
12 from __future__ import (absolute_import, division, print_function)
13 __metaclass__ = type
14
15 DOCUMENTATION = """
16 ---
17 module: ome_device_local_access_configuration
18 short_description: Configure local access settings on OpenManage Enterprise Modular.
19 description: This module allows to configure the local access settings of the power button, quick sync, KVM,
20 LCD, and chassis direct access on OpenManage Enterprise Modular.
21 version_added: "4.4.0"
22 extends_documentation_fragment:
23 - dellemc.openmanage.omem_auth_options
24 options:
25 device_id:
26 type: int
27 description:
28 - The ID of the chassis for which the local access configuration to be updated.
29 - If the device ID is not specified, this module updates the local access settings for the I(hostname).
30 - I(device_id) is mutually exclusive with I(device_service_tag).
31 device_service_tag:
32 type: str
33 description:
34 - The service tag of the chassis for which the local access settings needs to be updated.
35 - If the device service tag is not specified, this module updates the local access settings for the I(hostname).
36 - I(device_service_tag) is mutually exclusive with I(device_id).
37 enable_kvm_access:
38 type: bool
39 description: Enables or disables the keyboard, video, and mouse (KVM) interfaces.
40 enable_chassis_direct_access:
41 type: bool
42 description: Enables or disables the access to management consoles such as iDRAC and the management module of
43 the device on the chassis.
44 chassis_power_button:
45 type: dict
46 description: The settings for the chassis power button.
47 suboptions:
48 enable_chassis_power_button:
49 required: true
50 type: bool
51 description:
52 - Enables or disables the chassis power button.
53 - If C(false), the chassis cannot be turn on or turn off using the power button.
54 enable_lcd_override_pin:
55 type: bool
56 description:
57 - Enables or disables the LCD override pin.
58 - This is required when I(enable_chassis_power_button) is C(false).
59 disabled_button_lcd_override_pin:
60 type: str
61 description:
62 - The six digit LCD override pin to change the power state of the chassis.
63 - This is required when I(enable_lcd_override_pin) is C(true).
64 - The module will always report change when I(disabled_button_lcd_override_pin) is C(true).
65 - 'The value must be specified in quotes. ex: "001100".'
66 quick_sync:
67 type: dict
68 description:
69 - The settings for quick sync.
70 - The I(quick_sync) options are ignored if the quick sync hardware is not present.
71 suboptions:
72 quick_sync_access:
73 type: str
74 choices: [READ_WRITE, READ_ONLY, DISABLED]
75 description:
76 - Users with administrator privileges can set the following types of I(quick_sync_access).
77 - C(READ_WRITE) enables writing configuration using quick sync.
78 - C(READ_ONLY) enables read only access to Wi-Fi and Bluetooth Low Energy(BLE).
79 - C(DISABLED) disables reading or writing configuration through quick sync.
80 enable_inactivity_timeout:
81 type: bool
82 description: Enables or disables the inactivity timeout.
83 timeout_limit:
84 type: int
85 description:
86 - Inactivity timeout in seconds or minutes.
87 - The range is 120 to 3600 in seconds, or 2 to 60 in minutes.
88 - This option is required when I(enable_inactivity_timeout) is C(true).
89 timeout_limit_unit:
90 type: str
91 choices: [SECONDS, MINUTES]
92 description:
93 - Inactivity timeout limit unit.
94 - C(SECONDS) to set I(timeout_limit) in seconds.
95 - C(MINUTES) to set I(timeout_limit) in minutes.
96 - This option is required when I(enable_inactivity_timeout) is C(true).
97 enable_read_authentication:
98 type: bool
99 description: Enables or disables the option to log in using your user credentials and to read the
100 inventory in a secure data center.
101 enable_quick_sync_wifi:
102 type: bool
103 description: Enables or disables the Wi-Fi communication path to the chassis.
104 lcd:
105 type: dict
106 description:
107 - The settings for LCD.
108 - The I(lcd) options are ignored if the LCD hardware is not present in the chassis.
109 suboptions:
110 lcd_access:
111 type: str
112 choices: [VIEW_AND_MODIFY, VIEW_ONLY, DISABLED]
113 description:
114 - Option to configure the quick sync settings using LCD.
115 - C(VIEW_AND_MODIFY) to set access level to view and modify.
116 - C(VIEW_ONLY) to set access level to view.
117 - C(DISABLED) to disable the access.
118 user_defined:
119 type: str
120 description: The text to display on the LCD Home screen. The LCD Home screen is displayed when the system
121 is reset to factory default settings. The user-defined text can have a maximum of 62 characters.
122 lcd_language:
123 type: str
124 description:
125 - The language code in which the text on the LCD must be displayed.
126 - en to set English language.
127 - fr to set French language.
128 - de to set German language.
129 - es to set Spanish language.
130 - ja to set Japanese language.
131 - zh to set Chinese language.
132 requirements:
133 - "python >= 3.9.6"
134 author:
135 - "Felix Stephen (@felixs88)"
136 - "Shivam Sharma (@ShivamSh3)"
137 notes:
138 - Run this module from a system that has direct access to OpenManage Enterprise Modular.
139 - This module supports C(check_mode).
140 - The module will always report change when I(enable_chassis_power_button) is C(true).
141 """
142
143 EXAMPLES = """
144 ---
145 - name: Configure KVM, direct access and power button settings of the chassis using device ID.
146 dellemc.openmanage.ome_device_local_access_configuration:
147 hostname: "192.168.0.1"
148 username: "username"
149 password: "password"
150 ca_path: "/path/to/ca_cert.pem"
151 device_id: 25011
152 enable_kvm_access: true
153 enable_chassis_direct_access: false
154 chassis_power_button:
155 enable_chassis_power_button: false
156 enable_lcd_override_pin: true
157 disabled_button_lcd_override_pin: "123456"
158
159 - name: Configure Quick sync and LCD settings of the chassis using device service tag.
160 dellemc.openmanage.ome_device_local_access_configuration:
161 hostname: "192.168.0.1"
162 username: "username"
163 password: "password"
164 ca_path: "/path/to/ca_cert.pem"
165 device_service_tag: GHRT2RL
166 quick_sync:
167 quick_sync_access: READ_ONLY
168 enable_read_authentication: true
169 enable_quick_sync_wifi: true
170 enable_inactivity_timeout: true
171 timeout_limit: 10
172 timeout_limit_unit: MINUTES
173 lcd:
174 lcd_access: VIEW_ONLY
175 lcd_language: en
176 user_defined: "LCD Text"
177
178 - name: Configure all local access settings of the host chassis.
179 dellemc.openmanage.ome_device_local_access_configuration:
180 hostname: "192.168.0.1"
181 username: "username"
182 password: "password"
183 ca_path: "/path/to/ca_cert.pem"
184 enable_kvm_access: true
185 enable_chassis_direct_access: false
186 chassis_power_button:
187 enable_chassis_power_button: false
188 enable_lcd_override_pin: true
189 disabled_button_lcd_override_pin: "123456"
190 quick_sync:
191 quick_sync_access: READ_WRITE
192 enable_read_authentication: true
193 enable_quick_sync_wifi: true
194 enable_inactivity_timeout: true
195 timeout_limit: 120
196 timeout_limit_unit: SECONDS
197 lcd:
198 lcd_access: VIEW_MODIFY
199 lcd_language: en
200 user_defined: "LCD Text"
201 """
202
203 RETURN = """
204 ---
205 msg:
206 type: str
207 description: Overall status of the device local access settings.
208 returned: always
209 sample: "Successfully updated the local access settings."
210 location_details:
211 type: dict
212 description: returned when local access settings are updated successfully.
213 returned: success
214 sample: {
215 "SettingType": "LocalAccessConfiguration",
216 "EnableChassisDirect": false,
217 "EnableChassisPowerButton": false,
218 "EnableKvmAccess": true,
219 "EnableLcdOverridePin": false,
220 "LcdAccess": "VIEW_ONLY",
221 "LcdCustomString": "LCD Text",
222 "LcdLanguage": "en",
223 "LcdOverridePin": "",
224 "LcdPinLength": 6,
225 "LcdPresence": "Present",
226 "LedPresence": "Absent",
227 "QuickSync": {
228 "EnableInactivityTimeout": true,
229 "EnableQuickSyncWifi": false,
230 "EnableReadAuthentication": false,
231 "QuickSyncAccess": "READ_ONLY",
232 "QuickSyncHardware": "Present",
233 "TimeoutLimit": 7,
234 "TimeoutLimitUnit": "MINUTES"
235 }
236 }
237 error_info:
238 description: Details of the HTTP Error.
239 returned: on HTTP error
240 type: dict
241 sample: {
242 "error": {
243 "code": "Base.1.0.GeneralError",
244 "message": "A general error has occurred. See ExtendedInfo for more information.",
245 "@Message.ExtendedInfo": [
246 {
247 "MessageId": "GEN1234",
248 "RelatedProperties": [],
249 "Message": "Unable to process the request because an error occurred.",
250 "MessageArgs": [],
251 "Severity": "Critical",
252 "Resolution": "Retry the operation. If the issue persists, contact your system administrator."
253 }
254 ]
255 }
256 }
257 """
258
259
260 import json
261 import socket
262 import copy
263 from ssl import SSLError
264 from ansible.module_utils.six.moves.urllib.error import URLError, HTTPError
265 from ansible.module_utils.urls import ConnectionError
266 from ansible_collections.dellemc.openmanage.plugins.module_utils.ome import RestOME, OmeAnsibleModule
267
268 DOMAIN_URI = "ManagementDomainService/Domains"
269 DEVICE_URI = "DeviceService/Devices"
270 LAC_API = "DeviceService/Devices({0})/Settings('LocalAccessConfiguration')"
271 CONFIG_FAIL_MSG = "one of the following is required: enable_kvm_access, enable_chassis_direct_access, " \
272 "chassis_power_button, quick_sync, lcd"
273 DOMAIN_FAIL_MSG = "The operation to configure the local access is supported only on " \
274 "OpenManage Enterprise Modular."
275 FETCH_FAIL_MSG = "Unable to retrieve the device information."
276 DEVICE_FAIL_MSG = "Unable to complete the operation because the entered target device {0} '{1}' is invalid."
277 LAC_FAIL_MSG = "Unable to complete the operation because the local access configuration settings " \
278 "are not supported on the specified device."
279 CHANGES_FOUND = "Changes found to be applied."
280 NO_CHANGES_FOUND = "No changes found to be applied."
281 SUCCESS_MSG = "Successfully updated the local access settings."
282
283
284 def get_ip_from_host(hostname):
285 ipaddr = hostname
286 try:
287 result = socket.getaddrinfo(hostname, None)
288 last_element = result[-1]
289 ip_address = last_element[-1][0]
290 if ip_address:
291 ipaddr = ip_address
292 except socket.gaierror:
293 ipaddr = hostname
294 except Exception:
295 ipaddr = hostname
296 return ipaddr
297
298
299 def get_chassis_device(module, rest_obj):
300 key, value = None, None
301 ipaddress = get_ip_from_host(module.params["hostname"])
302 resp = rest_obj.invoke_request("GET", DOMAIN_URI)
303 for data in resp.json_data["value"]:
304 if ipaddress in data["PublicAddress"]:
305 key, value = ("Id", data["DeviceId"])
306 break
307 else:
308 module.fail_json(msg=FETCH_FAIL_MSG)
309 return key, value
310
311
312 def check_domain_service(module, rest_obj):
313 try:
314 rest_obj.invoke_request("GET", DOMAIN_URI, api_timeout=5)
315 except HTTPError as err:
316 err_message = json.load(err)
317 if err_message["error"]["@Message.ExtendedInfo"][0]["MessageId"] == "CGEN1006":
318 module.fail_json(msg=DOMAIN_FAIL_MSG)
319 return
320
321
322 def check_mode_validation(module, loc_resp):
323 exist_config = {
324 "EnableKvmAccess": loc_resp["EnableKvmAccess"], "EnableChassisDirect": loc_resp["EnableChassisDirect"],
325 "EnableChassisPowerButton": loc_resp["EnableChassisPowerButton"],
326 "EnableLcdOverridePin": loc_resp["EnableLcdOverridePin"], "LcdAccess": loc_resp["LcdAccess"],
327 "LcdCustomString": loc_resp["LcdCustomString"], "LcdLanguage": loc_resp["LcdLanguage"]}
328 quick_sync = loc_resp["QuickSync"]
329 exist_quick_config = {
330 "QuickSyncAccess": quick_sync["QuickSyncAccess"], "TimeoutLimit": quick_sync["TimeoutLimit"],
331 "EnableInactivityTimeout": quick_sync["EnableInactivityTimeout"],
332 "TimeoutLimitUnit": quick_sync["TimeoutLimitUnit"],
333 "EnableReadAuthentication": quick_sync["EnableReadAuthentication"],
334 "EnableQuickSyncWifi": quick_sync["EnableQuickSyncWifi"]}
335 req_config, req_quick_config, payload = {}, {}, {}
336 lcd_options, chassis_power = module.params.get("lcd"), module.params.get("chassis_power_button")
337 if loc_resp["LcdPresence"] == "Present" and lcd_options is not None:
338 req_config["LcdCustomString"] = lcd_options.get("user_defined")
339 req_config["LcdAccess"] = lcd_options.get("lcd_access")
340 req_config["LcdLanguage"] = lcd_options.get("lcd_language")
341 req_config["EnableKvmAccess"] = module.params.get("enable_kvm_access")
342 req_config["EnableChassisDirect"] = module.params.get("enable_chassis_direct_access")
343 if chassis_power is not None:
344 power_button = chassis_power["enable_chassis_power_button"]
345 if power_button is False:
346 chassis_pin = chassis_power.get("enable_lcd_override_pin")
347 if chassis_pin is True:
348 exist_config["LcdOverridePin"] = loc_resp["LcdOverridePin"]
349 req_config["LcdOverridePin"] = chassis_power["disabled_button_lcd_override_pin"]
350 req_config["EnableLcdOverridePin"] = chassis_pin
351 req_config["EnableChassisPowerButton"] = power_button
352 q_sync = module.params.get("quick_sync")
353 if q_sync is not None and loc_resp["QuickSync"]["QuickSyncHardware"] == "Present":
354 req_quick_config["QuickSyncAccess"] = q_sync.get("quick_sync_access")
355 req_quick_config["EnableReadAuthentication"] = q_sync.get("enable_read_authentication")
356 req_quick_config["EnableQuickSyncWifi"] = q_sync.get("enable_quick_sync_wifi")
357 if q_sync.get("enable_inactivity_timeout") is True:
358 time_limit, time_unit = q_sync.get("timeout_limit"), q_sync.get("timeout_limit_unit")
359 if q_sync.get("timeout_limit_unit") == "MINUTES":
360 time_limit, time_unit = time_limit * 60, "SECONDS"
361 req_quick_config["TimeoutLimit"] = time_limit
362 req_quick_config["TimeoutLimitUnit"] = time_unit
363 req_quick_config["EnableInactivityTimeout"] = q_sync.get("enable_inactivity_timeout")
364 req_config = dict([(k, v) for k, v in req_config.items() if v is not None])
365 req_quick_config = dict([(k, v) for k, v in req_quick_config.items() if v is not None])
366 cloned_req_config = copy.deepcopy(exist_config)
367 cloned_req_config.update(req_config)
368 cloned_req_quick_config = copy.deepcopy(exist_quick_config)
369 cloned_req_quick_config.update(req_quick_config)
370 diff_changes = [bool(set(exist_config.items()) ^ set(cloned_req_config.items())) or
371 bool(set(exist_quick_config.items()) ^ set(cloned_req_quick_config.items()))]
372 if module.check_mode and any(diff_changes) is True:
373 module.exit_json(msg=CHANGES_FOUND, changed=True)
374 elif (module.check_mode and all(diff_changes) is False) or \
375 (not module.check_mode and all(diff_changes) is False):
376 module.exit_json(msg=NO_CHANGES_FOUND)
377 payload.update(cloned_req_config)
378 payload["QuickSync"] = cloned_req_quick_config
379 payload["QuickSync"]["QuickSyncHardware"] = loc_resp["QuickSync"]["QuickSyncHardware"]
380 payload["SettingType"] = "LocalAccessConfiguration"
381 payload["LcdPresence"] = loc_resp["LcdPresence"]
382 payload["LcdPinLength"] = loc_resp["LcdPinLength"]
383 payload["LedPresence"] = loc_resp["LedPresence"]
384 return payload
385
386
387 def get_device_details(rest_obj, module):
388 device_id, tag = module.params.get("device_id"), module.params.get("device_service_tag")
389 if device_id is None and tag is None:
390 key, value = get_chassis_device(module, rest_obj)
391 device_id = value
392 else:
393 key, value = ("Id", device_id) if device_id is not None else ("DeviceServiceTag", tag)
394 param_value = "{0} eq {1}".format(key, value) if key == "Id" else "{0} eq '{1}'".format(key, value)
395 resp = rest_obj.invoke_request("GET", DEVICE_URI, query_param={"$filter": param_value})
396 resp_data = resp.json_data.get("value")
397 rename_key = "id" if key == "Id" else "service tag"
398 if not resp_data:
399 module.fail_json(msg=DEVICE_FAIL_MSG.format(rename_key, value))
400 if key == "DeviceServiceTag" and resp_data[0]["DeviceServiceTag"] == tag:
401 device_id = resp_data[0]["Id"]
402 elif key == "Id" and resp_data[0]["Id"] == device_id:
403 device_id = resp_data[0]["Id"]
404 else:
405 module.fail_json(msg=DEVICE_FAIL_MSG.format(rename_key, value))
406 try:
407 loc_resp = rest_obj.invoke_request("GET", LAC_API.format(device_id))
408 except HTTPError as err:
409 err_message = json.load(err)
410 error_msg = err_message.get('error', {}).get('@Message.ExtendedInfo')
411 if error_msg and error_msg[0].get("MessageId") == "CGEN1004":
412 module.fail_json(msg=LAC_FAIL_MSG)
413 else:
414 payload = check_mode_validation(module, loc_resp.json_data)
415 final_resp = rest_obj.invoke_request("PUT", LAC_API.format(device_id), data=payload)
416 return final_resp
417
418
419 def main():
420 chassis_power = {
421 "enable_chassis_power_button": {"type": "bool", "required": True},
422 "enable_lcd_override_pin": {"type": "bool", "required": False},
423 "disabled_button_lcd_override_pin": {"type": "str", "required": False, "no_log": True}}
424 quick_sync_options = {
425 "quick_sync_access": {"type": "str", "required": False, "choices": ["DISABLED", "READ_ONLY", "READ_WRITE"]},
426 "enable_inactivity_timeout": {"type": "bool", "required": False},
427 "timeout_limit": {"type": "int", "required": False},
428 "timeout_limit_unit": {"type": "str", "required": False, "choices": ["SECONDS", "MINUTES"]},
429 "enable_read_authentication": {"type": "bool", "required": False},
430 "enable_quick_sync_wifi": {"type": "bool", "required": False}}
431 lcd_options = {
432 "lcd_access": {"type": "str", "required": False, "choices": ["VIEW_AND_MODIFY", "VIEW_ONLY", "DISABLED"]},
433 "user_defined": {"type": "str", "required": False},
434 "lcd_language": {"type": "str", "required": False}}
435 specs = {
436 "device_id": {"required": False, "type": "int"},
437 "device_service_tag": {"required": False, "type": "str"},
438 "enable_kvm_access": {"required": False, "type": "bool"},
439 "enable_chassis_direct_access": {"required": False, "type": "bool"},
440 "chassis_power_button": {
441 "required": False, "type": "dict", "options": chassis_power,
442 "required_if": [["enable_lcd_override_pin", True, ("disabled_button_lcd_override_pin",)]],
443 },
444 "quick_sync": {
445 "required": False, "type": "dict", "options": quick_sync_options,
446 "required_if": [["enable_inactivity_timeout", True, ("timeout_limit", "timeout_limit_unit")]]
447 },
448 "lcd": {
449 "required": False, "type": "dict", "options": lcd_options,
450 },
451 }
452
453 module = OmeAnsibleModule(
454 argument_spec=specs,
455 mutually_exclusive=[('device_id', 'device_service_tag')],
456 required_one_of=[["enable_kvm_access", "enable_chassis_direct_access",
457 "chassis_power_button", "quick_sync", "lcd"]],
458 supports_check_mode=True,
459 )
460 try:
461 if not any([module.params.get("chassis_power_button"), module.params.get("quick_sync"),
462 module.params.get("lcd"), module.params.get("enable_kvm_access") is not None,
463 module.params.get("enable_chassis_direct_access") is not None]):
464 module.fail_json(msg=CONFIG_FAIL_MSG)
465 with RestOME(module.params, req_session=True) as rest_obj:
466 check_domain_service(module, rest_obj)
467 resp = get_device_details(rest_obj, module)
468 resp_data = resp.json_data
469 quick_sync = module.params.get("quick_sync")
470 if quick_sync is not None and quick_sync.get("enable_inactivity_timeout") is True and \
471 quick_sync.get("timeout_limit_unit") == "MINUTES":
472 resp_data["QuickSync"]["TimeoutLimit"] = int(resp_data["QuickSync"]["TimeoutLimit"] / 60)
473 resp_data["QuickSync"]["TimeoutLimitUnit"] = "MINUTES"
474 module.exit_json(msg=SUCCESS_MSG, local_access_settings=resp_data, changed=True)
475 except HTTPError as err:
476 module.exit_json(msg=str(err), error_info=json.load(err), failed=True)
477 except URLError as err:
478 module.exit_json(msg=str(err), unreachable=True)
479 except (IOError, ValueError, SSLError, TypeError, ConnectionError, AttributeError, IndexError, KeyError, OSError) as err:
480 module.exit_json(msg=str(err), failed=True)
481
482
483 if __name__ == '__main__':
484 main()