| 1 |
#!/usr/bin/python |
| 2 |
# Copyright (c) 2021-2022 Hewlett Packard Enterprise, Inc. All rights reserved. |
| 3 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 4 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 5 |
from __future__ import annotations |
| 6 |
|
| 7 |
DOCUMENTATION = r""" |
| 8 |
module: ilo_redfish_info |
| 9 |
short_description: Gathers server information through iLO using Redfish APIs |
| 10 |
version_added: 4.2.0 |
| 11 |
description: |
| 12 |
- Builds Redfish URIs locally and sends them to iLO to get information back. |
| 13 |
- For use with HPE iLO operations that require Redfish OEM extensions. |
| 14 |
extends_documentation_fragment: |
| 15 |
- community.general._attributes |
| 16 |
- community.general._attributes.info_module |
| 17 |
- community.general._redfish |
| 18 |
options: |
| 19 |
category: |
| 20 |
required: true |
| 21 |
description: |
| 22 |
- List of categories to execute on iLO. |
| 23 |
type: list |
| 24 |
elements: str |
| 25 |
command: |
| 26 |
required: true |
| 27 |
description: |
| 28 |
- List of commands to execute on iLO. |
| 29 |
type: list |
| 30 |
elements: str |
| 31 |
baseuri: |
| 32 |
required: true |
| 33 |
description: |
| 34 |
- Base URI of iLO. |
| 35 |
type: str |
| 36 |
username: |
| 37 |
description: |
| 38 |
- Username for authenticating to iLO. |
| 39 |
type: str |
| 40 |
password: |
| 41 |
description: |
| 42 |
- Password for authenticating to iLO. |
| 43 |
type: str |
| 44 |
auth_token: |
| 45 |
description: |
| 46 |
- Security token for authenticating to iLO. |
| 47 |
type: str |
| 48 |
timeout: |
| 49 |
description: |
| 50 |
- Timeout in seconds for HTTP requests to iLO. |
| 51 |
default: 10 |
| 52 |
type: int |
| 53 |
validate_certs: |
| 54 |
version_added: 10.6.0 |
| 55 |
ca_path: |
| 56 |
version_added: 10.6.0 |
| 57 |
ciphers: |
| 58 |
version_added: 10.6.0 |
| 59 |
author: |
| 60 |
- "Bhavya B (@bhavya06)" |
| 61 |
""" |
| 62 |
|
| 63 |
EXAMPLES = r""" |
| 64 |
- name: Get iLO Sessions |
| 65 |
community.general.ilo_redfish_info: |
| 66 |
category: Sessions |
| 67 |
command: GetiLOSessions |
| 68 |
baseuri: "{{ baseuri }}" |
| 69 |
username: "{{ username }}" |
| 70 |
password: "{{ password }}" |
| 71 |
register: result_sessions |
| 72 |
""" |
| 73 |
|
| 74 |
RETURN = r""" |
| 75 |
ilo_redfish_info: |
| 76 |
description: Returns iLO sessions. |
| 77 |
type: dict |
| 78 |
contains: |
| 79 |
GetiLOSessions: |
| 80 |
description: Returns the iLO session msg and whether the function executed successfully. |
| 81 |
type: dict |
| 82 |
contains: |
| 83 |
ret: |
| 84 |
description: Check variable to see if the information was successfully retrieved. |
| 85 |
type: bool |
| 86 |
msg: |
| 87 |
description: Information of all active iLO sessions. |
| 88 |
type: list |
| 89 |
elements: dict |
| 90 |
contains: |
| 91 |
Description: |
| 92 |
description: Provides a description of the resource. |
| 93 |
type: str |
| 94 |
Id: |
| 95 |
description: The sessionId. |
| 96 |
type: str |
| 97 |
Name: |
| 98 |
description: The name of the resource. |
| 99 |
type: str |
| 100 |
UserName: |
| 101 |
description: Name to use to log in to the management processor. |
| 102 |
type: str |
| 103 |
returned: always |
| 104 |
""" |
| 105 |
|
| 106 |
CATEGORY_COMMANDS_ALL = {"Sessions": ["GetiLOSessions"]} |
| 107 |
|
| 108 |
CATEGORY_COMMANDS_DEFAULT = {"Sessions": "GetiLOSessions"} |
| 109 |
|
| 110 |
from ansible.module_utils.basic import AnsibleModule |
| 111 |
|
| 112 |
from ansible_collections.community.general.plugins.module_utils._ilo_redfish_utils import iLORedfishUtils |
| 113 |
from ansible_collections.community.general.plugins.module_utils._redfish_utils import REDFISH_COMMON_ARGUMENT_SPEC |
| 114 |
|
| 115 |
|
| 116 |
def main(): |
| 117 |
result = {} |
| 118 |
category_list = [] |
| 119 |
argument_spec = dict( |
| 120 |
category=dict(required=True, type="list", elements="str"), |
| 121 |
command=dict(required=True, type="list", elements="str"), |
| 122 |
baseuri=dict(required=True), |
| 123 |
username=dict(), |
| 124 |
password=dict(no_log=True), |
| 125 |
auth_token=dict(no_log=True), |
| 126 |
timeout=dict(type="int", default=10), |
| 127 |
) |
| 128 |
argument_spec.update(REDFISH_COMMON_ARGUMENT_SPEC) |
| 129 |
module = AnsibleModule( |
| 130 |
argument_spec, |
| 131 |
required_together=[ |
| 132 |
("username", "password"), |
| 133 |
], |
| 134 |
required_one_of=[ |
| 135 |
("username", "auth_token"), |
| 136 |
], |
| 137 |
mutually_exclusive=[ |
| 138 |
("username", "auth_token"), |
| 139 |
], |
| 140 |
supports_check_mode=True, |
| 141 |
) |
| 142 |
|
| 143 |
creds = {"user": module.params["username"], "pswd": module.params["password"], "token": module.params["auth_token"]} |
| 144 |
|
| 145 |
timeout = module.params["timeout"] |
| 146 |
|
| 147 |
root_uri = f"https://{module.params['baseuri']}" |
| 148 |
rf_utils = iLORedfishUtils(creds, root_uri, timeout, module) |
| 149 |
|
| 150 |
# Build Category list |
| 151 |
if "all" in module.params["category"]: |
| 152 |
for entry in CATEGORY_COMMANDS_ALL: |
| 153 |
category_list.append(entry) |
| 154 |
else: |
| 155 |
# one or more categories specified |
| 156 |
category_list = module.params["category"] |
| 157 |
|
| 158 |
for category in category_list: |
| 159 |
command_list = [] |
| 160 |
# Build Command list for each Category |
| 161 |
if category in CATEGORY_COMMANDS_ALL: |
| 162 |
if not module.params["command"]: |
| 163 |
# True if we don't specify a command --> use default |
| 164 |
command_list.append(CATEGORY_COMMANDS_DEFAULT[category]) |
| 165 |
elif "all" in module.params["command"]: |
| 166 |
for entry in CATEGORY_COMMANDS_ALL[category]: |
| 167 |
command_list.append(entry) |
| 168 |
# one or more commands |
| 169 |
else: |
| 170 |
command_list = module.params["command"] |
| 171 |
# Verify that all commands are valid |
| 172 |
for cmd in command_list: |
| 173 |
# Fail if even one command given is invalid |
| 174 |
if cmd not in CATEGORY_COMMANDS_ALL[category]: |
| 175 |
module.fail_json(msg=f"Invalid Command: {cmd}") |
| 176 |
else: |
| 177 |
# Fail if even one category given is invalid |
| 178 |
module.fail_json(msg=f"Invalid Category: {category}") |
| 179 |
|
| 180 |
# Organize by Categories / Commands |
| 181 |
if category == "Sessions": |
| 182 |
for command in command_list: |
| 183 |
if command == "GetiLOSessions": |
| 184 |
result[command] = rf_utils.get_ilo_sessions() |
| 185 |
|
| 186 |
module.exit_json(ilo_redfish_info=result) |
| 187 |
|
| 188 |
|
| 189 |
if __name__ == "__main__": |
| 190 |
main() |