Debian Codegraph @

ansible_collections/community/general/plugins/modules/redfish_info.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_info
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 get information back.
14 - Information retrieved is placed in a location specified by the user.
15 extends_documentation_fragment:
16 - community.general._attributes
17 - community.general._attributes.info_module
18 - community.general._redfish
19 attributes:
20 check_mode:
21 version_added: 3.3.0
22 # This was backported to 2.5.4 and 1.3.11 as well, since this was a bugfix
23 options:
24 category:
25 description:
26 - List of categories to execute on OOB controller.
27 default: ['Systems']
28 type: list
29 elements: str
30 command:
31 description:
32 - List of commands to execute on OOB controller.
33 type: list
34 elements: str
35 baseuri:
36 required: true
37 description:
38 - Base URI of OOB controller.
39 type: str
40 username:
41 description:
42 - Username for authenticating to OOB controller.
43 type: str
44 password:
45 description:
46 - Password for authenticating to OOB controller.
47 type: str
48 auth_token:
49 description:
50 - Security token for authenticating to OOB controller.
51 type: str
52 version_added: 2.3.0
53 manager:
54 description:
55 - Name of manager on OOB controller to target.
56 type: str
57 version_added: '8.3.0'
58 timeout:
59 description:
60 - Timeout in seconds for HTTP requests to OOB controller.
61 - The default value for this parameter changed from V(10) to V(60) in community.general 9.0.0.
62 type: int
63 default: 60
64 update_handle:
65 description:
66 - Handle to check the status of an update in progress.
67 type: str
68 version_added: '6.1.0'
69 ciphers:
70 version_added: 9.2.0
71 validate_certs:
72 version_added: 10.6.0
73 ca_path:
74 version_added: 10.6.0
75
76 author: "Jose Delarosa (@jose-delarosa)"
77 """
78
79 EXAMPLES = r"""
80 - name: Get CPU inventory
81 community.general.redfish_info:
82 category: Systems
83 command: GetCpuInventory
84 baseuri: "{{ baseuri }}"
85 username: "{{ username }}"
86 password: "{{ password }}"
87 register: result
88
89 - name: Print fetched information
90 ansible.builtin.debug:
91 msg: "{{ result.redfish_facts.cpu.entries | to_nice_json }}"
92
93 - name: Get CPU model
94 community.general.redfish_info:
95 category: Systems
96 command: GetCpuInventory
97 baseuri: "{{ baseuri }}"
98 username: "{{ username }}"
99 password: "{{ password }}"
100 register: result
101
102 - name: Print fetched information
103 ansible.builtin.debug:
104 msg: "{{ result.redfish_facts.cpu.entries.0.Model }}"
105
106 - name: Get memory inventory
107 community.general.redfish_info:
108 category: Systems
109 command: GetMemoryInventory
110 baseuri: "{{ baseuri }}"
111 username: "{{ username }}"
112 password: "{{ password }}"
113 register: result
114
115 - name: Get fan inventory with a timeout of 20 seconds
116 community.general.redfish_info:
117 category: Chassis
118 command: GetFanInventory
119 baseuri: "{{ baseuri }}"
120 username: "{{ username }}"
121 password: "{{ password }}"
122 timeout: 20
123 register: result
124
125 - name: Get Virtual Media information
126 community.general.redfish_info:
127 category: Manager
128 command: GetVirtualMedia
129 baseuri: "{{ baseuri }}"
130 username: "{{ username }}"
131 password: "{{ password }}"
132 register: result
133
134 - name: Print fetched information
135 ansible.builtin.debug:
136 msg: "{{ result.redfish_facts.virtual_media.entries | to_nice_json }}"
137
138 - name: Get Virtual Media information from Systems
139 community.general.redfish_info:
140 category: Systems
141 command: GetVirtualMedia
142 baseuri: "{{ baseuri }}"
143 username: "{{ username }}"
144 password: "{{ password }}"
145 register: result
146
147 - name: Print fetched information
148 ansible.builtin.debug:
149 msg: "{{ result.redfish_facts.virtual_media.entries | to_nice_json }}"
150
151 - name: Get Volume Inventory
152 community.general.redfish_info:
153 category: Systems
154 command: GetVolumeInventory
155 baseuri: "{{ baseuri }}"
156 username: "{{ username }}"
157 password: "{{ password }}"
158 register: result
159 - name: Print fetched information
160 ansible.builtin.debug:
161 msg: "{{ result.redfish_facts.volume.entries | to_nice_json }}"
162
163 - name: Get Session information
164 community.general.redfish_info:
165 category: Sessions
166 command: GetSessions
167 baseuri: "{{ baseuri }}"
168 username: "{{ username }}"
169 password: "{{ password }}"
170 register: result
171
172 - name: Print fetched information
173 ansible.builtin.debug:
174 msg: "{{ result.redfish_facts.session.entries | to_nice_json }}"
175
176 - name: Get default inventory information
177 community.general.redfish_info:
178 baseuri: "{{ baseuri }}"
179 username: "{{ username }}"
180 password: "{{ password }}"
181 register: result
182 - name: Print fetched information
183 ansible.builtin.debug:
184 msg: "{{ result.redfish_facts | to_nice_json }}"
185
186 - name: Get several inventories
187 community.general.redfish_info:
188 category: Systems
189 command: GetNicInventory,GetBiosAttributes
190 baseuri: "{{ baseuri }}"
191 username: "{{ username }}"
192 password: "{{ password }}"
193
194 - name: Get configuration of the AccountService
195 community.general.redfish_info:
196 category: Accounts
197 command: GetAccountServiceConfig
198 baseuri: "{{ baseuri }}"
199 username: "{{ username }}"
200 password: "{{ password }}"
201
202 - name: Get default system inventory and user information
203 community.general.redfish_info:
204 category: Systems,Accounts
205 baseuri: "{{ baseuri }}"
206 username: "{{ username }}"
207 password: "{{ password }}"
208
209 - name: Get default system, user and firmware information
210 community.general.redfish_info:
211 category: ["Systems", "Accounts", "Update"]
212 baseuri: "{{ baseuri }}"
213 username: "{{ username }}"
214 password: "{{ password }}"
215
216 - name: Get Manager NIC inventory information
217 community.general.redfish_info:
218 category: Manager
219 command: GetManagerNicInventory
220 baseuri: "{{ baseuri }}"
221 username: "{{ username }}"
222 password: "{{ password }}"
223
224 - name: Get boot override information
225 community.general.redfish_info:
226 category: Systems
227 command: GetBootOverride
228 baseuri: "{{ baseuri }}"
229 username: "{{ username }}"
230 password: "{{ password }}"
231
232 - name: Get chassis inventory
233 community.general.redfish_info:
234 category: Chassis
235 command: GetChassisInventory
236 baseuri: "{{ baseuri }}"
237 username: "{{ username }}"
238 password: "{{ password }}"
239
240 - name: Get all information available in the Manager category
241 community.general.redfish_info:
242 category: Manager
243 command: all
244 baseuri: "{{ baseuri }}"
245 username: "{{ username }}"
246 password: "{{ password }}"
247
248 - name: Get firmware update capability information
249 community.general.redfish_info:
250 category: Update
251 command: GetFirmwareUpdateCapabilities
252 baseuri: "{{ baseuri }}"
253 username: "{{ username }}"
254 password: "{{ password }}"
255
256 - name: Get firmware inventory
257 community.general.redfish_info:
258 category: Update
259 command: GetFirmwareInventory
260 baseuri: "{{ baseuri }}"
261 username: "{{ username }}"
262 password: "{{ password }}"
263
264 - name: Get service identification
265 community.general.redfish_info:
266 category: Manager
267 command: GetServiceIdentification
268 manager: "{{ manager }}"
269 baseuri: "{{ baseuri }}"
270 username: "{{ username }}"
271 password: "{{ password }}"
272
273 - name: Get software inventory
274 community.general.redfish_info:
275 category: Update
276 command: GetSoftwareInventory
277 baseuri: "{{ baseuri }}"
278 username: "{{ username }}"
279 password: "{{ password }}"
280
281 - name: Get the status of an update operation
282 community.general.redfish_info:
283 category: Update
284 command: GetUpdateStatus
285 baseuri: "{{ baseuri }}"
286 username: "{{ username }}"
287 password: "{{ password }}"
288 update_handle: /redfish/v1/TaskService/TaskMonitors/735
289
290 - name: Get Manager Services
291 community.general.redfish_info:
292 category: Manager
293 command: GetNetworkProtocols
294 baseuri: "{{ baseuri }}"
295 username: "{{ username }}"
296 password: "{{ password }}"
297
298 - name: Get all information available in all categories
299 community.general.redfish_info:
300 category: all
301 command: all
302 baseuri: "{{ baseuri }}"
303 username: "{{ username }}"
304 password: "{{ password }}"
305
306 - name: Get system health report
307 community.general.redfish_info:
308 category: Systems
309 command: GetHealthReport
310 baseuri: "{{ baseuri }}"
311 username: "{{ username }}"
312 password: "{{ password }}"
313
314 - name: Get chassis health report
315 community.general.redfish_info:
316 category: Chassis
317 command: GetHealthReport
318 baseuri: "{{ baseuri }}"
319 username: "{{ username }}"
320 password: "{{ password }}"
321
322 - name: Get manager health report
323 community.general.redfish_info:
324 category: Manager
325 command: GetHealthReport
326 baseuri: "{{ baseuri }}"
327 username: "{{ username }}"
328 password: "{{ password }}"
329
330 - name: Get manager Redfish Host Interface inventory
331 community.general.redfish_info:
332 category: Manager
333 command: GetHostInterfaces
334 baseuri: "{{ baseuri }}"
335 username: "{{ username }}"
336 password: "{{ password }}"
337
338 - name: Get Manager Inventory
339 community.general.redfish_info:
340 category: Manager
341 command: GetManagerInventory
342 baseuri: "{{ baseuri }}"
343 username: "{{ username }}"
344 password: "{{ password }}"
345
346 - name: Get HPE Thermal Config
347 community.general.redfish_info:
348 category: Chassis
349 command: GetHPEThermalConfig
350 baseuri: "{{ baseuri }}"
351 username: "{{ username }}"
352 password: "{{ password }}"
353
354 - name: Get HPE Fan Percent Minimum
355 community.general.redfish_info:
356 category: Chassis
357 command: GetHPEFanPercentMin
358 baseuri: "{{ baseuri }}"
359 username: "{{ username }}"
360 password: "{{ password }}"
361
362 - name: Get BIOS registry
363 community.general.redfish_info:
364 category: Systems
365 command: GetBiosRegistries
366 baseuri: "{{ baseuri }}"
367 username: "{{ username }}"
368 password: "{{ password }}"
369
370 - name: Get power restore policy
371 community.general.redfish_info:
372 category: Systems
373 command: GetPowerRestorePolicy
374 baseuri: "{{ baseuri }}"
375 username: "{{ username }}"
376 password: "{{ password }}"
377
378 - name: Check the availability of the service with a timeout of 5 seconds
379 community.general.redfish_info:
380 category: Service
381 command: CheckAvailability
382 baseuri: "{{ baseuri }}"
383 username: "{{ username }}"
384 password: "{{ password }}"
385 timeout: 5
386 register: result
387 """
388
389 RETURN = r"""
390 result:
391 description: Different results depending on task.
392 returned: always
393 type: dict
394 sample: List of CPUs on system
395 """
396
397 from ansible.module_utils.basic import AnsibleModule
398
399 from ansible_collections.community.general.plugins.module_utils._redfish_utils import (
400 REDFISH_COMMON_ARGUMENT_SPEC,
401 RedfishUtils,
402 )
403
404 CATEGORY_COMMANDS_ALL = {
405 "Systems": [
406 "GetSystemInventory",
407 "GetPsuInventory",
408 "GetCpuInventory",
409 "GetMemoryInventory",
410 "GetNicInventory",
411 "GetHealthReport",
412 "GetStorageControllerInventory",
413 "GetDiskInventory",
414 "GetVolumeInventory",
415 "GetBiosAttributes",
416 "GetBootOrder",
417 "GetBootOverride",
418 "GetVirtualMedia",
419 "GetBiosRegistries",
420 "GetPowerRestorePolicy",
421 ],
422 "Chassis": [
423 "GetFanInventory",
424 "GetPsuInventory",
425 "GetChassisPower",
426 "GetChassisThermals",
427 "GetChassisInventory",
428 "GetHealthReport",
429 "GetHPEThermalConfig",
430 "GetHPEFanPercentMin",
431 ],
432 "Accounts": ["ListUsers", "GetAccountServiceConfig"],
433 "Sessions": ["GetSessions"],
434 "Update": ["GetFirmwareInventory", "GetFirmwareUpdateCapabilities", "GetSoftwareInventory", "GetUpdateStatus"],
435 "Manager": [
436 "GetManagerNicInventory",
437 "GetVirtualMedia",
438 "GetLogs",
439 "GetNetworkProtocols",
440 "GetHealthReport",
441 "GetHostInterfaces",
442 "GetManagerInventory",
443 "GetServiceIdentification",
444 ],
445 "Service": ["CheckAvailability"],
446 }
447
448 CATEGORY_COMMANDS_DEFAULT = {
449 "Systems": "GetSystemInventory",
450 "Chassis": "GetFanInventory",
451 "Accounts": "ListUsers",
452 "Update": "GetFirmwareInventory",
453 "Sessions": "GetSessions",
454 "Manager": "GetManagerNicInventory",
455 "Service": "CheckAvailability",
456 }
457
458
459 def main():
460 result = {}
461 category_list = []
462 argument_spec = dict(
463 category=dict(type="list", elements="str", default=["Systems"]),
464 command=dict(type="list", elements="str"),
465 baseuri=dict(required=True),
466 username=dict(),
467 password=dict(no_log=True),
468 auth_token=dict(no_log=True),
469 timeout=dict(type="int", default=60),
470 update_handle=dict(),
471 manager=dict(),
472 )
473 argument_spec.update(REDFISH_COMMON_ARGUMENT_SPEC)
474 module = AnsibleModule(
475 argument_spec,
476 required_together=[
477 ("username", "password"),
478 ],
479 required_one_of=[
480 ("username", "auth_token"),
481 ],
482 mutually_exclusive=[
483 ("username", "auth_token"),
484 ],
485 supports_check_mode=True,
486 )
487
488 # admin credentials used for authentication
489 creds = {"user": module.params["username"], "pswd": module.params["password"], "token": module.params["auth_token"]}
490
491 # timeout
492 timeout = module.params["timeout"]
493
494 # update handle
495 update_handle = module.params["update_handle"]
496
497 # manager
498 manager = module.params["manager"]
499
500 # Build root URI
501 root_uri = f"https://{module.params['baseuri']}"
502 rf_utils = RedfishUtils(creds, root_uri, timeout, module)
503
504 # Build Category list
505 if "all" in module.params["category"]:
506 for entry in CATEGORY_COMMANDS_ALL:
507 category_list.append(entry)
508 else:
509 # one or more categories specified
510 category_list = module.params["category"]
511
512 for category in category_list:
513 command_list = []
514 # Build Command list for each Category
515 if category in CATEGORY_COMMANDS_ALL:
516 if not module.params["command"]:
517 # True if we don't specify a command --> use default
518 command_list.append(CATEGORY_COMMANDS_DEFAULT[category])
519 elif "all" in module.params["command"]:
520 for entry in range(len(CATEGORY_COMMANDS_ALL[category])):
521 command_list.append(CATEGORY_COMMANDS_ALL[category][entry])
522 # one or more commands
523 else:
524 command_list = module.params["command"]
525 # Verify that all commands are valid
526 for cmd in command_list:
527 # Fail if even one command given is invalid
528 if cmd not in CATEGORY_COMMANDS_ALL[category]:
529 module.fail_json(msg=f"Invalid Command: {cmd}")
530 else:
531 # Fail if even one category given is invalid
532 module.fail_json(msg=f"Invalid Category: {category}")
533
534 # Organize by Categories / Commands
535 if category == "Service":
536 # service-level commands are always available
537 for command in command_list:
538 if command == "CheckAvailability":
539 result["service"] = rf_utils.check_service_availability()
540
541 elif category == "Systems":
542 # execute only if we find a Systems resource
543 resource = rf_utils._find_systems_resource()
544 if resource["ret"] is False:
545 module.fail_json(msg=resource["msg"])
546
547 for command in command_list:
548 if command == "GetSystemInventory":
549 result["system"] = rf_utils.get_multi_system_inventory()
550 elif command == "GetCpuInventory":
551 result["cpu"] = rf_utils.get_multi_cpu_inventory()
552 elif command == "GetMemoryInventory":
553 result["memory"] = rf_utils.get_multi_memory_inventory()
554 elif command == "GetNicInventory":
555 result["nic"] = rf_utils.get_multi_nic_inventory(category)
556 elif command == "GetStorageControllerInventory":
557 result["storage_controller"] = rf_utils.get_multi_storage_controller_inventory()
558 elif command == "GetDiskInventory":
559 result["disk"] = rf_utils.get_multi_disk_inventory()
560 elif command == "GetVolumeInventory":
561 result["volume"] = rf_utils.get_multi_volume_inventory()
562 elif command == "GetBiosAttributes":
563 result["bios_attribute"] = rf_utils.get_multi_bios_attributes()
564 elif command == "GetBootOrder":
565 result["boot_order"] = rf_utils.get_multi_boot_order()
566 elif command == "GetBootOverride":
567 result["boot_override"] = rf_utils.get_multi_boot_override()
568 elif command == "GetHealthReport":
569 result["health_report"] = rf_utils.get_multi_system_health_report()
570 elif command == "GetVirtualMedia":
571 result["virtual_media"] = rf_utils.get_multi_virtualmedia(category)
572 elif command == "GetBiosRegistries":
573 result["bios_registries"] = rf_utils.get_bios_registries()
574 elif command == "GetPowerRestorePolicy":
575 result["power_restore_policy"] = rf_utils.get_multi_power_restore_policy()
576
577 elif category == "Chassis":
578 # execute only if we find Chassis resource
579 resource = rf_utils._find_chassis_resource()
580 if resource["ret"] is False:
581 module.fail_json(msg=resource["msg"])
582
583 for command in command_list:
584 if command == "GetFanInventory":
585 result["fan"] = rf_utils.get_fan_inventory()
586 elif command == "GetPsuInventory":
587 result["psu"] = rf_utils.get_psu_inventory()
588 elif command == "GetChassisThermals":
589 result["thermals"] = rf_utils.get_chassis_thermals()
590 elif command == "GetChassisPower":
591 result["chassis_power"] = rf_utils.get_chassis_power()
592 elif command == "GetChassisInventory":
593 result["chassis"] = rf_utils.get_chassis_inventory()
594 elif command == "GetHealthReport":
595 result["health_report"] = rf_utils.get_multi_chassis_health_report()
596 elif command == "GetHPEThermalConfig":
597 result["hpe_thermal_config"] = rf_utils.get_hpe_thermal_config()
598 elif command == "GetHPEFanPercentMin":
599 result["hpe_fan_percent_min"] = rf_utils.get_hpe_fan_percent_min()
600
601 elif category == "Accounts":
602 # execute only if we find an Account service resource
603 resource = rf_utils._find_accountservice_resource()
604 if resource["ret"] is False:
605 module.fail_json(msg=resource["msg"])
606
607 for command in command_list:
608 if command == "ListUsers":
609 result["user"] = rf_utils.list_users()
610 elif command == "GetAccountServiceConfig":
611 result["accountservice_config"] = rf_utils.get_accountservice_properties()
612
613 elif category == "Update":
614 # execute only if we find UpdateService resources
615 resource = rf_utils._find_updateservice_resource()
616 if resource["ret"] is False:
617 module.fail_json(msg=resource["msg"])
618
619 for command in command_list:
620 if command == "GetFirmwareInventory":
621 result["firmware"] = rf_utils.get_firmware_inventory()
622 elif command == "GetSoftwareInventory":
623 result["software"] = rf_utils.get_software_inventory()
624 elif command == "GetFirmwareUpdateCapabilities":
625 result["firmware_update_capabilities"] = rf_utils.get_firmware_update_capabilities()
626 elif command == "GetUpdateStatus":
627 result["update_status"] = rf_utils.get_update_status(update_handle)
628
629 elif category == "Sessions":
630 # execute only if we find SessionService resources
631 resource = rf_utils._find_sessionservice_resource()
632 if resource["ret"] is False:
633 module.fail_json(msg=resource["msg"])
634
635 for command in command_list:
636 if command == "GetSessions":
637 result["session"] = rf_utils.get_sessions()
638
639 elif category == "Manager":
640 # execute only if we find a Manager service resource
641 resource = rf_utils._find_managers_resource()
642 if resource["ret"] is False:
643 module.fail_json(msg=resource["msg"])
644
645 for command in command_list:
646 if command == "GetManagerNicInventory":
647 result["manager_nics"] = rf_utils.get_multi_nic_inventory(category)
648 elif command == "GetVirtualMedia":
649 result["virtual_media"] = rf_utils.get_multi_virtualmedia(category)
650 elif command == "GetLogs":
651 result["log"] = rf_utils.get_logs()
652 elif command == "GetNetworkProtocols":
653 result["network_protocols"] = rf_utils.get_network_protocols()
654 elif command == "GetHealthReport":
655 result["health_report"] = rf_utils.get_multi_manager_health_report()
656 elif command == "GetHostInterfaces":
657 result["host_interfaces"] = rf_utils.get_hostinterfaces()
658 elif command == "GetManagerInventory":
659 result["manager"] = rf_utils.get_multi_manager_inventory()
660 elif command == "GetServiceIdentification":
661 result["service_id"] = rf_utils.get_service_identification(manager)
662
663 # Return data back
664 module.exit_json(redfish_facts=result)
665
666
667 if __name__ == "__main__":
668 main()