Debian Codegraph @

ansible_collections/community/general/plugins/modules/redfish_config.py

raw
1 #!/usr/bin/python
2
3 # Copyright (c) 2017-2018 Dell EMC Inc.
4 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
5 # SPDX-License-Identifier: GPL-3.0-or-later
6
7 from __future__ import annotations
8
9 DOCUMENTATION = r"""
10 module: redfish_config
11 short_description: Manages Out-Of-Band controllers using Redfish APIs
12 description:
13 - Builds Redfish URIs locally and sends them to remote OOB controllers to set or update a configuration attribute.
14 - Manages BIOS configuration settings.
15 - Manages OOB controller configuration settings.
16 extends_documentation_fragment:
17 - community.general._attributes
18 - community.general._redfish
19 attributes:
20 check_mode:
21 support: none
22 diff_mode:
23 support: none
24 options:
25 category:
26 required: true
27 description:
28 - Category to execute on OOB controller.
29 type: str
30 command:
31 required: true
32 description:
33 - List of commands to execute on OOB controller.
34 type: list
35 elements: str
36 baseuri:
37 required: true
38 description:
39 - Base URI of OOB controller.
40 type: str
41 username:
42 description:
43 - Username for authenticating to OOB controller.
44 type: str
45 password:
46 description:
47 - Password for authenticating to OOB controller.
48 type: str
49 auth_token:
50 description:
51 - Security token for authenticating to OOB controller.
52 type: str
53 version_added: 2.3.0
54 bios_attributes:
55 description:
56 - Dictionary of BIOS attributes to update.
57 default: {}
58 type: dict
59 version_added: '0.2.0'
60 timeout:
61 description:
62 - Timeout in seconds for HTTP requests to OOB controller.
63 - The default value for this parameter changed from V(10) to V(60) in community.general 9.0.0.
64 type: int
65 default: 60
66 boot_order:
67 description:
68 - List of BootOptionReference strings specifying the BootOrder.
69 default: []
70 type: list
71 elements: str
72 version_added: '0.2.0'
73 network_protocols:
74 description:
75 - Setting dict of manager services to update.
76 type: dict
77 default: {}
78 version_added: '0.2.0'
79 resource_id:
80 description:
81 - ID of the System, Manager or Chassis to modify.
82 type: str
83 version_added: '0.2.0'
84 service_id:
85 description:
86 - ID of the manager to update.
87 type: str
88 version_added: '8.4.0'
89 nic_addr:
90 description:
91 - EthernetInterface Address string on OOB controller.
92 default: 'null'
93 type: str
94 version_added: '0.2.0'
95 nic_config:
96 description:
97 - Setting dict of EthernetInterface on OOB controller.
98 type: dict
99 default: {}
100 version_added: '0.2.0'
101 strip_etag_quotes:
102 description:
103 - Removes surrounding quotes of etag used in C(If-Match) header of C(PATCH) requests.
104 - Only use this option to resolve bad vendor implementation where C(If-Match) only matches the unquoted etag string.
105 type: bool
106 default: false
107 version_added: 3.7.0
108 hostinterface_config:
109 description:
110 - Setting dict of HostInterface on OOB controller.
111 type: dict
112 default: {}
113 version_added: '4.1.0'
114 hostinterface_id:
115 description:
116 - Redfish HostInterface instance ID if multiple HostInterfaces are present.
117 type: str
118 version_added: '4.1.0'
119 sessions_config:
120 description:
121 - Setting dict of Sessions.
122 type: dict
123 default: {}
124 version_added: '5.7.0'
125 storage_subsystem_id:
126 description:
127 - ID of the Storage Subsystem on which the volume is to be created.
128 type: str
129 default: ''
130 version_added: '7.3.0'
131 storage_none_volume_deletion:
132 description:
133 - Indicates if all non-RAID volumes are automatically deleted prior to creating the new volume.
134 type: bool
135 default: false
136 version_added: '9.5.0'
137 volume_ids:
138 description:
139 - List of IDs of volumes to be deleted.
140 type: list
141 default: []
142 elements: str
143 version_added: '7.3.0'
144 secure_boot_enable:
145 description:
146 - Setting parameter to enable or disable SecureBoot.
147 type: bool
148 default: true
149 version_added: '7.5.0'
150 volume_details:
151 description:
152 - Setting dictionary of volume to be created.
153 - If C(CapacityBytes) key is not specified in this dictionary, the size of the volume is determined by the Redfish service.
154 It is possible the size is not the maximum available size.
155 type: dict
156 default: {}
157 version_added: '7.5.0'
158 power_restore_policy:
159 description:
160 - The desired power state of the system when power is restored after a power loss.
161 type: str
162 choices:
163 - AlwaysOn
164 - AlwaysOff
165 - LastState
166 version_added: '10.5.0'
167 ciphers:
168 version_added: 9.2.0
169 validate_certs:
170 version_added: 10.6.0
171 ca_path:
172 version_added: 10.6.0
173
174 author:
175 - "Jose Delarosa (@jose-delarosa)"
176 - "T S Kushal (@TSKushal)"
177 """
178
179 EXAMPLES = r"""
180 - name: Set BootMode to UEFI
181 community.general.redfish_config:
182 category: Systems
183 command: SetBiosAttributes
184 resource_id: 437XR1138R2
185 bios_attributes:
186 BootMode: "Uefi"
187 baseuri: "{{ baseuri }}"
188 username: "{{ username }}"
189 password: "{{ password }}"
190
191 - name: Set multiple BootMode attributes
192 community.general.redfish_config:
193 category: Systems
194 command: SetBiosAttributes
195 resource_id: 437XR1138R2
196 bios_attributes:
197 BootMode: "Bios"
198 OneTimeBootMode: "Enabled"
199 BootSeqRetry: "Enabled"
200 baseuri: "{{ baseuri }}"
201 username: "{{ username }}"
202 password: "{{ password }}"
203
204 - name: Enable PXE Boot for NIC1
205 community.general.redfish_config:
206 category: Systems
207 command: SetBiosAttributes
208 resource_id: 437XR1138R2
209 bios_attributes:
210 PxeDev1EnDis: Enabled
211 baseuri: "{{ baseuri }}"
212 username: "{{ username }}"
213 password: "{{ password }}"
214
215 - name: Set BIOS default settings with a timeout of 20 seconds
216 community.general.redfish_config:
217 category: Systems
218 command: SetBiosDefaultSettings
219 resource_id: 437XR1138R2
220 baseuri: "{{ baseuri }}"
221 username: "{{ username }}"
222 password: "{{ password }}"
223 timeout: 20
224
225 - name: Set boot order
226 community.general.redfish_config:
227 category: Systems
228 command: SetBootOrder
229 boot_order:
230 - Boot0002
231 - Boot0001
232 - Boot0000
233 - Boot0003
234 - Boot0004
235 baseuri: "{{ baseuri }}"
236 username: "{{ username }}"
237 password: "{{ password }}"
238
239 - name: Set boot order to the default
240 community.general.redfish_config:
241 category: Systems
242 command: SetDefaultBootOrder
243 baseuri: "{{ baseuri }}"
244 username: "{{ username }}"
245 password: "{{ password }}"
246
247 - name: Set Manager Network Protocols
248 community.general.redfish_config:
249 category: Manager
250 command: SetNetworkProtocols
251 network_protocols:
252 SNMP:
253 ProtocolEnabled: true
254 Port: 161
255 HTTP:
256 ProtocolEnabled: false
257 Port: 8080
258 baseuri: "{{ baseuri }}"
259 username: "{{ username }}"
260 password: "{{ password }}"
261
262 - name: Set Manager NIC
263 community.general.redfish_config:
264 category: Manager
265 command: SetManagerNic
266 nic_config:
267 DHCPv4:
268 DHCPEnabled: false
269 IPv4StaticAddresses:
270 Address: 192.168.1.3
271 Gateway: 192.168.1.1
272 SubnetMask: 255.255.255.0
273 baseuri: "{{ baseuri }}"
274 username: "{{ username }}"
275 password: "{{ password }}"
276
277 - name: Disable Host Interface
278 community.general.redfish_config:
279 category: Manager
280 command: SetHostInterface
281 hostinterface_config:
282 InterfaceEnabled: false
283 baseuri: "{{ baseuri }}"
284 username: "{{ username }}"
285 password: "{{ password }}"
286
287 - name: Enable Host Interface for HostInterface resource ID '2'
288 community.general.redfish_config:
289 category: Manager
290 command: SetHostInterface
291 hostinterface_config:
292 InterfaceEnabled: true
293 hostinterface_id: "2"
294 baseuri: "{{ baseuri }}"
295 username: "{{ username }}"
296 password: "{{ password }}"
297
298 - name: Set SessionService Session Timeout to 30 minutes
299 community.general.redfish_config:
300 category: Sessions
301 command: SetSessionService
302 sessions_config:
303 SessionTimeout: 1800
304 baseuri: "{{ baseuri }}"
305 username: "{{ username }}"
306 password: "{{ password }}"
307
308 - name: Enable SecureBoot
309 community.general.redfish_config:
310 category: Systems
311 command: EnableSecureBoot
312 baseuri: "{{ baseuri }}"
313 username: "{{ username }}"
314 password: "{{ password }}"
315
316 - name: Set SecureBoot
317 community.general.redfish_config:
318 category: Systems
319 command: SetSecureBoot
320 baseuri: "{{ baseuri }}"
321 username: "{{ username }}"
322 password: "{{ password }}"
323 secure_boot_enable: true
324
325 - name: Delete All Volumes
326 community.general.redfish_config:
327 category: Systems
328 command: DeleteVolumes
329 baseuri: "{{ baseuri }}"
330 username: "{{ username }}"
331 password: "{{ password }}"
332 storage_subsystem_id: "DExxxxxx"
333 volume_ids: ["volume1", "volume2"]
334
335 - name: Create Volume
336 community.general.redfish_config:
337 category: Systems
338 command: CreateVolume
339 baseuri: "{{ baseuri }}"
340 username: "{{ username }}"
341 password: "{{ password }}"
342 storage_subsystem_id: "DExxxxxx"
343 volume_details:
344 Name: "MR Volume"
345 RAIDType: "RAID0"
346 Drives:
347 - "/redfish/v1/Systems/1/Storage/DE00B000/Drives/1"
348
349 - name: Set PowerRestorePolicy
350 community.general.redfish_config:
351 category: Systems
352 command: SetPowerRestorePolicy
353 baseuri: "{{ baseuri }}"
354 username: "{{ username }}"
355 password: "{{ password }}"
356 power_restore_policy: "AlwaysOff"
357
358 - name: Set service identification to {{ service_id }}
359 community.general.redfish_config:
360 category: Manager
361 command: SetServiceIdentification
362 service_id: "{{ service_id }}"
363 baseuri: "{{ baseuri }}"
364 username: "{{ username }}"
365 password: "{{ password }}"
366 """
367
368 RETURN = r"""
369 msg:
370 description: Message with action result or error description.
371 returned: always
372 type: str
373 sample: "Action was successful"
374 """
375
376 from ansible.module_utils.basic import AnsibleModule
377 from ansible.module_utils.common.text.converters import to_native
378
379 from ansible_collections.community.general.plugins.module_utils._redfish_utils import (
380 REDFISH_COMMON_ARGUMENT_SPEC,
381 RedfishUtils,
382 )
383
384 # More will be added as module features are expanded
385 CATEGORY_COMMANDS_ALL = {
386 "Systems": [
387 "SetBiosDefaultSettings",
388 "SetBiosAttributes",
389 "SetBootOrder",
390 "SetDefaultBootOrder",
391 "EnableSecureBoot",
392 "SetSecureBoot",
393 "DeleteVolumes",
394 "CreateVolume",
395 "SetPowerRestorePolicy",
396 ],
397 "Manager": ["SetNetworkProtocols", "SetManagerNic", "SetHostInterface", "SetServiceIdentification"],
398 "Sessions": ["SetSessionService"],
399 }
400
401
402 def main():
403 result = {}
404 argument_spec = dict(
405 category=dict(required=True),
406 command=dict(required=True, type="list", elements="str"),
407 baseuri=dict(required=True),
408 username=dict(),
409 password=dict(no_log=True),
410 auth_token=dict(no_log=True),
411 bios_attributes=dict(type="dict", default={}),
412 timeout=dict(type="int", default=60),
413 boot_order=dict(type="list", elements="str", default=[]),
414 network_protocols=dict(type="dict", default={}),
415 resource_id=dict(),
416 service_id=dict(),
417 nic_addr=dict(default="null"),
418 nic_config=dict(type="dict", default={}),
419 strip_etag_quotes=dict(type="bool", default=False),
420 hostinterface_config=dict(type="dict", default={}),
421 hostinterface_id=dict(),
422 sessions_config=dict(type="dict", default={}),
423 storage_subsystem_id=dict(type="str", default=""),
424 storage_none_volume_deletion=dict(type="bool", default=False),
425 volume_ids=dict(type="list", default=[], elements="str"),
426 secure_boot_enable=dict(type="bool", default=True),
427 volume_details=dict(type="dict", default={}),
428 power_restore_policy=dict(choices=["AlwaysOn", "AlwaysOff", "LastState"]),
429 )
430 argument_spec.update(REDFISH_COMMON_ARGUMENT_SPEC)
431 module = AnsibleModule(
432 argument_spec,
433 required_together=[
434 ("username", "password"),
435 ],
436 required_one_of=[
437 ("username", "auth_token"),
438 ],
439 mutually_exclusive=[
440 ("username", "auth_token"),
441 ],
442 supports_check_mode=False,
443 )
444
445 category = module.params["category"]
446 command_list = module.params["command"]
447
448 # admin credentials used for authentication
449 creds = {"user": module.params["username"], "pswd": module.params["password"], "token": module.params["auth_token"]}
450
451 # timeout
452 timeout = module.params["timeout"]
453
454 # BIOS attributes to update
455 bios_attributes = module.params["bios_attributes"]
456
457 # boot order
458 boot_order = module.params["boot_order"]
459
460 # System, Manager or Chassis ID to modify
461 resource_id = module.params["resource_id"]
462
463 # manager nic
464 nic_addr = module.params["nic_addr"]
465 nic_config = module.params["nic_config"]
466
467 # Etag options
468 strip_etag_quotes = module.params["strip_etag_quotes"]
469
470 # HostInterface config options
471 hostinterface_config = module.params["hostinterface_config"]
472
473 # HostInterface instance ID
474 hostinterface_id = module.params["hostinterface_id"]
475
476 # Service Identification
477 service_id = module.params["service_id"]
478
479 # Sessions config options
480 sessions_config = module.params["sessions_config"]
481
482 # Volume deletion options
483 storage_subsystem_id = module.params["storage_subsystem_id"]
484 volume_ids = module.params["volume_ids"]
485
486 # Set SecureBoot options
487 secure_boot_enable = module.params["secure_boot_enable"]
488
489 # Volume creation options
490 volume_details = module.params["volume_details"]
491 storage_subsystem_id = module.params["storage_subsystem_id"]
492 storage_none_volume_deletion = module.params["storage_none_volume_deletion"]
493
494 # Power Restore Policy
495 power_restore_policy = module.params["power_restore_policy"]
496
497 # Build root URI
498 root_uri = f"https://{module.params['baseuri']}"
499 rf_utils = RedfishUtils(
500 creds,
501 root_uri,
502 timeout,
503 module,
504 resource_id=resource_id,
505 data_modification=True,
506 strip_etag_quotes=strip_etag_quotes,
507 )
508
509 # Check that Category is valid
510 if category not in CATEGORY_COMMANDS_ALL:
511 module.fail_json(msg=f"Invalid Category '{category}'. Valid Categories = {list(CATEGORY_COMMANDS_ALL.keys())}")
512
513 # Check that all commands are valid
514 for cmd in command_list:
515 # Fail if even one command given is invalid
516 if cmd not in CATEGORY_COMMANDS_ALL[category]:
517 module.fail_json(msg=f"Invalid Command '{cmd}'. Valid Commands = {CATEGORY_COMMANDS_ALL[category]}")
518
519 # Organize by Categories / Commands
520 if category == "Systems":
521 # execute only if we find a System resource
522 result = rf_utils._find_systems_resource()
523 if result["ret"] is False:
524 module.fail_json(msg=result["msg"])
525
526 for command in command_list:
527 if command == "SetBiosDefaultSettings":
528 result = rf_utils.set_bios_default_settings()
529 elif command == "SetBiosAttributes":
530 result = rf_utils.set_bios_attributes(bios_attributes)
531 elif command == "SetBootOrder":
532 result = rf_utils.set_boot_order(boot_order)
533 elif command == "SetDefaultBootOrder":
534 result = rf_utils.set_default_boot_order()
535 elif command == "EnableSecureBoot":
536 result = rf_utils.enable_secure_boot()
537 elif command == "SetSecureBoot":
538 result = rf_utils.set_secure_boot(secure_boot_enable)
539 elif command == "DeleteVolumes":
540 result = rf_utils.delete_volumes(storage_subsystem_id, volume_ids)
541 elif command == "CreateVolume":
542 result = rf_utils.create_volume(volume_details, storage_subsystem_id, storage_none_volume_deletion)
543 elif command == "SetPowerRestorePolicy":
544 result = rf_utils.set_power_restore_policy(power_restore_policy)
545
546 elif category == "Manager":
547 # execute only if we find a Manager service resource
548 result = rf_utils._find_managers_resource()
549 if result["ret"] is False:
550 module.fail_json(msg=result["msg"])
551
552 for command in command_list:
553 if command == "SetNetworkProtocols":
554 result = rf_utils.set_network_protocols(module.params["network_protocols"])
555 elif command == "SetManagerNic":
556 result = rf_utils.set_manager_nic(nic_addr, nic_config)
557 elif command == "SetHostInterface":
558 result = rf_utils.set_hostinterface_attributes(hostinterface_config, hostinterface_id)
559 elif command == "SetServiceIdentification":
560 result = rf_utils.set_service_identification(service_id)
561
562 elif category == "Sessions":
563 # execute only if we find a Sessions resource
564 result = rf_utils._find_sessionservice_resource()
565 if result["ret"] is False:
566 module.fail_json(msg=result["msg"])
567
568 for command in command_list:
569 if command == "SetSessionService":
570 result = rf_utils.set_session_service(sessions_config)
571
572 # Return data back or fail with proper message
573 if result["ret"] is True:
574 if result.get("warning"):
575 module.warn(to_native(result["warning"]))
576
577 module.exit_json(changed=result["changed"], msg=to_native(result["msg"]))
578 else:
579 module.fail_json(msg=to_native(result["msg"]))
580
581
582 if __name__ == "__main__":
583 main()