Debian Codegraph @

ansible_collections/community/general/plugins/modules/redfish_command.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_command
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 perform an action.
14 - Manages OOB controller ex. reboot, log management.
15 - Manages OOB controller users ex. add, remove, update.
16 - Manages system power ex. on, off, graceful and forced reboot.
17 extends_documentation_fragment:
18 - community.general._attributes
19 - community.general._redfish
20 attributes:
21 check_mode:
22 support: none
23 diff_mode:
24 support: none
25 options:
26 category:
27 required: true
28 description:
29 - Category to execute on OOB controller.
30 type: str
31 command:
32 required: true
33 description:
34 - List of commands to execute on OOB controller.
35 type: list
36 elements: str
37 baseuri:
38 required: true
39 description:
40 - Base URI of OOB controller.
41 type: str
42 username:
43 description:
44 - Username for authenticating to OOB controller.
45 type: str
46 password:
47 description:
48 - Password for authenticating to OOB controller.
49 type: str
50 auth_token:
51 description:
52 - Security token for authenticating to OOB controller.
53 type: str
54 version_added: 2.3.0
55 session_uri:
56 description:
57 - URI of the session resource.
58 type: str
59 version_added: 2.3.0
60 id:
61 aliases: [account_id]
62 description:
63 - ID of account to delete/modify.
64 - Can also be used in account creation to work around vendor issues where the ID of the new user is required in the
65 POST request.
66 type: str
67 new_username:
68 aliases: [account_username]
69 description:
70 - Username of account to add/delete/modify.
71 type: str
72 new_password:
73 aliases: [account_password]
74 description:
75 - New password of account to add/modify.
76 type: str
77 roleid:
78 aliases: [account_roleid]
79 description:
80 - Role of account to add/modify.
81 type: str
82 account_types:
83 aliases: [account_accounttypes]
84 description:
85 - Array of account types to apply to a user account.
86 type: list
87 elements: str
88 version_added: '7.2.0'
89 oem_account_types:
90 aliases: [account_oemaccounttypes]
91 description:
92 - Array of OEM account types to apply to a user account.
93 type: list
94 elements: str
95 version_added: '7.2.0'
96 bootdevice:
97 description:
98 - Boot device when setting boot configuration.
99 type: str
100 timeout:
101 description:
102 - Timeout in seconds for HTTP requests to OOB controller.
103 - The default value for this parameter changed from V(10) to V(60) in community.general 9.0.0.
104 type: int
105 default: 60
106 boot_override_mode:
107 description:
108 - Boot mode when using an override.
109 type: str
110 choices: [Legacy, UEFI]
111 version_added: 3.5.0
112 uefi_target:
113 description:
114 - UEFI boot target when bootdevice is "UefiTarget".
115 type: str
116 boot_next:
117 description:
118 - BootNext target when bootdevice is "UefiBootNext".
119 type: str
120 update_username:
121 aliases: [account_updatename]
122 description:
123 - New user name for updating account_username.
124 type: str
125 version_added: '0.2.0'
126 account_properties:
127 description:
128 - Properties of account service to update.
129 type: dict
130 default: {}
131 version_added: '0.2.0'
132 resource_id:
133 description:
134 - ID of the System, Manager or Chassis to modify.
135 type: str
136 version_added: '0.2.0'
137 update_image_uri:
138 description:
139 - URI of the image for the update.
140 type: str
141 version_added: '0.2.0'
142 update_image_file:
143 description:
144 - Filename, with optional path, of the image for the update.
145 type: path
146 version_added: '7.1.0'
147 update_protocol:
148 description:
149 - Protocol for the update.
150 type: str
151 version_added: '0.2.0'
152 update_targets:
153 description:
154 - List of target resource URIs to apply the update to.
155 type: list
156 elements: str
157 default: []
158 version_added: '0.2.0'
159 update_creds:
160 description:
161 - Credentials for retrieving the update image.
162 type: dict
163 version_added: '0.2.0'
164 suboptions:
165 username:
166 description:
167 - Username for retrieving the update image.
168 type: str
169 password:
170 description:
171 - Password for retrieving the update image.
172 type: str
173 update_apply_time:
174 description:
175 - Time when to apply the update.
176 type: str
177 choices:
178 - Immediate
179 - OnReset
180 - AtMaintenanceWindowStart
181 - InMaintenanceWindowOnReset
182 - OnStartUpdateRequest
183 version_added: '6.1.0'
184 update_oem_params:
185 description:
186 - Properties for HTTP Multipart Push Updates.
187 type: dict
188 version_added: '7.5.0'
189 update_handle:
190 description:
191 - Handle to check the status of an update in progress.
192 type: str
193 version_added: '6.1.0'
194 update_custom_oem_header:
195 description:
196 - Optional OEM header, sent as separate form-data for the Multipart HTTP push update.
197 - The header shall start with "Oem" according to DMTF Redfish spec 12.6.2.2.
198 - For more details, see U(https://www.dmtf.org/sites/default/files/standards/documents/DSP0266_1.21.0.html).
199 - If set, then O(update_custom_oem_params) is required too.
200 type: str
201 version_added: '10.1.0'
202 update_custom_oem_params:
203 description:
204 - Custom OEM properties for HTTP Multipart Push updates.
205 - If set, then O(update_custom_oem_header) is required too.
206 - The properties are passed raw without any validation or conversion by Ansible. This means the content can be a file,
207 a string, or any other data. If the content is a dictionary that should be converted to JSON, then the content must
208 be converted to JSON before passing it to this module using the P(ansible.builtin.to_json#filter) filter.
209 type: raw
210 version_added: '10.1.0'
211 update_custom_oem_mime_type:
212 description:
213 - MIME Type for custom OEM properties for HTTP Multipart Push updates.
214 type: str
215 version_added: '10.1.0'
216 virtual_media:
217 description:
218 - Options for VirtualMedia commands.
219 type: dict
220 version_added: '0.2.0'
221 suboptions:
222 media_types:
223 description:
224 - List of media types appropriate for the image.
225 type: list
226 elements: str
227 default: []
228 image_url:
229 description:
230 - URL of the image to insert or eject.
231 type: str
232 inserted:
233 description:
234 - Indicates that the image is treated as inserted on command completion.
235 type: bool
236 default: true
237 write_protected:
238 description:
239 - Indicates that the media is treated as write-protected.
240 type: bool
241 default: true
242 username:
243 description:
244 - Username for accessing the image URL.
245 type: str
246 password:
247 description:
248 - Password for accessing the image URL.
249 type: str
250 transfer_protocol_type:
251 description:
252 - Network protocol to use with the image.
253 type: str
254 transfer_method:
255 description:
256 - Transfer method to use with the image.
257 type: str
258 strip_etag_quotes:
259 description:
260 - Removes surrounding quotes of etag used in C(If-Match) header of C(PATCH) requests.
261 - Only use this option to resolve bad vendor implementation where C(If-Match) only matches the unquoted etag string.
262 type: bool
263 default: false
264 version_added: 3.7.0
265 bios_attributes:
266 description:
267 - BIOS attributes that needs to be verified in the given server.
268 type: dict
269 version_added: 6.4.0
270 reset_to_defaults_mode:
271 description:
272 - Mode to apply when reseting to default.
273 type: str
274 choices: [ResetAll, PreserveNetworkAndUsers, PreserveNetwork]
275 version_added: 8.6.0
276 wait:
277 description:
278 - Block until the service is ready again.
279 type: bool
280 default: false
281 version_added: 9.1.0
282 wait_timeout:
283 description:
284 - How long to block until the service is ready again before giving up.
285 type: int
286 default: 120
287 version_added: 9.1.0
288 ciphers:
289 version_added: 9.2.0
290 validate_certs:
291 version_added: 10.6.0
292 ca_path:
293 version_added: 10.6.0
294
295 author:
296 - "Jose Delarosa (@jose-delarosa)"
297 - "T S Kushal (@TSKushal)"
298 """
299
300 EXAMPLES = r"""
301 - name: Restart system power gracefully
302 community.general.redfish_command:
303 category: Systems
304 command: PowerGracefulRestart
305 resource_id: 437XR1138R2
306 baseuri: "{{ baseuri }}"
307 username: "{{ username }}"
308 password: "{{ password }}"
309
310 - name: Turn system power off
311 community.general.redfish_command:
312 category: Systems
313 command: PowerForceOff
314 resource_id: 437XR1138R2
315
316 - name: Restart system power forcefully
317 community.general.redfish_command:
318 category: Systems
319 command: PowerForceRestart
320 resource_id: 437XR1138R2
321
322 - name: Shutdown system power gracefully
323 community.general.redfish_command:
324 category: Systems
325 command: PowerGracefulShutdown
326 resource_id: 437XR1138R2
327
328 - name: Turn system power on
329 community.general.redfish_command:
330 category: Systems
331 command: PowerOn
332 resource_id: 437XR1138R2
333
334 - name: Reboot system power
335 community.general.redfish_command:
336 category: Systems
337 command: PowerReboot
338 resource_id: 437XR1138R2
339
340 - name: Set one-time boot device to {{ bootdevice }}
341 community.general.redfish_command:
342 category: Systems
343 command: SetOneTimeBoot
344 resource_id: 437XR1138R2
345 bootdevice: "{{ bootdevice }}"
346 baseuri: "{{ baseuri }}"
347 username: "{{ username }}"
348 password: "{{ password }}"
349
350 - name: Set one-time boot device to UefiTarget of "/0x31/0x33/0x01/0x01"
351 community.general.redfish_command:
352 category: Systems
353 command: SetOneTimeBoot
354 resource_id: 437XR1138R2
355 bootdevice: "UefiTarget"
356 uefi_target: "/0x31/0x33/0x01/0x01"
357 baseuri: "{{ baseuri }}"
358 username: "{{ username }}"
359 password: "{{ password }}"
360
361 - name: Set one-time boot device to BootNext target of "Boot0001"
362 community.general.redfish_command:
363 category: Systems
364 command: SetOneTimeBoot
365 resource_id: 437XR1138R2
366 bootdevice: "UefiBootNext"
367 boot_next: "Boot0001"
368 baseuri: "{{ baseuri }}"
369 username: "{{ username }}"
370 password: "{{ password }}"
371
372 - name: Set persistent boot device override
373 community.general.redfish_command:
374 category: Systems
375 command: EnableContinuousBootOverride
376 resource_id: 437XR1138R2
377 bootdevice: "{{ bootdevice }}"
378 baseuri: "{{ baseuri }}"
379 username: "{{ username }}"
380 password: "{{ password }}"
381
382 - name: Set one-time boot to BiosSetup
383 community.general.redfish_command:
384 category: Systems
385 command: SetOneTimeBoot
386 boot_next: BiosSetup
387 boot_override_mode: Legacy
388 baseuri: "{{ baseuri }}"
389 username: "{{ username }}"
390 password: "{{ password }}"
391
392 - name: Disable persistent boot device override
393 community.general.redfish_command:
394 category: Systems
395 command: DisableBootOverride
396
397 - name: Set system indicator LED to blink using security token for auth
398 community.general.redfish_command:
399 category: Systems
400 command: IndicatorLedBlink
401 resource_id: 437XR1138R2
402 baseuri: "{{ baseuri }}"
403 auth_token: "{{ result.session.token }}"
404
405 - name: Add user
406 community.general.redfish_command:
407 category: Accounts
408 command: AddUser
409 baseuri: "{{ baseuri }}"
410 username: "{{ username }}"
411 password: "{{ password }}"
412 new_username: "{{ new_username }}"
413 new_password: "{{ new_password }}"
414 roleid: "{{ roleid }}"
415
416 - name: Add user with specified account types
417 community.general.redfish_command:
418 category: Accounts
419 command: AddUser
420 baseuri: "{{ baseuri }}"
421 username: "{{ username }}"
422 password: "{{ password }}"
423 new_username: "{{ new_username }}"
424 new_password: "{{ new_password }}"
425 roleid: "{{ roleid }}"
426 account_types:
427 - Redfish
428 - WebUI
429
430 - name: Add user using new option aliases
431 community.general.redfish_command:
432 category: Accounts
433 command: AddUser
434 baseuri: "{{ baseuri }}"
435 username: "{{ username }}"
436 password: "{{ password }}"
437 account_username: "{{ account_username }}"
438 account_password: "{{ account_password }}"
439 account_roleid: "{{ account_roleid }}"
440
441 - name: Delete user
442 community.general.redfish_command:
443 category: Accounts
444 command: DeleteUser
445 baseuri: "{{ baseuri }}"
446 username: "{{ username }}"
447 password: "{{ password }}"
448 account_username: "{{ account_username }}"
449
450 - name: Disable user
451 community.general.redfish_command:
452 category: Accounts
453 command: DisableUser
454 baseuri: "{{ baseuri }}"
455 username: "{{ username }}"
456 password: "{{ password }}"
457 account_username: "{{ account_username }}"
458
459 - name: Enable user
460 community.general.redfish_command:
461 category: Accounts
462 command: EnableUser
463 baseuri: "{{ baseuri }}"
464 username: "{{ username }}"
465 password: "{{ password }}"
466 account_username: "{{ account_username }}"
467
468 - name: Add and enable user
469 community.general.redfish_command:
470 category: Accounts
471 command: AddUser,EnableUser
472 baseuri: "{{ baseuri }}"
473 username: "{{ username }}"
474 password: "{{ password }}"
475 new_username: "{{ new_username }}"
476 new_password: "{{ new_password }}"
477 roleid: "{{ roleid }}"
478
479 - name: Update user password
480 community.general.redfish_command:
481 category: Accounts
482 command: UpdateUserPassword
483 baseuri: "{{ baseuri }}"
484 username: "{{ username }}"
485 password: "{{ password }}"
486 account_username: "{{ account_username }}"
487 account_password: "{{ account_password }}"
488
489 - name: Update user role
490 community.general.redfish_command:
491 category: Accounts
492 command: UpdateUserRole
493 baseuri: "{{ baseuri }}"
494 username: "{{ username }}"
495 password: "{{ password }}"
496 account_username: "{{ account_username }}"
497 roleid: "{{ roleid }}"
498
499 - name: Update user name
500 community.general.redfish_command:
501 category: Accounts
502 command: UpdateUserName
503 baseuri: "{{ baseuri }}"
504 username: "{{ username }}"
505 password: "{{ password }}"
506 account_username: "{{ account_username }}"
507 account_updatename: "{{ account_updatename }}"
508
509 - name: Update user name
510 community.general.redfish_command:
511 category: Accounts
512 command: UpdateUserName
513 baseuri: "{{ baseuri }}"
514 username: "{{ username }}"
515 password: "{{ password }}"
516 account_username: "{{ account_username }}"
517 update_username: "{{ update_username }}"
518
519 - name: Update AccountService properties
520 community.general.redfish_command:
521 category: Accounts
522 command: UpdateAccountServiceProperties
523 baseuri: "{{ baseuri }}"
524 username: "{{ username }}"
525 password: "{{ password }}"
526 account_properties:
527 AccountLockoutThreshold: 5
528 AccountLockoutDuration: 600
529
530 - name: Update user AccountTypes
531 community.general.redfish_command:
532 category: Accounts
533 command: UpdateUserAccountTypes
534 baseuri: "{{ baseuri }}"
535 username: "{{ username }}"
536 password: "{{ password }}"
537 account_username: "{{ account_username }}"
538 account_types:
539 - Redfish
540 - WebUI
541
542 - name: Clear Manager Logs with a timeout of 20 seconds
543 community.general.redfish_command:
544 category: Manager
545 command: ClearLogs
546 resource_id: BMC
547 baseuri: "{{ baseuri }}"
548 username: "{{ username }}"
549 password: "{{ password }}"
550 timeout: 20
551
552 - name: Create session
553 community.general.redfish_command:
554 category: Sessions
555 command: CreateSession
556 baseuri: "{{ baseuri }}"
557 username: "{{ username }}"
558 password: "{{ password }}"
559 register: result
560
561 - name: Set chassis indicator LED to blink using security token for auth
562 community.general.redfish_command:
563 category: Chassis
564 command: IndicatorLedBlink
565 resource_id: 1U
566 baseuri: "{{ baseuri }}"
567 auth_token: "{{ result.session.token }}"
568
569 - name: Delete session using security token created by CreateSesssion above
570 community.general.redfish_command:
571 category: Sessions
572 command: DeleteSession
573 baseuri: "{{ baseuri }}"
574 auth_token: "{{ result.session.token }}"
575 session_uri: "{{ result.session.uri }}"
576
577 - name: Clear Sessions
578 community.general.redfish_command:
579 category: Sessions
580 command: ClearSessions
581 baseuri: "{{ baseuri }}"
582 username: "{{ username }}"
583 password: "{{ password }}"
584
585 - name: Simple update
586 community.general.redfish_command:
587 category: Update
588 command: SimpleUpdate
589 baseuri: "{{ baseuri }}"
590 username: "{{ username }}"
591 password: "{{ password }}"
592 update_image_uri: https://example.com/myupdate.img
593
594 - name: Simple update with additional options
595 community.general.redfish_command:
596 category: Update
597 command: SimpleUpdate
598 baseuri: "{{ baseuri }}"
599 username: "{{ username }}"
600 password: "{{ password }}"
601 update_image_uri: //example.com/myupdate.img
602 update_protocol: FTP
603 update_targets:
604 - /redfish/v1/UpdateService/FirmwareInventory/BMC
605 update_creds:
606 username: operator
607 password: supersecretpwd
608
609 - name: Multipart HTTP push update; timeout is 600 seconds to allow for a large image transfer
610 community.general.redfish_command:
611 category: Update
612 command: MultipartHTTPPushUpdate
613 baseuri: "{{ baseuri }}"
614 username: "{{ username }}"
615 password: "{{ password }}"
616 timeout: 600
617 update_image_file: ~/images/myupdate.img
618
619 - name: Multipart HTTP push with additional options; timeout is 600 seconds to allow for a large image transfer
620 community.general.redfish_command:
621 category: Update
622 command: MultipartHTTPPushUpdate
623 baseuri: "{{ baseuri }}"
624 username: "{{ username }}"
625 password: "{{ password }}"
626 timeout: 600
627 update_image_file: ~/images/myupdate.img
628 update_targets:
629 - /redfish/v1/UpdateService/FirmwareInventory/BMC
630 update_oem_params:
631 PreserveConfiguration: false
632
633 - name: Multipart HTTP push with custom OEM options
634 vars:
635 oem_payload:
636 ImageType: BMC
637 community.general.redfish_command:
638 category: Update
639 command: MultipartHTTPPushUpdate
640 baseuri: "{{ baseuri }}"
641 username: "{{ username }}"
642 password: "{{ password }}"
643 update_image_file: ~/images/myupdate.img
644 update_targets:
645 - /redfish/v1/UpdateService/FirmwareInventory/BMC
646 update_custom_oem_header: OemParameters
647 update_custom_oem_mime_type: "application/json"
648 update_custom_oem_params: "{{ oem_payload | to_json }}"
649
650 - name: Perform requested operations to continue the update
651 community.general.redfish_command:
652 category: Update
653 command: PerformRequestedOperations
654 baseuri: "{{ baseuri }}"
655 username: "{{ username }}"
656 password: "{{ password }}"
657 update_handle: /redfish/v1/TaskService/TaskMonitors/735
658
659 - name: Insert Virtual Media
660 community.general.redfish_command:
661 category: Systems
662 command: VirtualMediaInsert
663 baseuri: "{{ baseuri }}"
664 username: "{{ username }}"
665 password: "{{ password }}"
666 virtual_media:
667 image_url: 'http://example.com/images/SomeLinux-current.iso'
668 media_types:
669 - CD
670 - DVD
671 resource_id: 1
672
673 - name: Insert Virtual Media
674 community.general.redfish_command:
675 category: Manager
676 command: VirtualMediaInsert
677 baseuri: "{{ baseuri }}"
678 username: "{{ username }}"
679 password: "{{ password }}"
680 virtual_media:
681 image_url: 'http://example.com/images/SomeLinux-current.iso'
682 media_types:
683 - CD
684 - DVD
685 resource_id: BMC
686
687 - name: Eject Virtual Media
688 community.general.redfish_command:
689 category: Systems
690 command: VirtualMediaEject
691 baseuri: "{{ baseuri }}"
692 username: "{{ username }}"
693 password: "{{ password }}"
694 virtual_media:
695 image_url: 'http://example.com/images/SomeLinux-current.iso'
696 resource_id: 1
697
698 - name: Eject Virtual Media
699 community.general.redfish_command:
700 category: Manager
701 command: VirtualMediaEject
702 baseuri: "{{ baseuri }}"
703 username: "{{ username }}"
704 password: "{{ password }}"
705 virtual_media:
706 image_url: 'http://example.com/images/SomeLinux-current.iso'
707 resource_id: BMC
708
709 - name: Restart manager power gracefully
710 community.general.redfish_command:
711 category: Manager
712 command: GracefulRestart
713 resource_id: BMC
714 baseuri: "{{ baseuri }}"
715 username: "{{ username }}"
716 password: "{{ password }}"
717
718 - name: Restart manager power gracefully and wait for it to be available
719 community.general.redfish_command:
720 category: Manager
721 command: GracefulRestart
722 resource_id: BMC
723 baseuri: "{{ baseuri }}"
724 username: "{{ username }}"
725 password: "{{ password }}"
726 wait: true
727
728 - name: Restart manager power gracefully
729 community.general.redfish_command:
730 category: Manager
731 command: PowerGracefulRestart
732 resource_id: BMC
733
734 - name: Turn manager power off
735 community.general.redfish_command:
736 category: Manager
737 command: PowerForceOff
738 resource_id: BMC
739
740 - name: Restart manager power forcefully
741 community.general.redfish_command:
742 category: Manager
743 command: PowerForceRestart
744 resource_id: BMC
745
746 - name: Shutdown manager power gracefully
747 community.general.redfish_command:
748 category: Manager
749 command: PowerGracefulShutdown
750 resource_id: BMC
751
752 - name: Turn manager power on
753 community.general.redfish_command:
754 category: Manager
755 command: PowerOn
756 resource_id: BMC
757
758 - name: Reboot manager power
759 community.general.redfish_command:
760 category: Manager
761 command: PowerReboot
762 resource_id: BMC
763
764 - name: Factory reset manager to defaults
765 community.general.redfish_command:
766 category: Manager
767 command: ResetToDefaults
768 resource_id: BMC
769 reset_to_defaults_mode: ResetAll
770
771 - name: Verify BIOS attributes
772 community.general.redfish_command:
773 category: Systems
774 command: VerifyBiosAttributes
775 baseuri: "{{ baseuri }}"
776 username: "{{ username }}"
777 password: "{{ password }}"
778 bios_attributes:
779 SubNumaClustering: "Disabled"
780 WorkloadProfile: "Virtualization-MaxPerformance"
781 """
782
783 RETURN = r"""
784 msg:
785 description: Message with action result or error description.
786 returned: always
787 type: str
788 sample: "Action was successful"
789 return_values:
790 description: Dictionary containing command-specific response data from the action.
791 returned: on success
792 type: dict
793 version_added: 6.1.0
794 sample:
795 {
796 "update_status": {
797 "handle": "/redfish/v1/TaskService/TaskMonitors/735",
798 "messages": [],
799 "resets_requested": [],
800 "ret": true,
801 "status": "New"
802 }
803 }
804 """
805
806 from ansible.module_utils.basic import AnsibleModule
807 from ansible.module_utils.common.text.converters import to_native
808
809 from ansible_collections.community.general.plugins.module_utils._redfish_utils import (
810 REDFISH_COMMON_ARGUMENT_SPEC,
811 RedfishUtils,
812 )
813
814 # More will be added as module features are expanded
815 CATEGORY_COMMANDS_ALL = {
816 "Systems": [
817 "PowerOn",
818 "PowerForceOff",
819 "PowerForceRestart",
820 "PowerGracefulRestart",
821 "PowerGracefulShutdown",
822 "PowerReboot",
823 "PowerCycle",
824 "PowerFullPowerCycle",
825 "SetOneTimeBoot",
826 "EnableContinuousBootOverride",
827 "DisableBootOverride",
828 "IndicatorLedOn",
829 "IndicatorLedOff",
830 "IndicatorLedBlink",
831 "VirtualMediaInsert",
832 "VirtualMediaEject",
833 "VerifyBiosAttributes",
834 ],
835 "Chassis": ["IndicatorLedOn", "IndicatorLedOff", "IndicatorLedBlink"],
836 "Accounts": [
837 "AddUser",
838 "EnableUser",
839 "DeleteUser",
840 "DisableUser",
841 "UpdateUserRole",
842 "UpdateUserPassword",
843 "UpdateUserName",
844 "UpdateUserAccountTypes",
845 "UpdateAccountServiceProperties",
846 ],
847 "Sessions": ["ClearSessions", "CreateSession", "DeleteSession"],
848 "Manager": [
849 "GracefulRestart",
850 "ClearLogs",
851 "VirtualMediaInsert",
852 "ResetToDefaults",
853 "VirtualMediaEject",
854 "PowerOn",
855 "PowerForceOff",
856 "PowerForceRestart",
857 "PowerGracefulRestart",
858 "PowerGracefulShutdown",
859 "PowerReboot",
860 ],
861 "Update": ["SimpleUpdate", "MultipartHTTPPushUpdate", "PerformRequestedOperations"],
862 }
863
864
865 def main():
866 result = {}
867 return_values = {}
868 argument_spec = dict(
869 category=dict(required=True),
870 command=dict(required=True, type="list", elements="str"),
871 baseuri=dict(required=True),
872 username=dict(),
873 password=dict(no_log=True),
874 auth_token=dict(no_log=True),
875 session_uri=dict(),
876 id=dict(aliases=["account_id"]),
877 new_username=dict(aliases=["account_username"]),
878 new_password=dict(aliases=["account_password"], no_log=True),
879 roleid=dict(aliases=["account_roleid"]),
880 account_types=dict(type="list", elements="str", aliases=["account_accounttypes"]),
881 oem_account_types=dict(type="list", elements="str", aliases=["account_oemaccounttypes"]),
882 update_username=dict(type="str", aliases=["account_updatename"]),
883 account_properties=dict(type="dict", default={}),
884 bootdevice=dict(),
885 timeout=dict(type="int", default=60),
886 uefi_target=dict(),
887 boot_next=dict(),
888 boot_override_mode=dict(choices=["Legacy", "UEFI"]),
889 resource_id=dict(),
890 update_image_uri=dict(),
891 update_image_file=dict(type="path"),
892 update_protocol=dict(),
893 update_targets=dict(type="list", elements="str", default=[]),
894 update_oem_params=dict(type="dict"),
895 update_custom_oem_header=dict(type="str"),
896 update_custom_oem_mime_type=dict(type="str"),
897 update_custom_oem_params=dict(type="raw"),
898 update_creds=dict(type="dict", options=dict(username=dict(), password=dict(no_log=True))),
899 update_apply_time=dict(
900 choices=[
901 "Immediate",
902 "OnReset",
903 "AtMaintenanceWindowStart",
904 "InMaintenanceWindowOnReset",
905 "OnStartUpdateRequest",
906 ]
907 ),
908 update_handle=dict(),
909 virtual_media=dict(
910 type="dict",
911 options=dict(
912 media_types=dict(type="list", elements="str", default=[]),
913 image_url=dict(),
914 inserted=dict(type="bool", default=True),
915 write_protected=dict(type="bool", default=True),
916 username=dict(),
917 password=dict(no_log=True),
918 transfer_protocol_type=dict(),
919 transfer_method=dict(),
920 ),
921 ),
922 strip_etag_quotes=dict(type="bool", default=False),
923 reset_to_defaults_mode=dict(choices=["ResetAll", "PreserveNetworkAndUsers", "PreserveNetwork"]),
924 bios_attributes=dict(type="dict"),
925 wait=dict(type="bool", default=False),
926 wait_timeout=dict(type="int", default=120),
927 )
928 argument_spec.update(REDFISH_COMMON_ARGUMENT_SPEC)
929 module = AnsibleModule(
930 argument_spec,
931 required_together=[
932 ("username", "password"),
933 ("update_custom_oem_header", "update_custom_oem_params"),
934 ],
935 required_one_of=[
936 ("username", "auth_token"),
937 ],
938 mutually_exclusive=[
939 ("username", "auth_token"),
940 ],
941 supports_check_mode=False,
942 )
943
944 category = module.params["category"]
945 command_list = module.params["command"]
946
947 # admin credentials used for authentication
948 creds = {"user": module.params["username"], "pswd": module.params["password"], "token": module.params["auth_token"]}
949
950 # user to add/modify/delete
951 user = {
952 "account_id": module.params["id"],
953 "account_username": module.params["new_username"],
954 "account_password": module.params["new_password"],
955 "account_roleid": module.params["roleid"],
956 "account_accounttypes": module.params["account_types"],
957 "account_oemaccounttypes": module.params["oem_account_types"],
958 "account_updatename": module.params["update_username"],
959 "account_properties": module.params["account_properties"],
960 "account_passwordchangerequired": None,
961 }
962
963 # timeout
964 timeout = module.params["timeout"]
965
966 # System, Manager or Chassis ID to modify
967 resource_id = module.params["resource_id"]
968
969 # update options
970 update_opts = {
971 "update_image_uri": module.params["update_image_uri"],
972 "update_image_file": module.params["update_image_file"],
973 "update_protocol": module.params["update_protocol"],
974 "update_targets": module.params["update_targets"],
975 "update_creds": module.params["update_creds"],
976 "update_apply_time": module.params["update_apply_time"],
977 "update_oem_params": module.params["update_oem_params"],
978 "update_custom_oem_header": module.params["update_custom_oem_header"],
979 "update_custom_oem_params": module.params["update_custom_oem_params"],
980 "update_custom_oem_mime_type": module.params["update_custom_oem_mime_type"],
981 "update_handle": module.params["update_handle"],
982 }
983
984 # Boot override options
985 boot_opts = {
986 "bootdevice": module.params["bootdevice"],
987 "uefi_target": module.params["uefi_target"],
988 "boot_next": module.params["boot_next"],
989 "boot_override_mode": module.params["boot_override_mode"],
990 }
991
992 # VirtualMedia options
993 virtual_media = module.params["virtual_media"]
994
995 # Etag options
996 strip_etag_quotes = module.params["strip_etag_quotes"]
997
998 # BIOS Attributes options
999 bios_attributes = module.params["bios_attributes"]
1000
1001 # Build root URI
1002 root_uri = f"https://{module.params['baseuri']}"
1003 rf_utils = RedfishUtils(
1004 creds,
1005 root_uri,
1006 timeout,
1007 module,
1008 resource_id=resource_id,
1009 data_modification=True,
1010 strip_etag_quotes=strip_etag_quotes,
1011 )
1012
1013 # Check that Category is valid
1014 if category not in CATEGORY_COMMANDS_ALL:
1015 module.fail_json(msg=f"Invalid Category '{category}'. Valid Categories = {list(CATEGORY_COMMANDS_ALL.keys())}")
1016
1017 # Check that all commands are valid
1018 for cmd in command_list:
1019 # Fail if even one command given is invalid
1020 if cmd not in CATEGORY_COMMANDS_ALL[category]:
1021 module.fail_json(msg=f"Invalid Command '{cmd}'. Valid Commands = {CATEGORY_COMMANDS_ALL[category]}")
1022
1023 # Organize by Categories / Commands
1024 if category == "Accounts":
1025 ACCOUNTS_COMMANDS = {
1026 "AddUser": rf_utils.add_user,
1027 "EnableUser": rf_utils.enable_user,
1028 "DeleteUser": rf_utils.delete_user,
1029 "DisableUser": rf_utils.disable_user,
1030 "UpdateUserRole": rf_utils.update_user_role,
1031 "UpdateUserPassword": rf_utils.update_user_password,
1032 "UpdateUserName": rf_utils.update_user_name,
1033 "UpdateUserAccountTypes": rf_utils.update_user_accounttypes,
1034 "UpdateAccountServiceProperties": rf_utils.update_accountservice_properties,
1035 }
1036
1037 # execute only if we find an Account service resource
1038 result = rf_utils._find_accountservice_resource()
1039 if result["ret"] is False:
1040 # If a password change is required and the user is attempting to
1041 # modify their password, try to proceed.
1042 user["account_passwordchangerequired"] = rf_utils.check_password_change_required(result)
1043 if (
1044 len(command_list) == 1
1045 and command_list[0] == "UpdateUserPassword"
1046 and user["account_passwordchangerequired"]
1047 ):
1048 result = rf_utils.update_user_password(user)
1049 else:
1050 module.fail_json(msg=to_native(result["msg"]))
1051 else:
1052 for command in command_list:
1053 result = ACCOUNTS_COMMANDS[command](user)
1054
1055 elif category == "Systems":
1056 # execute only if we find a System resource
1057 result = rf_utils._find_systems_resource()
1058 if result["ret"] is False:
1059 module.fail_json(msg=result["msg"])
1060
1061 for command in command_list:
1062 if command.startswith("Power"):
1063 result = rf_utils.manage_system_power(command)
1064 elif command == "SetOneTimeBoot":
1065 boot_opts["override_enabled"] = "Once"
1066 result = rf_utils.set_boot_override(boot_opts)
1067 elif command == "EnableContinuousBootOverride":
1068 boot_opts["override_enabled"] = "Continuous"
1069 result = rf_utils.set_boot_override(boot_opts)
1070 elif command == "DisableBootOverride":
1071 boot_opts["override_enabled"] = "Disabled"
1072 result = rf_utils.set_boot_override(boot_opts)
1073 elif command.startswith("IndicatorLed"):
1074 result = rf_utils.manage_system_indicator_led(command)
1075 elif command == "VirtualMediaInsert":
1076 result = rf_utils.virtual_media_insert(virtual_media, category)
1077 elif command == "VirtualMediaEject":
1078 result = rf_utils.virtual_media_eject(virtual_media, category)
1079 elif command == "VerifyBiosAttributes":
1080 result = rf_utils.verify_bios_attributes(bios_attributes)
1081
1082 elif category == "Chassis":
1083 result = rf_utils._find_chassis_resource()
1084 if result["ret"] is False:
1085 module.fail_json(msg=result["msg"])
1086
1087 led_commands = ["IndicatorLedOn", "IndicatorLedOff", "IndicatorLedBlink"]
1088
1089 # Check if more than one led_command is present
1090 num_led_commands = sum([command in led_commands for command in command_list])
1091 if num_led_commands > 1:
1092 result = {"ret": False, "msg": "Only one IndicatorLed command should be sent at a time."}
1093 else:
1094 for command in command_list:
1095 if command in led_commands:
1096 result = rf_utils.manage_chassis_indicator_led(command)
1097
1098 elif category == "Sessions":
1099 # execute only if we find SessionService resources
1100 resource = rf_utils._find_sessionservice_resource()
1101 if resource["ret"] is False:
1102 module.fail_json(msg=resource["msg"])
1103
1104 for command in command_list:
1105 if command == "ClearSessions":
1106 result = rf_utils.clear_sessions()
1107 elif command == "CreateSession":
1108 result = rf_utils.create_session()
1109 elif command == "DeleteSession":
1110 result = rf_utils.delete_session(module.params["session_uri"])
1111
1112 elif category == "Manager":
1113 # execute only if we find a Manager service resource
1114 result = rf_utils._find_managers_resource()
1115 if result["ret"] is False:
1116 module.fail_json(msg=result["msg"])
1117
1118 for command in command_list:
1119 # standardize on the Power* commands, but allow the legacy
1120 # GracefulRestart command
1121 if command == "GracefulRestart":
1122 command = "PowerGracefulRestart"
1123
1124 if command.startswith("Power"):
1125 result = rf_utils.manage_manager_power(command, module.params["wait"], module.params["wait_timeout"])
1126 elif command == "ClearLogs":
1127 result = rf_utils.clear_logs()
1128 elif command == "VirtualMediaInsert":
1129 result = rf_utils.virtual_media_insert(virtual_media, category)
1130 elif command == "VirtualMediaEject":
1131 result = rf_utils.virtual_media_eject(virtual_media, category)
1132 elif command == "ResetToDefaults":
1133 result = rf_utils.manager_reset_to_defaults(module.params["reset_to_defaults_mode"])
1134
1135 elif category == "Update":
1136 # execute only if we find UpdateService resources
1137 resource = rf_utils._find_updateservice_resource()
1138 if resource["ret"] is False:
1139 module.fail_json(msg=resource["msg"])
1140
1141 for command in command_list:
1142 if command == "SimpleUpdate":
1143 result = rf_utils.simple_update(update_opts)
1144 if "update_status" in result:
1145 return_values["update_status"] = result["update_status"]
1146 elif command == "MultipartHTTPPushUpdate":
1147 result = rf_utils.multipath_http_push_update(update_opts)
1148 if "update_status" in result:
1149 return_values["update_status"] = result["update_status"]
1150 elif command == "PerformRequestedOperations":
1151 result = rf_utils.perform_requested_update_operations(update_opts["update_handle"])
1152
1153 # Return data back or fail with proper message
1154 if result["ret"] is True:
1155 del result["ret"]
1156 changed = result.get("changed", True)
1157 session = result.get("session", dict())
1158 module.exit_json(changed=changed, session=session, msg="Action was successful", return_values=return_values)
1159 else:
1160 module.fail_json(msg=to_native(result["msg"]))
1161
1162
1163 if __name__ == "__main__":
1164 main()