| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright (c) 2022 Western Digital Corporation |
| 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: wdc_redfish_command |
| 11 |
short_description: Manages WDC UltraStar Data102 Out-Of-Band controllers using Redfish APIs |
| 12 |
version_added: 5.4.0 |
| 13 |
description: |
| 14 |
- Builds Redfish URIs locally and sends them to remote OOB controllers to perform an action. |
| 15 |
- Manages OOB controller firmware. For example, Firmware Activate, Update and Activate. |
| 16 |
extends_documentation_fragment: |
| 17 |
- community.general._attributes |
| 18 |
- community.general._redfish |
| 19 |
attributes: |
| 20 |
check_mode: |
| 21 |
support: full |
| 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 |
description: |
| 38 |
- Base URI of OOB controller. Must include this or O(ioms). |
| 39 |
type: str |
| 40 |
ioms: |
| 41 |
description: |
| 42 |
- List of IOM FQDNs for the enclosure. Must include this or O(baseuri). |
| 43 |
type: list |
| 44 |
elements: str |
| 45 |
username: |
| 46 |
description: |
| 47 |
- User for authentication with OOB controller. |
| 48 |
type: str |
| 49 |
password: |
| 50 |
description: |
| 51 |
- Password for authentication with OOB controller. |
| 52 |
type: str |
| 53 |
auth_token: |
| 54 |
description: |
| 55 |
- Security token for authentication with OOB controller. |
| 56 |
type: str |
| 57 |
timeout: |
| 58 |
description: |
| 59 |
- Timeout in seconds for URL requests to OOB controller. |
| 60 |
default: 10 |
| 61 |
type: int |
| 62 |
resource_id: |
| 63 |
description: |
| 64 |
- ID of the component to modify, such as V(Enclosure), V(IOModuleAFRU), V(PowerSupplyBFRU), V(FanExternalFRU3), or V(FanInternalFRU). |
| 65 |
type: str |
| 66 |
version_added: 5.4.0 |
| 67 |
update_image_uri: |
| 68 |
description: |
| 69 |
- The URI of the image for the update. |
| 70 |
type: str |
| 71 |
update_creds: |
| 72 |
description: |
| 73 |
- The credentials for retrieving the update image. |
| 74 |
type: dict |
| 75 |
suboptions: |
| 76 |
username: |
| 77 |
description: |
| 78 |
- The username for retrieving the update image. |
| 79 |
type: str |
| 80 |
password: |
| 81 |
description: |
| 82 |
- The password for retrieving the update image. |
| 83 |
type: str |
| 84 |
validate_certs: |
| 85 |
version_added: 10.6.0 |
| 86 |
ca_path: |
| 87 |
version_added: 10.6.0 |
| 88 |
ciphers: |
| 89 |
version_added: 10.6.0 |
| 90 |
notes: |
| 91 |
- In the inventory, you can specify baseuri or ioms. See the EXAMPLES section. |
| 92 |
- Ioms is a list of FQDNs for the enclosure's IOMs. |
| 93 |
author: Mike Moerk (@mikemoerk) |
| 94 |
""" |
| 95 |
|
| 96 |
EXAMPLES = r""" |
| 97 |
- name: Firmware Activate (required after SimpleUpdate to apply the new firmware) |
| 98 |
community.general.wdc_redfish_command: |
| 99 |
category: Update |
| 100 |
command: FWActivate |
| 101 |
ioms: "{{ ioms }}" |
| 102 |
username: "{{ username }}" |
| 103 |
password: "{{ password }}" |
| 104 |
|
| 105 |
- name: Firmware Activate with individual IOMs specified |
| 106 |
community.general.wdc_redfish_command: |
| 107 |
category: Update |
| 108 |
command: FWActivate |
| 109 |
ioms: |
| 110 |
- iom1.wdc.com |
| 111 |
- iom2.wdc.com |
| 112 |
username: "{{ username }}" |
| 113 |
password: "{{ password }}" |
| 114 |
|
| 115 |
- name: Firmware Activate with baseuri specified |
| 116 |
community.general.wdc_redfish_command: |
| 117 |
category: Update |
| 118 |
command: FWActivate |
| 119 |
baseuri: "iom1.wdc.com" |
| 120 |
username: "{{ username }}" |
| 121 |
password: "{{ password }}" |
| 122 |
|
| 123 |
|
| 124 |
- name: Update and Activate (orchestrates firmware update and activation with a single command) |
| 125 |
community.general.wdc_redfish_command: |
| 126 |
category: Update |
| 127 |
command: UpdateAndActivate |
| 128 |
ioms: "{{ ioms }}" |
| 129 |
username: "{{ username }}" |
| 130 |
password: "{{ password }}" |
| 131 |
update_image_uri: "{{ update_image_uri }}" |
| 132 |
update_creds: |
| 133 |
username: operator |
| 134 |
password: supersecretpwd |
| 135 |
|
| 136 |
- name: Turn on enclosure indicator LED |
| 137 |
community.general.wdc_redfish_command: |
| 138 |
category: Chassis |
| 139 |
resource_id: Enclosure |
| 140 |
command: IndicatorLedOn |
| 141 |
username: "{{ username }}" |
| 142 |
password: "{{ password }}" |
| 143 |
|
| 144 |
- name: Turn off IOM A indicator LED |
| 145 |
community.general.wdc_redfish_command: |
| 146 |
category: Chassis |
| 147 |
resource_id: IOModuleAFRU |
| 148 |
command: IndicatorLedOff |
| 149 |
username: "{{ username }}" |
| 150 |
password: "{{ password }}" |
| 151 |
|
| 152 |
- name: Turn on Power Supply B indicator LED |
| 153 |
community.general.wdc_redfish_command: |
| 154 |
category: Chassis |
| 155 |
resource_id: PowerSupplyBFRU |
| 156 |
command: IndicatorLedOn |
| 157 |
username: "{{ username }}" |
| 158 |
password: "{{ password }}" |
| 159 |
|
| 160 |
- name: Turn on External Fan 3 indicator LED |
| 161 |
community.general.wdc_redfish_command: |
| 162 |
category: Chassis |
| 163 |
resource_id: FanExternalFRU3 |
| 164 |
command: IndicatorLedOn |
| 165 |
username: "{{ username }}" |
| 166 |
password: "{{ password }}" |
| 167 |
|
| 168 |
- name: Turn on Internal Fan indicator LED |
| 169 |
community.general.wdc_redfish_command: |
| 170 |
category: Chassis |
| 171 |
resource_id: FanInternalFRU |
| 172 |
command: IndicatorLedOn |
| 173 |
username: "{{ username }}" |
| 174 |
password: "{{ password }}" |
| 175 |
|
| 176 |
- name: Set chassis to Low Power Mode |
| 177 |
community.general.wdc_redfish_command: |
| 178 |
category: Chassis |
| 179 |
resource_id: Enclosure |
| 180 |
command: PowerModeLow |
| 181 |
|
| 182 |
- name: Set chassis to Normal Power Mode |
| 183 |
community.general.wdc_redfish_command: |
| 184 |
category: Chassis |
| 185 |
resource_id: Enclosure |
| 186 |
command: PowerModeNormal |
| 187 |
""" |
| 188 |
|
| 189 |
RETURN = r""" |
| 190 |
msg: |
| 191 |
description: Message with action result or error description. |
| 192 |
returned: always |
| 193 |
type: str |
| 194 |
sample: "Action was successful" |
| 195 |
""" |
| 196 |
|
| 197 |
from ansible.module_utils.basic import AnsibleModule |
| 198 |
from ansible.module_utils.common.text.converters import to_native |
| 199 |
|
| 200 |
from ansible_collections.community.general.plugins.module_utils._redfish_utils import REDFISH_COMMON_ARGUMENT_SPEC |
| 201 |
from ansible_collections.community.general.plugins.module_utils._wdc_redfish_utils import WdcRedfishUtils |
| 202 |
|
| 203 |
CATEGORY_COMMANDS_ALL = { |
| 204 |
"Update": ["FWActivate", "UpdateAndActivate"], |
| 205 |
"Chassis": [ |
| 206 |
"IndicatorLedOn", |
| 207 |
"IndicatorLedOff", |
| 208 |
"PowerModeLow", |
| 209 |
"PowerModeNormal", |
| 210 |
], |
| 211 |
} |
| 212 |
|
| 213 |
|
| 214 |
def main(): |
| 215 |
argument_spec = dict( |
| 216 |
category=dict(required=True), |
| 217 |
command=dict(required=True, type="list", elements="str"), |
| 218 |
ioms=dict(type="list", elements="str"), |
| 219 |
baseuri=dict(), |
| 220 |
username=dict(), |
| 221 |
password=dict(no_log=True), |
| 222 |
auth_token=dict(no_log=True), |
| 223 |
update_creds=dict(type="dict", options=dict(username=dict(), password=dict(no_log=True))), |
| 224 |
resource_id=dict(), |
| 225 |
update_image_uri=dict(), |
| 226 |
timeout=dict(type="int", default=10), |
| 227 |
) |
| 228 |
argument_spec.update(REDFISH_COMMON_ARGUMENT_SPEC) |
| 229 |
module = AnsibleModule( |
| 230 |
argument_spec, |
| 231 |
required_together=[ |
| 232 |
("username", "password"), |
| 233 |
], |
| 234 |
required_one_of=[("username", "auth_token"), ("baseuri", "ioms")], |
| 235 |
mutually_exclusive=[ |
| 236 |
("username", "auth_token"), |
| 237 |
], |
| 238 |
supports_check_mode=True, |
| 239 |
) |
| 240 |
|
| 241 |
category = module.params["category"] |
| 242 |
command_list = module.params["command"] |
| 243 |
|
| 244 |
# admin credentials used for authentication |
| 245 |
creds = {"user": module.params["username"], "pswd": module.params["password"], "token": module.params["auth_token"]} |
| 246 |
|
| 247 |
# timeout |
| 248 |
timeout = module.params["timeout"] |
| 249 |
|
| 250 |
# Resource to modify |
| 251 |
resource_id = module.params["resource_id"] |
| 252 |
|
| 253 |
# Check that Category is valid |
| 254 |
if category not in CATEGORY_COMMANDS_ALL: |
| 255 |
module.fail_json( |
| 256 |
msg=f"Invalid Category '{category}'. Valid Categories = {sorted(CATEGORY_COMMANDS_ALL.keys())}" |
| 257 |
) |
| 258 |
|
| 259 |
# Check that all commands are valid |
| 260 |
for cmd in command_list: |
| 261 |
# Fail if even one command given is invalid |
| 262 |
if cmd not in CATEGORY_COMMANDS_ALL[category]: |
| 263 |
module.fail_json(msg=f"Invalid Command '{cmd}'. Valid Commands = {CATEGORY_COMMANDS_ALL[category]}") |
| 264 |
|
| 265 |
# Build root URI(s) |
| 266 |
if module.params.get("baseuri") is not None: |
| 267 |
root_uris = [f"https://{module.params['baseuri']}"] |
| 268 |
else: |
| 269 |
root_uris = [f"https://{iom}" for iom in module.params["ioms"]] |
| 270 |
rf_utils = WdcRedfishUtils(creds, root_uris, timeout, module, resource_id=resource_id, data_modification=True) |
| 271 |
|
| 272 |
# Organize by Categories / Commands |
| 273 |
|
| 274 |
if category == "Update": |
| 275 |
# execute only if we find UpdateService resources |
| 276 |
resource = rf_utils._find_updateservice_resource() |
| 277 |
if resource["ret"] is False: |
| 278 |
module.fail_json(msg=resource["msg"]) |
| 279 |
# update options |
| 280 |
update_opts = {"update_creds": module.params["update_creds"]} |
| 281 |
for command in command_list: |
| 282 |
if command == "FWActivate": |
| 283 |
if module.check_mode: |
| 284 |
result = {"ret": True, "changed": True, "msg": "FWActivate not performed in check mode."} |
| 285 |
else: |
| 286 |
result = rf_utils.firmware_activate(update_opts) |
| 287 |
elif command == "UpdateAndActivate": |
| 288 |
update_opts["update_image_uri"] = module.params["update_image_uri"] |
| 289 |
result = rf_utils.update_and_activate(update_opts) |
| 290 |
|
| 291 |
elif category == "Chassis": |
| 292 |
result = rf_utils._find_chassis_resource() |
| 293 |
if result["ret"] is False: |
| 294 |
module.fail_json(msg=result["msg"]) |
| 295 |
|
| 296 |
led_commands = ["IndicatorLedOn", "IndicatorLedOff"] |
| 297 |
|
| 298 |
# Check if more than one led_command is present |
| 299 |
num_led_commands = sum([command in led_commands for command in command_list]) |
| 300 |
if num_led_commands > 1: |
| 301 |
result = {"ret": False, "msg": "Only one IndicatorLed command should be sent at a time."} |
| 302 |
else: |
| 303 |
for command in command_list: |
| 304 |
if command.startswith("IndicatorLed"): |
| 305 |
result = rf_utils.manage_chassis_indicator_led(command) |
| 306 |
elif command.startswith("PowerMode"): |
| 307 |
result = rf_utils.manage_chassis_power_mode(command) |
| 308 |
|
| 309 |
if result["ret"] is False: |
| 310 |
module.fail_json(msg=to_native(result["msg"])) |
| 311 |
else: |
| 312 |
del result["ret"] |
| 313 |
changed = result.get("changed", True) |
| 314 |
session = result.get("session", dict()) |
| 315 |
module.exit_json( |
| 316 |
changed=changed, |
| 317 |
session=session, |
| 318 |
msg="Action was successful" |
| 319 |
if not module.check_mode |
| 320 |
else result.get("msg", "No action performed in check mode."), |
| 321 |
) |
| 322 |
|
| 323 |
|
| 324 |
if __name__ == "__main__": |
| 325 |
main() |