Debian Codegraph @

ansible_collections/dellemc/openmanage/plugins/modules/idrac_boot.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
13 from __future__ import (absolute_import, division, print_function)
14 __metaclass__ = type
15
16 DOCUMENTATION = """
17 ---
18 module: idrac_boot
19 short_description: Configure the boot order settings.
20 version_added: "6.1.0"
21 description:
22 - This module allows to configure the boot order settings.
23 extends_documentation_fragment:
24 - dellemc.openmanage.idrac_x_auth_options
25 options:
26 boot_options:
27 type: list
28 elements: dict
29 description:
30 - Options to enable or disable the boot devices.
31 - This is mutually exclusive with I(boot_order), I(boot_source_override_mode), I(boot_source_override_enabled)
32 I(boot_source_override_target), and I(uefi_target_boot_source_override).
33 suboptions:
34 boot_option_reference:
35 type: str
36 description:
37 - FQDD of the boot device.
38 - This is mutually exclusive with I(display_name).
39 display_name:
40 type: str
41 description:
42 - Display name of the boot source device.
43 - This is mutually exclusive with I(boot_option_reference).
44 enabled:
45 type: bool
46 required: true
47 description: Enable or disable the boot device.
48 boot_order:
49 type: list
50 elements: str
51 description:
52 - This option allows to set the boot devices in the required boot order sequences.
53 - This is mutually exclusive with I(boot_options).
54 boot_source_override_mode:
55 type: str
56 description:
57 - The BIOS boot mode (either Legacy or UEFI) to be used when I(boot_source_override_target)
58 boot source is booted from.
59 - C(legacy) The system boot in non-UEFI(Legacy) boot mode to the I(boot_source_override_target).
60 - C(uefi) The system boot in UEFI boot mode to the I(boot_source_override_target).
61 - This is read-only property for iDRAC 17G and later.
62 - This is mutually exclusive with I(boot_options).
63 choices: [legacy, uefi]
64 boot_source_override_enabled:
65 type: str
66 description:
67 - The state of the Boot Source Override feature.
68 - C(disabled) The system boots normally.
69 - C(once) The system boots (one time) to the I(boot_source_override_target).
70 - C(continuous) The system boots to the target specified in the I(boot_source_override_target)
71 until this property is set to Disabled.
72 - The state is set to C(once) for the one-time boot override and C(continuous) for the
73 remain-active-until—canceled override. If the state is set C(once) or C(continuous), the value is reset
74 to C(disabled) after the I(boot_source_override_target) actions have completed successfully.
75 - Changes to this options do not alter the BIOS persistent boot order configuration.
76 - This is mutually exclusive with I(boot_options).
77 choices: [continuous, disabled, once]
78 boot_source_override_target:
79 type: str
80 description:
81 - The boot source override target device to use during the next boot instead of the normal boot device.
82 - C(pxe) performs PXE boot from the primary NIC.
83 - C(floppy), C(cd), C(hdd), C(sd_card) performs boot from their devices respectively.
84 - C(bios_setup) performs boot into the native BIOS setup.
85 - C(utilities) performs boot from the local utilities.
86 - C(uefi_target) performs boot from the UEFI device path found in I(uefi_target_boot_source_override).
87 - If the I(boot_source_override_target) is set to a value other than C(none) then the
88 I(boot_source_override_enabled) is automatically set to C(once) if I(boot_source_override_enabled) is not provided.
89 - Changes to this options do not alter the BIOS persistent boot order configuration.
90 - This is required if I(boot_source_override_enabled) is C(once) or C(continuous) for iDRAC 17G and later.
91 - This is not idempotent for iDRAC 17G and later.
92 - This is mutually exclusive with I(boot_options).
93 choices: [uefi_http, sd_card, uefi_target, utilities, bios_setup, hdd, cd, floppy, pxe, none]
94 uefi_target_boot_source_override:
95 type: str
96 description:
97 - The UEFI device path of the device from which to boot when I(boot_source_override_target) is C(uefi_target).
98 - I(boot_source_override_enabled) cannot be set to c(continuous) if I(boot_source_override_target)
99 set to C(uefi_target) because this settings is defined in UEFI as a one-time-boot setting.
100 - Changes to this options do not alter the BIOS persistent boot order configuration.
101 - This is required if I(boot_source_override_target) is C(uefi_target).
102 - This is mutually exclusive with I(boot_options).
103 reset_type:
104 type: str
105 description:
106 - C(none) Host system is not rebooted and I(job_wait) is not applicable.
107 - C(force_restart) Forcefully reboot the Host system.
108 - C(graceful_restart) Gracefully reboot the Host system.
109 choices: [graceful_restart, force_restart, none]
110 default: graceful_restart
111 job_wait:
112 type: bool
113 description:
114 - Provides the option to wait for job completion.
115 - This is applicable when I(reset_type) is C(force_reset) or C(graceful_reset).
116 default: true
117 job_wait_timeout:
118 type: int
119 description:
120 - The maximum wait time of I(job_wait) in seconds. The job is tracked only for this duration.
121 - This option is applicable when I(job_wait) is C(true).
122 default: 900
123 resource_id:
124 type: str
125 description: Redfish ID of the resource.
126 requirements:
127 - "python >= 3.9.6"
128 author:
129 - "Felix Stephen (@felixs88)"
130 - "Abhishek Sinha (@ABHISHEK-SINHA10)"
131 notes:
132 - Run this module from a system that has direct access to Dell iDRAC.
133 - This module supports C(check_mode).
134 """
135
136
137 EXAMPLES = """
138 ---
139 - name: Configure the system boot options settings.
140 dellemc.openmanage.idrac_boot:
141 idrac_ip: "192.168.0.1"
142 idrac_user: "user_name"
143 idrac_password: "user_password"
144 ca_path: "/path/to/ca_cert.pem"
145 boot_options:
146 - display_name: Hard drive C
147 enabled: true
148 - boot_option_reference: NIC.PxeDevice.2-1
149 enabled: true
150
151 - name: Configure the boot order settings.
152 dellemc.openmanage.idrac_boot:
153 idrac_ip: "192.168.0.1"
154 idrac_user: "user_name"
155 idrac_password: "user_password"
156 ca_path: "/path/to/ca_cert.pem"
157 boot_order:
158 - Boot0001
159 - Boot0002
160 - Boot0004
161 - Boot0003
162
163 - name: Configure the boot source override mode.
164 dellemc.openmanage.idrac_boot:
165 idrac_ip: "192.168.0.1"
166 idrac_user: "user_name"
167 idrac_password: "user_password"
168 ca_path: "/path/to/ca_cert.pem"
169 boot_source_override_mode: legacy
170 boot_source_override_target: cd
171 boot_source_override_enabled: once
172
173 - name: Configure the UEFI target settings.
174 dellemc.openmanage.idrac_boot:
175 idrac_ip: "192.168.0.1"
176 idrac_user: "user_name"
177 idrac_password: "user_password"
178 ca_path: "/path/to/ca_cert.pem"
179 boot_source_override_mode: uefi
180 boot_source_override_target: uefi_target
181 uefi_target_boot_source_override: "VenHw(3A191845-5F86-4E78-8FCE-C4CFF59F9DAA)"
182
183 - name: Configure the boot source override mode as pxe.
184 dellemc.openmanage.idrac_boot:
185 idrac_ip: "192.168.0.1"
186 idrac_user: "user_name"
187 idrac_password: "user_password"
188 ca_path: "/path/to/ca_cert.pem"
189 boot_source_override_mode: legacy
190 boot_source_override_target: pxe
191 boot_source_override_enabled: continuous
192 """
193
194
195 RETURN = r'''
196 ---
197 msg:
198 description: Successfully updated the boot settings.
199 returned: success
200 type: str
201 sample: Successfully updated the boot settings.
202 job:
203 description: Configured job details.
204 returned: success
205 type: dict
206 sample: {
207 "ActualRunningStartTime": "2019-06-19T00:57:24",
208 "ActualRunningStopTime": "2019-06-19T01:00:27",
209 "CompletionTime": "2019-06-19T01:00:27",
210 "Description": "Job Instance",
211 "EndTime": "TIME_NA",
212 "Id": "JID_609237056489",
213 "JobState": "Completed",
214 "JobType": "BIOSConfiguration",
215 "Message": "Job completed successfully.",
216 "MessageArgs": [],
217 "MessageId": "PR19",
218 "Name": "Configure: BIOS.Setup.1-1",
219 "PercentComplete": 100,
220 "StartTime": "2019-06-19T00:55:05",
221 "TargetSettingsURI": null }
222 boot:
223 description: Configured boot settings details.
224 returned: success
225 type: dict
226 sample: {
227 "BootOptions": {
228 "Description": "Collection of BootOptions",
229 "Members": [{
230 "BootOptionEnabled": false,
231 "BootOptionReference": "HardDisk.List.1-1",
232 "Description": "Current settings of the Legacy Boot option",
233 "DisplayName": "Hard drive C:",
234 "Id": "HardDisk.List.1-1",
235 "Name": "Legacy Boot option",
236 "UefiDevicePath": "VenHw(D6C0639F-C705-4EB9-AA4F-5802D8823DE6)"}],
237 "Name": "Boot Options Collection"
238 },
239 "BootOrder": [ "HardDisk.List.1-1"],
240 "BootSourceOverrideEnabled": "Disabled",
241 "BootSourceOverrideMode": "Legacy",
242 "BootSourceOverrideTarget": "None",
243 "UefiTargetBootSourceOverride": null }
244 error_info:
245 description: Details of the HTTP Error.
246 returned: on HTTP error
247 type: dict
248 sample: {
249 "error": {
250 "code": "Base.1.0.GeneralError",
251 "message": "A general error has occurred. See ExtendedInfo for more information.",
252 "@Message.ExtendedInfo": [
253 {
254 "MessageId": "GEN1234",
255 "RelatedProperties": [],
256 "Message": "Unable to process the request because an error occurred.",
257 "MessageArgs": [],
258 "Severity": "Critical",
259 "Resolution": "Retry the operation. If the issue persists, contact your system administrator."
260 }
261 ]
262 }
263 }
264 '''
265
266
267 import json
268 import time
269 from ansible.module_utils.six.moves.urllib.error import URLError, HTTPError
270 from ansible.module_utils.urls import ConnectionError, SSLValidationError
271 from ansible_collections.dellemc.openmanage.plugins.module_utils.idrac_redfish import iDRACRedfishAPI, IdracAnsibleModule
272 from ansible_collections.dellemc.openmanage.plugins.module_utils.utils import (strip_substr_dict, idrac_system_reset,
273 get_system_res_id,
274 wait_for_idrac_job_completion)
275
276
277 SYSTEM_URI = "/redfish/v1/Systems"
278 BOOT_OPTIONS_URI = "/redfish/v1/Systems/{0}/BootOptions?$expand=*($levels=1)"
279 JOB_URI = "/redfish/v1/Managers/iDRAC.Embedded.1/Oem/Dell/Jobs?$expand=*($levels=1)"
280 JOB_URI_ID = "/redfish/v1/Managers/iDRAC.Embedded.1/Oem/Dell/Jobs/{0}"
281 BOOT_SEQ_URI = "/redfish/v1/Systems/{0}/Oem/Dell/DellBootSources"
282 PATCH_BOOT_SEQ_URI = "/redfish/v1/Systems/{0}/Oem/Dell/DellBootSources/Settings"
283
284 NO_CHANGES_MSG = "No changes found to be applied."
285 CHANGES_MSG = "Changes found to be applied."
286 JOB_EXISTS = "Unable to complete the request because the BIOS configuration job already " \
287 "exists. Wait for the pending job to complete."
288 BOOT_OPT_ERROR_MSG = "{0} boot_options provided."
289 INVALID_BOOT_OPT = "{0} boot order reference provided."
290 SUCCESS_MSG = "Successfully updated the boot settings."
291 FAILED_MSG = "Failed to update the boot settings."
292 UNSUPPORTED_MSG = "The system does not support the BootOptions feature."
293 JOB_WAIT_MSG = "The boot settings operation is triggered/submitted successfully."
294 AUTH_ERROR_MSG = "Unable to communicate with iDRAC {0}. This may be due to one of the following: " \
295 "Incorrect username or password, unreachable iDRAC IP or a failure in TLS/SSL handshake."
296
297 BS_OVERRIDE_MODE = {"legacy": "Legacy", "uefi": "UEFI"}
298 BS_OVERRIDE_ENABLED = {"continuous": "Continuous", "disabled": "Disabled", "once": "Once"}
299 BS_OVERRIDE_TARGET = {"none": "None", "pxe": "Pxe", "floppy": "Floppy", "cd": "Cd",
300 "hdd": "Hdd", "bios_setup": "BiosSetup", "utilities": "Utilities",
301 "uefi_target": "UefiTarget", "sd_card": "SDCard", "uefi_http": "UefiHttp"}
302 RESET_TYPE = {"graceful_restart": "GracefulRestart", "force_restart": "ForceRestart", "none": None}
303
304
305 def get_response_attributes(module, idrac, res_id):
306 resp = idrac.invoke_request("{0}/{1}".format(SYSTEM_URI, res_id), "GET")
307 resp_data = resp.json_data["Boot"]
308 resp_data.pop("Certificates", None)
309 resp_data.pop("[email protected]", None)
310 resp_data.pop("[email protected]", None)
311 if resp_data.get("BootOptions") is None and module.params.get("boot_options") is not None:
312 module.fail_json(msg=UNSUPPORTED_MSG)
313 if resp.json_data.get("Actions") is not None:
314 type_reset = resp.json_data["Actions"]["#ComputerSystem.Reset"]["[email protected]"]
315 if "GracefulRestart" not in type_reset:
316 RESET_TYPE["graceful_restart"] = "ForceRestart"
317 return resp_data
318
319
320 def get_existing_boot_options(idrac, res_id):
321 resp = idrac.invoke_request(BOOT_OPTIONS_URI.format(res_id), "GET")
322 resp_data = strip_substr_dict(resp.json_data)
323 strip_members = []
324 for each in resp_data["Members"]:
325 strip_members.append(strip_substr_dict(each))
326 resp_data["Members"] = strip_members
327 return resp_data
328
329
330 def system_reset(module, idrac, res_id):
331 reset_msg, track_failed, reset, reset_type, job_resp = "", False, True, module.params.get("reset_type"), {}
332 if reset_type is not None and not reset_type == "none":
333 data = {"ResetType": RESET_TYPE[reset_type]}
334 job_wait_timeout = module.params.get("job_wait_timeout")
335 reset, track_failed, reset_msg, job_resp = idrac_system_reset(idrac, res_id, payload=data, job_wait=True,
336 wait_time_sec=job_wait_timeout)
337 if RESET_TYPE["graceful_restart"] == "ForceRestart":
338 reset = True
339 if reset_type == "force_restart" and RESET_TYPE["graceful_restart"] == "GracefulRestart":
340 reset = True
341 return reset, track_failed, reset_msg, job_resp
342
343
344 def get_scheduled_job(idrac, job_state=None):
345 if job_state is None:
346 job_state = ["Scheduled", "New", "Running"]
347 is_job, job_type_name, progress_job = False, "BIOSConfiguration", []
348 time.sleep(10)
349 job_resp = idrac.invoke_request(JOB_URI, "GET")
350 job_resp_member = job_resp.json_data["Members"]
351 if job_resp_member:
352 bios_config_job = list(filter(lambda d: d.get("JobType") in [job_type_name], job_resp_member))
353 progress_job = list(filter(lambda d: d.get("JobState") in job_state, bios_config_job))
354 if progress_job:
355 is_job = True
356 return is_job, progress_job
357
358
359 def configure_boot_options(module, idrac, res_id, payload):
360 is_job, progress_job = get_scheduled_job(idrac)
361 job_data, job_wait = {}, module.params["job_wait"]
362 resp_data = get_response_attributes(module, idrac, res_id)
363 override_mode = resp_data["BootSourceOverrideMode"]
364 if module.params["reset_type"] == "none":
365 job_wait = False
366 if is_job:
367 module.fail_json(msg=JOB_EXISTS)
368 boot_seq_resp = idrac.invoke_request(BOOT_SEQ_URI.format(res_id), "GET")
369 seq_key = "BootSeq" if override_mode == "Legacy" else "UefiBootSeq"
370 boot_seq_data = boot_seq_resp.json_data["Attributes"][seq_key]
371 [each.update({"Enabled": payload.get(each["Name"])}
372 ) for each in boot_seq_data if payload.get(each["Name"]) is not None]
373 seq_payload = {"Attributes": {seq_key: boot_seq_data}, "@Redfish.SettingsApplyTime": {"ApplyTime": "OnReset"}}
374 seq_payload = _update_seq_payload(payload, resp_data, seq_key, boot_seq_data, seq_payload)
375 resp = idrac.invoke_request(PATCH_BOOT_SEQ_URI.format(res_id), "PATCH", data=seq_payload)
376 if resp.status_code in [200, 202]:
377 location = resp.headers["Location"]
378 job_id = location.split("/")[-1]
379 reset, track_failed, reset_msg, reset_job_resp = system_reset(module, idrac, res_id)
380 if reset_job_resp:
381 job_data = reset_job_resp.json_data
382 if reset:
383 job_resp, error_msg = wait_for_idrac_job_completion(idrac, JOB_URI_ID.format(job_id),
384 job_wait=job_wait,
385 wait_timeout=module.params["job_wait_timeout"])
386 if error_msg:
387 module.fail_json(msg=error_msg)
388 job_data = job_resp.json_data
389 else:
390 module.fail_json(msg=reset_msg)
391 return job_data
392
393
394 def _update_seq_payload(payload, resp_data, seq_key, boot_seq_data, seq_payload):
395 if seq_key == "UefiBootSeq":
396 for i in range(len(boot_seq_data)):
397 if payload.get(resp_data["BootOrder"][i]) is not None:
398 boot_seq_data[i].update({"Enabled": payload.get(resp_data["BootOrder"][i])})
399 seq_payload["Attributes"][seq_key] = boot_seq_data
400 return seq_payload
401
402
403 def apply_boot_settings(module, idrac, payload, res_id, generation):
404 job_data, job_wait = {}, module.params["job_wait"]
405 if module.params["reset_type"] == "none":
406 job_wait = False
407 system_uri = f"{SYSTEM_URI}/{res_id}"
408 BOOT_SETTINGS_URI_LOGIC = system_uri if generation < 17 else system_uri + "/Settings"
409 resp = idrac.invoke_request(BOOT_SETTINGS_URI_LOGIC, "PATCH", data=payload)
410 if resp.status_code in [200, 202]:
411 reset, track_failed, reset_msg, reset_job_resp = system_reset(module, idrac, res_id)
412 if reset_job_resp:
413 job_data = reset_job_resp.json_data
414 is_job, progress_job = get_scheduled_job(idrac)
415 if is_job:
416 if reset:
417 job_resp, error_msg = wait_for_idrac_job_completion(idrac, JOB_URI_ID.format(progress_job[0]["Id"]),
418 job_wait=job_wait,
419 wait_timeout=module.params["job_wait_timeout"])
420 if error_msg:
421 module.fail_json(msg=error_msg)
422 job_data = job_resp.json_data
423 else:
424 module.fail_json(msg=reset_msg)
425 return job_data
426
427
428 def configure_boot_settings(module, idrac, res_id):
429 job_resp, payload = {}, {"Boot": {}}
430 boot_order = module.params.get("boot_order")
431 override_mode = module.params.get("boot_source_override_mode")
432 override_enabled = module.params.get("boot_source_override_enabled")
433 override_target = module.params.get("boot_source_override_target")
434 response = get_response_attributes(module, idrac, res_id)
435 payload["Boot"].update(configure_boot_order(module, boot_order, response))
436 gen = idrac.get_server_generation
437 generation = gen[0]
438 payload["Boot"].update(checking_all_conditions_for_boot_settings(module, override_mode,
439 override_enabled, override_target,
440 response, generation))
441 if module.check_mode and payload["Boot"]:
442 module.exit_json(msg=CHANGES_MSG, changed=True)
443 elif (module.check_mode or not module.check_mode) and not payload["Boot"]:
444 module.exit_json(msg=NO_CHANGES_MSG)
445 else:
446 job_resp = apply_boot_settings(module, idrac, payload, res_id, generation)
447 return job_resp
448
449
450 def checking_all_conditions_for_boot_settings(module, override_mode, override_enabled, override_target, response, gen):
451 payload = {}
452 if override_mode is not None and \
453 (BS_OVERRIDE_MODE.get(override_mode) != response.get("BootSourceOverrideMode")):
454 payload.update({"BootSourceOverrideMode": BS_OVERRIDE_MODE.get(override_mode)})
455 if override_enabled is not None and \
456 (BS_OVERRIDE_ENABLED.get(override_enabled) != response.get("BootSourceOverrideEnabled")):
457 payload.update({"BootSourceOverrideEnabled": BS_OVERRIDE_ENABLED.get(override_enabled)})
458 if override_target is not None and \
459 (BS_OVERRIDE_TARGET.get(override_target) != response.get("BootSourceOverrideTarget")
460 or (override_enabled != "disabled" and gen >= 17)):
461 payload.update({"BootSourceOverrideTarget": BS_OVERRIDE_TARGET.get(override_target)})
462 uefi_override_target = module.params.get("uefi_target_boot_source_override")
463 if override_target == "uefi_target" and uefi_override_target != response.get("UefiTargetBootSourceOverride"):
464 payload.update({"UefiTargetBootSourceOverride": uefi_override_target})
465 return payload
466
467
468 def configure_boot_order(module, boot_order, response):
469 if boot_order is not None:
470 exist_boot_order = response.get("BootOrder")
471 invalid_boot_order = [bo for bo in boot_order if bo not in exist_boot_order]
472 if invalid_boot_order:
473 module.fail_json(msg=INVALID_BOOT_OPT.format("Invalid"), invalid_boot_order=invalid_boot_order)
474 if not len(set(boot_order)) == len(boot_order):
475 dup_order = boot_order[:]
476 [dup_order.remove(bo) for bo in exist_boot_order if bo in dup_order]
477 module.fail_json(msg=INVALID_BOOT_OPT.format("Duplicate"),
478 duplicate_boot_order=dup_order)
479 if not len(boot_order) == len(exist_boot_order):
480 module.fail_json(msg="Unable to complete the operation because all boot devices "
481 "are required for this operation.")
482 if not boot_order == exist_boot_order:
483 return {"BootOrder": boot_order}
484 return {}
485
486
487 def configure_idrac_boot(module, idrac, res_id):
488 boot_options = module.params.get("boot_options")
489 inv_boot_options, diff_change, payload, job_resp = [], [], {}, {}
490 if boot_options is not None:
491 boot_option_data = get_existing_boot_options(idrac, res_id)
492 for each in boot_options:
493 attr_val = each["display_name"] if each.get("display_name") is not None else each.get("boot_option_reference")
494 attr_key = "DisplayName" if each.get("display_name") is not None else "BootOptionReference"
495 report = list(filter(lambda d: d[attr_key] in [attr_val], boot_option_data["Members"]))
496 if not report:
497 inv_boot_options.append(each)
498 else:
499 act_val = {"BootOptionEnabled": each["enabled"]}
500 ext_val = {"BootOptionEnabled": report[0]["BootOptionEnabled"]}
501 diff_change.append(bool(set(ext_val.items()) ^ set(act_val.items())))
502 payload[report[0]["Id"]] = each["enabled"]
503 if inv_boot_options:
504 module.fail_json(msg=BOOT_OPT_ERROR_MSG.format("Invalid"), invalid_boot_options=inv_boot_options)
505 if not len(payload) == len(boot_options):
506 module.fail_json(msg=BOOT_OPT_ERROR_MSG.format("Duplicate"), duplicate_boot_options=boot_options)
507 if module.check_mode and any(diff_change) is True:
508 module.exit_json(msg=CHANGES_MSG, changed=True)
509 elif (module.check_mode and all(diff_change) is False) or (not module.check_mode and not any(diff_change)):
510 module.exit_json(msg=NO_CHANGES_MSG)
511 else:
512 job_resp = configure_boot_options(module, idrac, res_id, payload)
513 else:
514 job_resp = configure_boot_settings(module, idrac, res_id)
515 return job_resp
516
517
518 def main():
519 specs = {
520 "boot_options": {
521 "required": False, "type": "list", "elements": "dict",
522 "options": {
523 "boot_option_reference": {"required": False, "type": "str"},
524 "display_name": {"required": False, "type": "str"},
525 "enabled": {"required": True, "type": "bool"},
526 },
527 "mutually_exclusive": [("boot_option_reference", "display_name")],
528 "required_one_of": [("boot_option_reference", "display_name")],
529 },
530 "boot_order": {"required": False, "type": "list", "elements": "str"},
531 "boot_source_override_mode": {"required": False, "type": "str", "choices": ["legacy", "uefi"]},
532 "boot_source_override_enabled": {"required": False, "type": "str",
533 "choices": ["continuous", "disabled", "once"]},
534 "boot_source_override_target": {"required": False, "type": "str",
535 "choices": ["uefi_http", "sd_card", "uefi_target", "utilities", "bios_setup",
536 "hdd", "cd", "floppy", "pxe", "none"]},
537 "uefi_target_boot_source_override": {"required": False, "type": "str"},
538 "reset_type": {"required": False, "type": "str", "default": "graceful_restart",
539 "choices": ["graceful_restart", "force_restart", "none"]},
540 "job_wait": {"required": False, "type": "bool", "default": True},
541 "job_wait_timeout": {"required": False, "type": "int", "default": 900},
542 "resource_id": {"required": False, "type": "str"}
543 }
544
545 module = IdracAnsibleModule(
546 argument_spec=specs,
547 required_one_of=[["boot_options", "boot_order", "boot_source_override_mode",
548 "boot_source_override_enabled", "boot_source_override_target",
549 "uefi_target_boot_source_override"]],
550 mutually_exclusive=[
551 ("boot_options", "boot_order"), ("boot_options", "boot_source_override_mode"),
552 ("boot_options", "boot_source_override_enabled"), ("boot_options", "boot_source_override_target"),
553 ("boot_options", "uefi_target_boot_source_override")
554 ],
555 required_if=[
556 ["boot_source_override_target", "uefi_target", ("uefi_target_boot_source_override",)],
557 ],
558 supports_check_mode=True,
559 )
560 try:
561 with iDRACRedfishAPI(module.params, req_session=True) as idrac:
562 res_id = module.params.get("resource_id")
563 if not res_id:
564 res_id, error_msg = get_system_res_id(idrac)
565 if error_msg:
566 module.fail_json(msg=error_msg)
567 job_resp = configure_idrac_boot(module, idrac, res_id)
568 job_resp_data = strip_substr_dict(job_resp)
569 boot_option_data = get_existing_boot_options(idrac, res_id)
570 boot_attr = get_response_attributes(module, idrac, res_id)
571 boot_attr["BootOptions"] = boot_option_data
572 if job_resp_data and \
573 (job_resp_data.get("JobState") in ["Failed", "RebootFailed"] or
574 "failed" in job_resp_data.get("Message").lower()):
575 module.fail_json(msg=FAILED_MSG, job=job_resp_data)
576 if (not module.params["job_wait"] or module.params["reset_type"] == "none") and \
577 not job_resp_data.get("JobState") == "RebootCompleted":
578 module.exit_json(msg=JOB_WAIT_MSG, job=job_resp_data, boot=boot_attr)
579 module.exit_json(msg=SUCCESS_MSG, job=job_resp_data, boot=boot_attr, changed=True)
580 except HTTPError as err:
581 if err.code == 401:
582 module.fail_json(msg=AUTH_ERROR_MSG.format(module.params["idrac_ip"]))
583 module.exit_json(msg=str(err), error_info=json.load(err), failed=True)
584 except URLError:
585 module.exit_json(msg=AUTH_ERROR_MSG.format(module.params["idrac_ip"]), unreachable=True)
586 except (ImportError, ValueError, RuntimeError, SSLValidationError,
587 ConnectionError, KeyError, TypeError, IndexError) as e:
588 module.fail_json(msg=str(e))
589
590
591 if __name__ == '__main__':
592 main()