| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
from __future__ import (absolute_import, division, print_function) |
| 13 |
__metaclass__ = type |
| 14 |
|
| 15 |
DOCUMENTATION = """ |
| 16 |
--- |
| 17 |
module: ome_device_power_settings |
| 18 |
short_description: Configure chassis power settings on OpenManage Enterprise Modular |
| 19 |
description: This module allows to configure the chassis power settings on OpenManage Enterprise Modular. |
| 20 |
version_added: "4.2.0" |
| 21 |
extends_documentation_fragment: |
| 22 |
- dellemc.openmanage.omem_auth_options |
| 23 |
options: |
| 24 |
device_id: |
| 25 |
type: int |
| 26 |
description: |
| 27 |
- The ID of the chassis for which the settings need to be updated. |
| 28 |
- If the device ID is not specified, this module updates the power settings for the I(hostname). |
| 29 |
- I(device_id) is mutually exclusive with I(device_service_tag). |
| 30 |
device_service_tag: |
| 31 |
type: str |
| 32 |
description: |
| 33 |
- The service tag of the chassis for which the setting needs to be updated. |
| 34 |
- If the device service tag is not specified, this module updates the power settings for the I(hostname). |
| 35 |
- I(device_service_tag) is mutually exclusive with I(device_id). |
| 36 |
power_configuration: |
| 37 |
description: The settings for Power configuration. |
| 38 |
type: dict |
| 39 |
suboptions: |
| 40 |
enable_power_cap: |
| 41 |
type: bool |
| 42 |
description: Enables or disables the Power Cap Settings. |
| 43 |
required: true |
| 44 |
power_cap: |
| 45 |
type: int |
| 46 |
description: |
| 47 |
- The maximum power consumption limit of the device. Specify the consumption limit in Watts. |
| 48 |
- This is required if I(enable_power_cap) is set to true. |
| 49 |
redundancy_configuration: |
| 50 |
description: The settings for Redundancy configuration. |
| 51 |
type: dict |
| 52 |
suboptions: |
| 53 |
redundancy_policy: |
| 54 |
type: str |
| 55 |
description: |
| 56 |
- The choices to configure the redundancy policy. |
| 57 |
- C(NO_REDUNDANCY) no redundancy policy is used. |
| 58 |
- C(GRID_REDUNDANCY) to distributes power by dividing the PSUs into two grids. |
| 59 |
- C(PSU_REDUNDANCY) to distribute power between all the PSUs. |
| 60 |
choices: ['NO_REDUNDANCY', 'GRID_REDUNDANCY', 'PSU_REDUNDANCY'] |
| 61 |
default: NO_REDUNDANCY |
| 62 |
hot_spare_configuration: |
| 63 |
description: The settings for Hot Spare configuration. |
| 64 |
type: dict |
| 65 |
suboptions: |
| 66 |
enable_hot_spare: |
| 67 |
type: bool |
| 68 |
description: Enables or disables Hot Spare configuration to facilitate voltage regulation when power |
| 69 |
utilized by the Power Supply Unit (PSU) is low. |
| 70 |
required: true |
| 71 |
primary_grid: |
| 72 |
type: str |
| 73 |
description: |
| 74 |
- The choices for PSU grid. |
| 75 |
- C(GRID_1) Hot Spare on Grid 1. |
| 76 |
- C(GRID_2) Hot Spare on Grid 2. |
| 77 |
choices: ['GRID_1', 'GRID_2'] |
| 78 |
default: GRID_1 |
| 79 |
requirements: |
| 80 |
- "python >= 3.9.6" |
| 81 |
author: |
| 82 |
- "Felix Stephen (@felixs88)" |
| 83 |
notes: |
| 84 |
- Run this module from a system that has direct access to Dell OpenManage Enterprise Modular. |
| 85 |
- This module supports C(check_mode). |
| 86 |
""" |
| 87 |
|
| 88 |
EXAMPLES = """ |
| 89 |
--- |
| 90 |
- name: Update power configuration settings of a chassis using the device ID. |
| 91 |
dellemc.openmanage.ome_device_power_settings: |
| 92 |
hostname: "192.168.0.1" |
| 93 |
username: "username" |
| 94 |
password: "password" |
| 95 |
ca_path: "/path/to/ca_cert.pem" |
| 96 |
device_id: 25011 |
| 97 |
power_configuration: |
| 98 |
enable_power_cap: true |
| 99 |
power_cap: 3424 |
| 100 |
|
| 101 |
- name: Update redundancy configuration settings of a chassis using the device service tag. |
| 102 |
dellemc.openmanage.ome_device_power_settings: |
| 103 |
hostname: "192.168.0.1" |
| 104 |
username: "username" |
| 105 |
password: "password" |
| 106 |
ca_path: "/path/to/ca_cert.pem" |
| 107 |
device_service_tag: GHRT2RL |
| 108 |
redundancy_configuration: |
| 109 |
redundancy_policy: GRID_REDUNDANCY |
| 110 |
|
| 111 |
- name: Update hot spare configuration settings of a chassis using device ID. |
| 112 |
dellemc.openmanage.ome_device_power_settings: |
| 113 |
hostname: "192.168.0.1" |
| 114 |
username: "username" |
| 115 |
password: "password" |
| 116 |
ca_path: "/path/to/ca_cert.pem" |
| 117 |
device_id: 25012 |
| 118 |
hot_spare_configuration: |
| 119 |
enable_hot_spare: true |
| 120 |
primary_grid: GRID_1 |
| 121 |
""" |
| 122 |
|
| 123 |
RETURN = """ |
| 124 |
--- |
| 125 |
msg: |
| 126 |
type: str |
| 127 |
description: Overall status of the device power settings. |
| 128 |
returned: always |
| 129 |
sample: "Successfully updated the power settings." |
| 130 |
power_details: |
| 131 |
type: dict |
| 132 |
description: returned when power settings are updated successfully. |
| 133 |
returned: success |
| 134 |
sample: { |
| 135 |
"EnableHotSpare": true, |
| 136 |
"EnablePowerCapSettings": true, |
| 137 |
"MaxPowerCap": "3424", |
| 138 |
"MinPowerCap": "3291", |
| 139 |
"PowerCap": "3425", |
| 140 |
"PrimaryGrid": "GRID_1", |
| 141 |
"RedundancyPolicy": "NO_REDUNDANCY", |
| 142 |
"SettingType": "Power" |
| 143 |
} |
| 144 |
error_info: |
| 145 |
description: Details of the HTTP Error. |
| 146 |
returned: on HTTP error |
| 147 |
type: dict |
| 148 |
sample: { |
| 149 |
"error": { |
| 150 |
"code": "Base.1.0.GeneralError", |
| 151 |
"message": "A general error has occurred. See ExtendedInfo for more information.", |
| 152 |
"@Message.ExtendedInfo": [ |
| 153 |
{ |
| 154 |
"MessageId": "GEN1234", |
| 155 |
"RelatedProperties": [], |
| 156 |
"Message": "Unable to process the request because an error occurred.", |
| 157 |
"MessageArgs": [], |
| 158 |
"Severity": "Critical", |
| 159 |
"Resolution": "Retry the operation. If the issue persists, contact your system administrator." |
| 160 |
} |
| 161 |
] |
| 162 |
} |
| 163 |
} |
| 164 |
""" |
| 165 |
|
| 166 |
|
| 167 |
import json |
| 168 |
import socket |
| 169 |
import copy |
| 170 |
from ssl import SSLError |
| 171 |
from ansible.module_utils.six.moves.urllib.error import URLError, HTTPError |
| 172 |
from ansible.module_utils.urls import ConnectionError |
| 173 |
from ansible_collections.dellemc.openmanage.plugins.module_utils.ome import RestOME, OmeAnsibleModule |
| 174 |
POWER_API = "DeviceService/Devices({0})/Settings('Power')" |
| 175 |
DEVICE_URI = "DeviceService/Devices" |
| 176 |
DOMAIN_URI = "ManagementDomainService/Domains" |
| 177 |
DEVICE_FAIL_MSG = "Unable to complete the operation because the entered target device {0} '{1}' is invalid." |
| 178 |
CONFIG_FAIL_MSG = "one of the following is required: power_configuration, " \ |
| 179 |
"redundancy_configuration, hot_spare_configuration" |
| 180 |
CHANGES_FOUND = "Changes found to be applied." |
| 181 |
NO_CHANGES_FOUND = "No changes found to be applied." |
| 182 |
SUCCESS_MSG = "Successfully updated the power settings." |
| 183 |
FETCH_FAIL_MSG = "Failed to fetch the device information." |
| 184 |
POWER_FAIL_MSG = "Unable to complete the operation because the power settings " \ |
| 185 |
"are not supported on the specified device." |
| 186 |
DOMAIN_FAIL_MSG = "The device location settings operation is supported only on " \ |
| 187 |
"OpenManage Enterprise Modular." |
| 188 |
|
| 189 |
|
| 190 |
def check_domain_service(module, rest_obj): |
| 191 |
try: |
| 192 |
rest_obj.invoke_request("GET", DOMAIN_URI, api_timeout=5) |
| 193 |
except HTTPError as err: |
| 194 |
err_message = json.load(err) |
| 195 |
if err_message["error"]["@Message.ExtendedInfo"][0]["MessageId"] == "CGEN1006": |
| 196 |
module.exit_json(msg=DOMAIN_FAIL_MSG, failed=True) |
| 197 |
return |
| 198 |
|
| 199 |
|
| 200 |
def get_ip_from_host(hostname): |
| 201 |
ipaddr = hostname |
| 202 |
try: |
| 203 |
result = socket.getaddrinfo(hostname, None) |
| 204 |
last_element = result[-1] |
| 205 |
ip_address = last_element[-1][0] |
| 206 |
if ip_address: |
| 207 |
ipaddr = ip_address |
| 208 |
except socket.gaierror: |
| 209 |
ipaddr = hostname |
| 210 |
except Exception: |
| 211 |
ipaddr = hostname |
| 212 |
return ipaddr |
| 213 |
|
| 214 |
|
| 215 |
def get_chassis_device(module, rest_obj): |
| 216 |
key, value = None, None |
| 217 |
ipaddress = get_ip_from_host(module.params["hostname"]) |
| 218 |
resp = rest_obj.invoke_request("GET", DOMAIN_URI) |
| 219 |
for data in resp.json_data["value"]: |
| 220 |
if ipaddress in data["PublicAddress"]: |
| 221 |
key, value = ("Id", data["DeviceId"]) |
| 222 |
break |
| 223 |
else: |
| 224 |
module.exit_json(msg=FETCH_FAIL_MSG, failed=True) |
| 225 |
return key, value |
| 226 |
|
| 227 |
|
| 228 |
def check_mode_validation(module, loc_data): |
| 229 |
power_data = {"PowerCap": loc_data.get("PowerCap"), "MinPowerCap": loc_data["MinPowerCap"], |
| 230 |
"MaxPowerCap": loc_data["MaxPowerCap"], "RedundancyPolicy": loc_data.get("RedundancyPolicy"), |
| 231 |
"EnablePowerCapSettings": loc_data["EnablePowerCapSettings"], |
| 232 |
"EnableHotSpare": loc_data["EnableHotSpare"], "PrimaryGrid": loc_data.get("PrimaryGrid")} |
| 233 |
cloned_data = copy.deepcopy(power_data) |
| 234 |
if module.params.get("power_configuration") is not None: |
| 235 |
if module.params["power_configuration"]["enable_power_cap"] is None: |
| 236 |
module.exit_json(msg="missing parameter: enable_power_cap", failed=True) |
| 237 |
enable_power_cap = module.params["power_configuration"]["enable_power_cap"] |
| 238 |
power_cap = module.params["power_configuration"].get("power_cap") |
| 239 |
if enable_power_cap is True: |
| 240 |
cloned_data.update({"EnablePowerCapSettings": enable_power_cap, "PowerCap": str(power_cap)}) |
| 241 |
else: |
| 242 |
cloned_data.update({"EnablePowerCapSettings": enable_power_cap}) |
| 243 |
if module.params.get("redundancy_configuration") is not None: |
| 244 |
cloned_data.update({"RedundancyPolicy": module.params["redundancy_configuration"]["redundancy_policy"]}) |
| 245 |
if module.params.get("hot_spare_configuration") is not None: |
| 246 |
if module.params["hot_spare_configuration"]["enable_hot_spare"] is None: |
| 247 |
module.exit_json(msg="missing parameter: enable_hot_spare", failed=True) |
| 248 |
enable_hot_spare = module.params["hot_spare_configuration"]["enable_hot_spare"] |
| 249 |
primary_grid = module.params["hot_spare_configuration"].get("primary_grid") |
| 250 |
if enable_hot_spare is True: |
| 251 |
cloned_data.update({"EnableHotSpare": enable_hot_spare, "PrimaryGrid": primary_grid}) |
| 252 |
else: |
| 253 |
cloned_data.update({"EnableHotSpare": enable_hot_spare}) |
| 254 |
power_diff = bool(set(power_data.items()) ^ set(cloned_data.items())) |
| 255 |
if not power_diff and not module.check_mode: |
| 256 |
module.exit_json(msg=NO_CHANGES_FOUND) |
| 257 |
elif not power_diff and module.check_mode: |
| 258 |
module.exit_json(msg=NO_CHANGES_FOUND) |
| 259 |
elif power_diff and module.check_mode: |
| 260 |
module.exit_json(msg=CHANGES_FOUND, changed=True) |
| 261 |
cloned_data.update({"SettingType": "Power"}) |
| 262 |
return cloned_data |
| 263 |
|
| 264 |
|
| 265 |
def fetch_device_details(module, rest_obj): |
| 266 |
device_id, tag, final_resp = module.params.get("device_id"), module.params.get("device_service_tag"), {} |
| 267 |
if device_id is None and tag is None: |
| 268 |
key, value = get_chassis_device(module, rest_obj) |
| 269 |
device_id = value |
| 270 |
else: |
| 271 |
key, value = ("Id", device_id) if device_id is not None else ("DeviceServiceTag", tag) |
| 272 |
param_value = "{0} eq {1}".format(key, value) if key == "Id" else "{0} eq '{1}'".format(key, value) |
| 273 |
resp = rest_obj.invoke_request("GET", DEVICE_URI, query_param={"$filter": param_value}) |
| 274 |
resp_data = resp.json_data.get("value") |
| 275 |
rename_key = "id" if key == "Id" else "service tag" |
| 276 |
if not resp_data: |
| 277 |
module.exit_json(msg=DEVICE_FAIL_MSG.format(rename_key, value), failed=True) |
| 278 |
if key == "DeviceServiceTag" and resp_data[0]["DeviceServiceTag"] == tag: |
| 279 |
device_id = resp_data[0]["Id"] |
| 280 |
elif key == "Id" and resp_data[0]["Id"] == device_id: |
| 281 |
device_id = resp_data[0]["Id"] |
| 282 |
else: |
| 283 |
module.exit_json(msg=DEVICE_FAIL_MSG.format(rename_key, value), failed=True) |
| 284 |
try: |
| 285 |
loc_resp = rest_obj.invoke_request("GET", POWER_API.format(device_id)) |
| 286 |
except HTTPError as err: |
| 287 |
if err.code == 404: |
| 288 |
module.exit_json(msg=POWER_FAIL_MSG, failed=True) |
| 289 |
err_message = json.load(err) |
| 290 |
error_msg = err_message.get('error', {}).get('@Message.ExtendedInfo') |
| 291 |
if error_msg and error_msg[0].get("MessageId") == "CGEN1004": |
| 292 |
module.exit_json(msg=POWER_FAIL_MSG, failed=True) |
| 293 |
else: |
| 294 |
payload = check_mode_validation(module, loc_resp.json_data) |
| 295 |
final_resp = rest_obj.invoke_request("PUT", POWER_API.format(device_id), data=payload) |
| 296 |
return final_resp |
| 297 |
|
| 298 |
|
| 299 |
def main(): |
| 300 |
power_options = {"enable_power_cap": {"type": "bool", "required": True}, |
| 301 |
"power_cap": {"type": "int", "required": False}} |
| 302 |
redundancy_options = {"redundancy_policy": {"type": "str", "default": "NO_REDUNDANCY", |
| 303 |
"choices": ["NO_REDUNDANCY", "GRID_REDUNDANCY", "PSU_REDUNDANCY"]}} |
| 304 |
hot_spare_options = {"enable_hot_spare": {"required": True, "type": "bool"}, |
| 305 |
"primary_grid": {"required": False, "type": "str", "default": "GRID_1", |
| 306 |
"choices": ["GRID_1", "GRID_2"]}} |
| 307 |
specs = { |
| 308 |
"device_id": {"required": False, "type": "int"}, |
| 309 |
"device_service_tag": {"required": False, "type": "str"}, |
| 310 |
"power_configuration": {"type": "dict", "required": False, "options": power_options, |
| 311 |
"required_if": [["enable_power_cap", True, ("power_cap",), True]]}, |
| 312 |
"redundancy_configuration": {"type": "dict", "required": False, "options": redundancy_options}, |
| 313 |
"hot_spare_configuration": {"type": "dict", "required": False, "options": hot_spare_options, |
| 314 |
"required_if": [["enable_hot_spare", True, ("primary_grid",)]]}, |
| 315 |
} |
| 316 |
|
| 317 |
module = OmeAnsibleModule( |
| 318 |
argument_spec=specs, |
| 319 |
mutually_exclusive=[('device_id', 'device_service_tag')], |
| 320 |
required_one_of=[["power_configuration", "redundancy_configuration", "hot_spare_configuration"]], |
| 321 |
supports_check_mode=True, |
| 322 |
) |
| 323 |
try: |
| 324 |
if not any([module.params.get("power_configuration"), module.params.get("redundancy_configuration"), |
| 325 |
module.params.get("hot_spare_configuration")]): |
| 326 |
module.exit_json(msg=CONFIG_FAIL_MSG, failed=True) |
| 327 |
with RestOME(module.params, req_session=True) as rest_obj: |
| 328 |
check_domain_service(module, rest_obj) |
| 329 |
resp = fetch_device_details(module, rest_obj) |
| 330 |
module.exit_json(msg=SUCCESS_MSG, power_details=resp.json_data, changed=True) |
| 331 |
except HTTPError as err: |
| 332 |
module.exit_json(msg=str(err), error_info=json.load(err), failed=True) |
| 333 |
except URLError as err: |
| 334 |
module.exit_json(msg=str(err), unreachable=True) |
| 335 |
except (IOError, ValueError, SSLError, TypeError, ConnectionError, AttributeError, IndexError, KeyError, OSError) as err: |
| 336 |
module.exit_json(msg=str(err), failed=True) |
| 337 |
|
| 338 |
|
| 339 |
if __name__ == '__main__': |
| 340 |
main() |