| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
from __future__ import (absolute_import, division, print_function) |
| 14 |
|
| 15 |
__metaclass__ = type |
| 16 |
|
| 17 |
DOCUMENTATION = r''' |
| 18 |
--- |
| 19 |
module: idrac_attributes |
| 20 |
short_description: Configure the iDRAC attributes. |
| 21 |
version_added: "6.0.0" |
| 22 |
description: |
| 23 |
- This module allows to configure the iDRAC attributes. |
| 24 |
extends_documentation_fragment: |
| 25 |
- dellemc.openmanage.idrac_x_auth_options |
| 26 |
options: |
| 27 |
idrac_attributes: |
| 28 |
type: dict |
| 29 |
description: |
| 30 |
- "Dictionary of iDRAC attributes and value. The attributes should be |
| 31 |
part of the Integrated Dell Remote Access Controller Attribute Registry. |
| 32 |
To view the list of attributes in Attribute Registry for iDRAC9 and above, |
| 33 |
see, U(https://I(idrac_ip)/redfish/v1/Managers/iDRAC.Embedded.1/Oem/Dell/DellAttributes/iDRAC.Embedded.1) |
| 34 |
and U(https://I(idrac_ip)/redfish/v1/Registries/ManagerAttributeRegistry)." |
| 35 |
- "For iDRAC8 based servers, derive the manager attribute name from Server Configuration Profile. |
| 36 |
If the manager attribute name in Server Configuration Profile is <GroupName>.<Instance>#<AttributeName> |
| 37 |
(for Example, 'SNMP.1#AgentCommunity') then the equivalent attribute name for Redfish is |
| 38 |
<GroupName>.<Instance>.<AttributeName> (for Example, 'SNMP.1.AgentCommunity')." |
| 39 |
system_attributes: |
| 40 |
type: dict |
| 41 |
description: |
| 42 |
- "Dictionary of System attributes and value. The attributes should be |
| 43 |
part of the Integrated Dell Remote Access Controller Attribute Registry. To view the list of attributes in Attribute Registry for iDRAC9 and above, |
| 44 |
see, U(https://I(idrac_ip)/redfish/v1/Managers/iDRAC.Embedded.1/Oem/Dell/DellAttributes/System.Embedded.1) |
| 45 |
and U(https://I(idrac_ip)/redfish/v1/Registries/ManagerAttributeRegistry)." |
| 46 |
- "For iDRAC8 based servers, derive the manager attribute name from Server Configuration Profile. |
| 47 |
If the manager attribute name in Server Configuration Profile is <GroupName>.<Instance>#<AttributeName> |
| 48 |
(for Example, 'ThermalSettings.1#ThermalProfile') then the equivalent attribute name for Redfish is |
| 49 |
<GroupName>.<Instance>.<AttributeName> (for Example, 'ThermalSettings.1.ThermalProfile')." |
| 50 |
lifecycle_controller_attributes: |
| 51 |
type: dict |
| 52 |
description: |
| 53 |
- "Dictionary of Lifecycle Controller attributes and value. The attributes should be |
| 54 |
part of the Integrated Dell Remote Access Controller Attribute Registry.To view the list of attributes in Attribute Registry for iDRAC9 and above, |
| 55 |
see, U(https://I(idrac_ip)/redfish/v1/Managers/iDRAC.Embedded.1/Oem/Dell/DellAttributes/LifecycleController.Embedded.1) |
| 56 |
and U(https://I(idrac_ip)/redfish/v1/Registries/ManagerAttributeRegistry)." |
| 57 |
- "For iDRAC8 based servers, derive the manager attribute name from Server Configuration Profile. |
| 58 |
If the manager attribute name in Server Configuration Profile is <GroupName>.<Instance>#<AttributeName> |
| 59 |
(for Example, 'LCAttributes.1#AutoUpdate') then the equivalent attribute name for Redfish is |
| 60 |
<GroupName>.<Instance>.<AttributeName> (for Example, 'LCAttributes.1.AutoUpdate')." |
| 61 |
resource_id: |
| 62 |
type: str |
| 63 |
description: Redfish ID of the resource. |
| 64 |
requirements: |
| 65 |
- "python >= 3.9.6" |
| 66 |
author: |
| 67 |
- Husniya Abdul Hameed (@husniya-hameed) |
| 68 |
- Felix Stephen (@felixs88) |
| 69 |
notes: |
| 70 |
- Run this module from a system that has direct access to Dell iDRAC. |
| 71 |
- This module supports C(check_mode). |
| 72 |
- For iDRAC8 based servers, the value provided for the attributes are not be validated. |
| 73 |
Ensure appropriate values are passed. |
| 74 |
''' |
| 75 |
|
| 76 |
EXAMPLES = """ |
| 77 |
--- |
| 78 |
- name: Configure iDRAC attributes |
| 79 |
dellemc.openmanage.idrac_attributes: |
| 80 |
idrac_ip: "192.168.0.1" |
| 81 |
idrac_user: "user_name" |
| 82 |
idrac_password: "user_password" |
| 83 |
ca_path: "/path/to/ca_cert.pem" |
| 84 |
idrac_attributes: |
| 85 |
SNMP.1.AgentCommunity: public |
| 86 |
|
| 87 |
- name: Configure System attributes |
| 88 |
dellemc.openmanage.idrac_attributes: |
| 89 |
idrac_ip: "192.168.0.1" |
| 90 |
idrac_user: "user_name" |
| 91 |
idrac_password: "user_password" |
| 92 |
ca_path: "/path/to/ca_cert.pem" |
| 93 |
system_attributes: |
| 94 |
ThermalSettings.1.ThermalProfile: Sound Cap |
| 95 |
|
| 96 |
- name: Configure Lifecycle Controller attributes |
| 97 |
dellemc.openmanage.idrac_attributes: |
| 98 |
idrac_ip: "192.168.0.1" |
| 99 |
idrac_user: "user_name" |
| 100 |
idrac_password: "user_password" |
| 101 |
ca_path: "/path/to/ca_cert.pem" |
| 102 |
lifecycle_controller_attributes: |
| 103 |
LCAttributes.1.AutoUpdate: Enabled |
| 104 |
|
| 105 |
- name: Configure the iDRAC attributes for email alert settings. |
| 106 |
dellemc.openmanage.idrac_attributes: |
| 107 |
idrac_ip: "192.168.0.1" |
| 108 |
idrac_user: "user_name" |
| 109 |
idrac_password: "user_password" |
| 110 |
ca_path: "/path/to/ca_cert.pem" |
| 111 |
idrac_attributes: |
| 112 |
EmailAlert.1.CustomMsg: Display Message |
| 113 |
EmailAlert.1.Enable: Enabled |
| 114 |
EmailAlert.1.Address: [email protected] |
| 115 |
|
| 116 |
- name: Configure the iDRAC attributes for SNMP alert settings. |
| 117 |
dellemc.openmanage.idrac_attributes: |
| 118 |
idrac_ip: "192.168.0.1" |
| 119 |
idrac_user: "user_name" |
| 120 |
idrac_password: "user_password" |
| 121 |
ca_path: "/path/to/ca_cert.pem" |
| 122 |
idrac_attributes: |
| 123 |
SNMPAlert.1.Destination: 192.168.0.2 |
| 124 |
SNMPAlert.1.State: Enabled |
| 125 |
SNMPAlert.1.SNMPv3Username: username |
| 126 |
|
| 127 |
- name: Configure the iDRAC attributes for SMTP alert settings. |
| 128 |
dellemc.openmanage.idrac_attributes: |
| 129 |
idrac_ip: "192.168.0.1" |
| 130 |
idrac_user: "user_name" |
| 131 |
idrac_password: "user_password" |
| 132 |
ca_path: "/path/to/ca_cert.pem" |
| 133 |
idrac_attributes: |
| 134 |
RemoteHosts.1.SMTPServerIPAddress: 192.168.0.3 |
| 135 |
RemoteHosts.1.SMTPAuthentication: Enabled |
| 136 |
RemoteHosts.1.SMTPPort: 25 |
| 137 |
RemoteHosts.1.SMTPUserName: username |
| 138 |
RemoteHosts.1.SMTPPassword: password |
| 139 |
|
| 140 |
- name: Configure the iDRAC attributes for webserver settings. |
| 141 |
dellemc.openmanage.idrac_attributes: |
| 142 |
idrac_ip: "192.168.0.1" |
| 143 |
idrac_user: "user_name" |
| 144 |
idrac_password: "user_password" |
| 145 |
ca_path: "/path/to/ca_cert.pem" |
| 146 |
idrac_attributes: |
| 147 |
WebServer.1.SSLEncryptionBitLength: 128-Bit or higher |
| 148 |
WebServer.1.TLSProtocol: TLS 1.1 and Higher |
| 149 |
|
| 150 |
- name: Configure the iDRAC attributes for SNMP settings. |
| 151 |
dellemc.openmanage.idrac_attributes: |
| 152 |
idrac_ip: "192.168.0.1" |
| 153 |
idrac_user: "user_name" |
| 154 |
idrac_password: "user_password" |
| 155 |
ca_path: "/path/to/ca_cert.pem" |
| 156 |
idrac_attributes: |
| 157 |
SNMP.1.SNMPProtocol: All |
| 158 |
SNMP.1.AgentEnable: Enabled |
| 159 |
SNMP.1.TrapFormat: SNMPv1 |
| 160 |
SNMP.1.AlertPort: 162 |
| 161 |
SNMP.1.AgentCommunity: public |
| 162 |
|
| 163 |
- name: Configure the iDRAC LC attributes for collecting system inventory. |
| 164 |
dellemc.openmanage.idrac_attributes: |
| 165 |
idrac_ip: "192.168.0.1" |
| 166 |
idrac_user: "user_name" |
| 167 |
idrac_password: "user_password" |
| 168 |
ca_path: "/path/to/ca_cert.pem" |
| 169 |
lifecycle_controller_attributes: |
| 170 |
LCAttributes.1.CollectSystemInventoryOnRestart: Enabled |
| 171 |
|
| 172 |
- name: Configure the iDRAC system attributes for LCD configuration. |
| 173 |
dellemc.openmanage.idrac_attributes: |
| 174 |
idrac_ip: "192.168.0.1" |
| 175 |
idrac_user: "user_name" |
| 176 |
idrac_password: "user_password" |
| 177 |
ca_path: "/path/to/ca_cert.pem" |
| 178 |
system_attributes: |
| 179 |
LCD.1.Configuration: Service Tag |
| 180 |
LCD.1.vConsoleIndication: Enabled |
| 181 |
LCD.1.FrontPanelLocking: Full-Access |
| 182 |
LCD.1.UserDefinedString: custom string |
| 183 |
|
| 184 |
- name: Configure the iDRAC attributes for Timezone settings. |
| 185 |
dellemc.openmanage.idrac_attributes: |
| 186 |
idrac_ip: "192.168.0.1" |
| 187 |
idrac_user: "user_name" |
| 188 |
idrac_password: "user_password" |
| 189 |
ca_path: "/path/to/ca_cert.pem" |
| 190 |
idrac_attributes: |
| 191 |
Time.1.Timezone: CST6CDT |
| 192 |
NTPConfigGroup.1.NTPEnable: Enabled |
| 193 |
NTPConfigGroup.1.NTP1: 192.168.0.5 |
| 194 |
NTPConfigGroup.1.NTP2: 192.168.0.6 |
| 195 |
NTPConfigGroup.1.NTP3: 192.168.0.7 |
| 196 |
|
| 197 |
- name: Configure all attributes |
| 198 |
dellemc.openmanage.idrac_attributes: |
| 199 |
idrac_ip: "192.168.0.1" |
| 200 |
idrac_user: "user_name" |
| 201 |
idrac_password: "user_password" |
| 202 |
ca_path: "/path/to/ca_cert.pem" |
| 203 |
idrac_attributes: |
| 204 |
SNMP.1.AgentCommunity: test |
| 205 |
SNMP.1.AgentEnable: Enabled |
| 206 |
SNMP.1.DiscoveryPort: 161 |
| 207 |
system_attributes: |
| 208 |
ServerOS.1.HostName: demohostname |
| 209 |
lifecycle_controller_attributes: |
| 210 |
LCAttributes.1.AutoUpdate: Disabled |
| 211 |
|
| 212 |
- name: Enable idrac basic syslog |
| 213 |
dellemc.openmanage.idrac_attributes: |
| 214 |
idrac_ip: "192.168.0.1" |
| 215 |
idrac_user: "user_name" |
| 216 |
idrac_password: "user_password" |
| 217 |
ca_path: "/path/to/ca_cert.pem" |
| 218 |
idrac_attributes: |
| 219 |
SysLog.1.SysLogEnable: Enabled |
| 220 |
SysLog.1.Server1: 192.168.0.2 |
| 221 |
SysLog.1.Server2: 192.168.0.3 |
| 222 |
SysLog.1.Server3: 192.168.0.4 |
| 223 |
SysLog.1.Port: 514 |
| 224 |
|
| 225 |
- name: Disable idrac basic syslog |
| 226 |
dellemc.openmanage.idrac_attributes: |
| 227 |
idrac_ip: "192.168.0.1" |
| 228 |
idrac_user: "user_name" |
| 229 |
idrac_password: "user_password" |
| 230 |
ca_path: "/path/to/ca_cert.pem" |
| 231 |
idrac_attributes: |
| 232 |
SysLog.1.SysLogEnable: Disabled |
| 233 |
|
| 234 |
- name: Enable idrac secure syslog |
| 235 |
dellemc.openmanage.idrac_attributes: |
| 236 |
idrac_ip: "192.168.0.1" |
| 237 |
idrac_user: "user_name" |
| 238 |
idrac_password: "user_password" |
| 239 |
ca_path: "/path/to/ca_cert.pem" |
| 240 |
idrac_attributes: |
| 241 |
SysLog.1.securesyslogenable: Enabled |
| 242 |
SysLog.1.secureserver1: 192.168.0.2 |
| 243 |
SysLog.1.secureport: 6511 |
| 244 |
SysLog.1.secureclientauth: Anonymous |
| 245 |
|
| 246 |
- name: Disable idrac secure syslog |
| 247 |
dellemc.openmanage.idrac_attributes: |
| 248 |
idrac_ip: "192.168.0.1" |
| 249 |
idrac_user: "user_name" |
| 250 |
idrac_password: "user_password" |
| 251 |
ca_path: "/path/to/ca_cert.pem" |
| 252 |
idrac_attributes: |
| 253 |
SysLog.1.securesyslogenable: Disabled |
| 254 |
""" |
| 255 |
|
| 256 |
RETURN = r''' |
| 257 |
--- |
| 258 |
msg: |
| 259 |
type: str |
| 260 |
description: Status of the attribute update operation. |
| 261 |
returned: always |
| 262 |
sample: "Successfully updated the attributes." |
| 263 |
invalid_attributes: |
| 264 |
type: dict |
| 265 |
description: Dict of invalid attributes provided. |
| 266 |
returned: on invalid attributes or values. |
| 267 |
sample: { |
| 268 |
"LCAttributes.1.AutoUpdate": "Invalid value for Enumeration.", |
| 269 |
"LCAttributes.1.StorageHealthRollupStatus": "Read only Attribute cannot be modified.", |
| 270 |
"SNMP.1.AlertPort": "Not a valid integer.", |
| 271 |
"SNMP.1.AlertPorty": "Attribute does not exist.", |
| 272 |
"SysLog.1.PowerLogInterval": "Integer out of valid range.", |
| 273 |
"ThermalSettings.1.AirExhaustTemp": "Invalid value for Enumeration." |
| 274 |
} |
| 275 |
error_info: |
| 276 |
description: Error information of the operation. |
| 277 |
returned: when attribute value is invalid. |
| 278 |
type: dict |
| 279 |
sample: { |
| 280 |
"error": { |
| 281 |
"@Message.ExtendedInfo": [ |
| 282 |
{ |
| 283 |
"Message": "The value 'false' for the property LCAttributes.1.BIOSRTDRequested is of a different type than the property can accept.", |
| 284 |
"MessageArgs": [ |
| 285 |
"false", |
| 286 |
"LCAttributes.1.BIOSRTDRequested" |
| 287 |
], |
| 288 |
"[email protected]": 2, |
| 289 |
"MessageId": "Base.1.12.PropertyValueTypeError", |
| 290 |
"RelatedProperties": [ |
| 291 |
"#/Attributes/LCAttributes.1.BIOSRTDRequested" |
| 292 |
], |
| 293 |
"[email protected]": 1, |
| 294 |
"Resolution": "Correct the value for the property in the request body and resubmit the request if the operation failed.", |
| 295 |
"Severity": "Warning" |
| 296 |
} |
| 297 |
], |
| 298 |
"code": "Base.1.12.GeneralError", |
| 299 |
"message": "A general error has occurred. See ExtendedInfo for more information" |
| 300 |
} |
| 301 |
} |
| 302 |
''' |
| 303 |
|
| 304 |
import json |
| 305 |
import re |
| 306 |
from ansible.module_utils.six.moves.urllib.error import URLError, HTTPError |
| 307 |
from ansible.module_utils.urls import ConnectionError |
| 308 |
from ansible_collections.dellemc.openmanage.plugins.module_utils.idrac_redfish import iDRACRedfishAPI, IdracAnsibleModule |
| 309 |
from ansible_collections.dellemc.openmanage.plugins.module_utils.utils import get_manager_res_id |
| 310 |
|
| 311 |
|
| 312 |
SUCCESS_MSG = "Successfully updated the attributes." |
| 313 |
NO_CHANGES_MSG = "No changes found to be applied." |
| 314 |
CHANGES_MSG = "Changes found to be applied." |
| 315 |
ATTR_FAIL_MSG = "Application of some of the attributes failed due to invalid value or enumeration." |
| 316 |
SYSTEM_ID = "System.Embedded.1" |
| 317 |
MANAGER_ID = "iDRAC.Embedded.1" |
| 318 |
LC_ID = "LifecycleController.Embedded.1" |
| 319 |
MANAGERS_URI = "/redfish/v1/Managers" |
| 320 |
ATTR = "Attributes" |
| 321 |
JOB_URI = "/redfish/v1/Managers/{manager_id}/Jobs/{job_id}" |
| 322 |
|
| 323 |
|
| 324 |
def xml_data_conversion(attrbite, fqdd=None): |
| 325 |
component = """<Component FQDD="{0}">{1}</Component>""" |
| 326 |
attr = "" |
| 327 |
json_data = {} |
| 328 |
for k, v in attrbite.items(): |
| 329 |
key = re.sub(r"\.(?!\d)", "#", k) |
| 330 |
attr += '<Attribute Name="{0}">{1}</Attribute>'.format(key, v) |
| 331 |
json_data[key] = str(v) |
| 332 |
root = component.format(fqdd, attr) |
| 333 |
return root, json_data |
| 334 |
|
| 335 |
|
| 336 |
def validate_attr_name(attribute, req_data): |
| 337 |
invalid_attr = {} |
| 338 |
data_dict = {attr["Name"]: attr["Value"] for attr in attribute if attr["Name"] in req_data.keys()} |
| 339 |
if not len(data_dict) == len(req_data): |
| 340 |
for key in req_data.keys(): |
| 341 |
if key not in data_dict: |
| 342 |
act_key = key.replace("#", ".") |
| 343 |
invalid_attr[act_key] = "Attribute does not exist." |
| 344 |
return data_dict, invalid_attr |
| 345 |
|
| 346 |
|
| 347 |
def get_check_mode(module, idrac, idrac_json, sys_json, lc_json): |
| 348 |
scp_response = idrac.export_scp(export_format="JSON", export_use="Default", |
| 349 |
target="iDRAC,System,LifecycleController", job_wait=True) |
| 350 |
comp = scp_response.json_data["SystemConfiguration"]["Components"] |
| 351 |
exist_idrac, exist_sys, exist_lc, invalid = {}, {}, {}, {} |
| 352 |
for cmp in comp: |
| 353 |
if idrac_json and cmp.get("FQDD") == MANAGER_ID: |
| 354 |
exist_idrac, invalid_attr = validate_attr_name(cmp["Attributes"], idrac_json) |
| 355 |
if invalid_attr: |
| 356 |
invalid.update(invalid_attr) |
| 357 |
if sys_json and cmp.get("FQDD") == SYSTEM_ID: |
| 358 |
exist_sys, invalid_attr = validate_attr_name(cmp["Attributes"], sys_json) |
| 359 |
if invalid_attr: |
| 360 |
invalid.update(invalid_attr) |
| 361 |
if lc_json and cmp.get("FQDD") == LC_ID: |
| 362 |
exist_lc, invalid_attr = validate_attr_name(cmp["Attributes"], lc_json) |
| 363 |
if invalid_attr: |
| 364 |
invalid.update(invalid_attr) |
| 365 |
if invalid: |
| 366 |
module.fail_json(msg="Attributes have invalid values.", invalid_attributes=invalid) |
| 367 |
diff_change = [bool(set(exist_idrac.items()) ^ set(idrac_json.items())) or |
| 368 |
bool(set(exist_sys.items()) ^ set(sys_json.items())) or |
| 369 |
bool(set(exist_lc.items()) ^ set(lc_json.items()))] |
| 370 |
if module.check_mode and any(diff_change) is True: |
| 371 |
module.exit_json(msg=CHANGES_MSG, changed=True) |
| 372 |
elif (module.check_mode and all(diff_change) is False) or \ |
| 373 |
(not module.check_mode and all(diff_change) is False): |
| 374 |
module.exit_json(msg=NO_CHANGES_MSG) |
| 375 |
|
| 376 |
|
| 377 |
def scp_idrac_attributes(module, idrac, res_id): |
| 378 |
job_wait = module.params.get("job_wait", True) |
| 379 |
idrac_attr = module.params.get("idrac_attributes") |
| 380 |
system_attr = module.params.get("system_attributes") |
| 381 |
lc_attr = module.params.get("lifecycle_controller_attributes") |
| 382 |
root = """<SystemConfiguration>{0}</SystemConfiguration>""" |
| 383 |
component = "" |
| 384 |
idrac_json_data, system_json_data, lc_json_data = {}, {}, {} |
| 385 |
if idrac_attr is not None: |
| 386 |
idrac_xml_payload, idrac_json_data = xml_data_conversion(idrac_attr, fqdd=MANAGER_ID) |
| 387 |
component += idrac_xml_payload |
| 388 |
if system_attr is not None: |
| 389 |
system_xml_payload, system_json_data = xml_data_conversion(system_attr, fqdd=SYSTEM_ID) |
| 390 |
component += system_xml_payload |
| 391 |
if lc_attr is not None: |
| 392 |
lc_xml_payload, lc_json_data = xml_data_conversion(lc_attr, fqdd=LC_ID) |
| 393 |
component += lc_xml_payload |
| 394 |
get_check_mode(module, idrac, idrac_json_data, system_json_data, lc_json_data,) |
| 395 |
payload = root.format(component) |
| 396 |
resp = idrac.import_scp(import_buffer=payload, target="ALL", job_wait=False) |
| 397 |
job_id = resp.headers["Location"].split("/")[-1] |
| 398 |
job_uri = JOB_URI.format(manager_id=res_id, job_id=job_id) |
| 399 |
job_resp = idrac.wait_for_job_completion(job_uri, job_wait=job_wait) |
| 400 |
return job_resp |
| 401 |
|
| 402 |
|
| 403 |
def get_response_attr(idrac, idrac_id, attr, uri_dict): |
| 404 |
response_attr = {} |
| 405 |
diff = 0 |
| 406 |
response = idrac.invoke_request(uri_dict.get(idrac_id), "GET") |
| 407 |
for k in attr.keys(): |
| 408 |
if response.json_data[ATTR].get(k) != attr.get(k): |
| 409 |
|
| 410 |
response_attr[k] = attr.get(k) |
| 411 |
diff += 1 |
| 412 |
return diff, response_attr |
| 413 |
|
| 414 |
|
| 415 |
def get_attributes_registry(idrac): |
| 416 |
reggy = {} |
| 417 |
try: |
| 418 |
resp = idrac.invoke_request("/redfish/v1/Registries/ManagerAttributeRegistry", "GET") |
| 419 |
loc_list = resp.json_data.get("Location", []) |
| 420 |
if loc_list: |
| 421 |
reg_json_uri = loc_list[-1].get("Uri") |
| 422 |
reg_resp = idrac.invoke_request(reg_json_uri, "GET") |
| 423 |
attr_list = reg_resp.json_data.get("RegistryEntries").get("Attributes") |
| 424 |
reggy = dict((x["AttributeName"], x) for x in attr_list) |
| 425 |
except Exception: |
| 426 |
reggy = {} |
| 427 |
return reggy |
| 428 |
|
| 429 |
|
| 430 |
def validate_vs_registry(registry, attr_dict): |
| 431 |
invalid = {} |
| 432 |
for k, v in attr_dict.items(): |
| 433 |
if k in registry: |
| 434 |
val_dict = registry.get(k) |
| 435 |
if val_dict.get("Readonly"): |
| 436 |
invalid[k] = "Read only Attribute cannot be modified." |
| 437 |
else: |
| 438 |
type = val_dict.get("Type") |
| 439 |
if type == "Enumeration": |
| 440 |
found = False |
| 441 |
for val in val_dict.get("Value", []): |
| 442 |
if v == val.get("ValueDisplayName"): |
| 443 |
found = True |
| 444 |
break |
| 445 |
if not found: |
| 446 |
invalid[k] = "Invalid value for Enumeration." |
| 447 |
if type == "Integer": |
| 448 |
try: |
| 449 |
i = int(v) |
| 450 |
except Exception: |
| 451 |
invalid[k] = "Not a valid integer." |
| 452 |
else: |
| 453 |
if not (val_dict.get("LowerBound") <= i <= val_dict.get("UpperBound")): |
| 454 |
invalid[k] = "Integer out of valid range." |
| 455 |
else: |
| 456 |
invalid[k] = "Attribute does not exist." |
| 457 |
return invalid |
| 458 |
|
| 459 |
|
| 460 |
def fetch_idrac_uri_attr(idrac, module, res_id): |
| 461 |
diff = 0 |
| 462 |
uri_dict = {} |
| 463 |
idrac_response_attr = {} |
| 464 |
system_response_attr = {} |
| 465 |
lc_response_attr = {} |
| 466 |
response = idrac.invoke_request("{0}/{1}".format(MANAGERS_URI, res_id), "GET") |
| 467 |
dell_attributes = response.json_data.get('Links', {}).get('Oem', {}).get('Dell', {}).get('DellAttributes') |
| 468 |
if dell_attributes: |
| 469 |
for item in dell_attributes: |
| 470 |
uri = item.get('@odata.id') |
| 471 |
attr_id = uri.split("/")[-1] |
| 472 |
uri_dict[attr_id] = uri |
| 473 |
idrac_attr = module.params.get("idrac_attributes") |
| 474 |
system_attr = module.params.get("system_attributes") |
| 475 |
lc_attr = module.params.get("lifecycle_controller_attributes") |
| 476 |
invalid = {} |
| 477 |
attr_registry = get_attributes_registry(idrac) |
| 478 |
if idrac_attr is not None: |
| 479 |
x, idrac_response_attr = get_response_attr(idrac, MANAGER_ID, idrac_attr, uri_dict) |
| 480 |
invalid.update(validate_vs_registry(attr_registry, idrac_response_attr)) |
| 481 |
diff += x |
| 482 |
if system_attr is not None: |
| 483 |
x, system_response_attr = get_response_attr(idrac, SYSTEM_ID, system_attr, uri_dict) |
| 484 |
invalid.update(validate_vs_registry(attr_registry, system_response_attr)) |
| 485 |
diff += x |
| 486 |
if lc_attr is not None: |
| 487 |
x, lc_response_attr = get_response_attr(idrac, LC_ID, lc_attr, uri_dict) |
| 488 |
invalid.update(validate_vs_registry(attr_registry, lc_response_attr)) |
| 489 |
diff += x |
| 490 |
if invalid: |
| 491 |
module.exit_json(failed=True, msg="Attributes have invalid values.", invalid_attributes=invalid) |
| 492 |
else: |
| 493 |
job_resp = scp_idrac_attributes(module, idrac, res_id) |
| 494 |
if job_resp.status_code == 200: |
| 495 |
error_msg = ["Unable to complete application of configuration profile values.", |
| 496 |
"Import of Server Configuration Profile operation completed with errors."] |
| 497 |
message = job_resp.json_data["Message"] |
| 498 |
message_id = job_resp.json_data["MessageId"] |
| 499 |
if message_id == "SYS069": |
| 500 |
module.exit_json(msg=NO_CHANGES_MSG) |
| 501 |
elif message_id == "SYS053": |
| 502 |
module.exit_json(msg=SUCCESS_MSG, changed=True) |
| 503 |
elif message in error_msg: |
| 504 |
module.fail_json(msg=ATTR_FAIL_MSG) |
| 505 |
else: |
| 506 |
module.fail_json(msg=message) |
| 507 |
return diff, uri_dict, idrac_response_attr, system_response_attr, lc_response_attr |
| 508 |
|
| 509 |
|
| 510 |
def process_check_mode(module, diff): |
| 511 |
if not diff: |
| 512 |
module.exit_json(msg=NO_CHANGES_MSG) |
| 513 |
elif diff and module.check_mode: |
| 514 |
module.exit_json(msg=CHANGES_MSG, changed=True) |
| 515 |
|
| 516 |
|
| 517 |
def update_idrac_attributes(idrac, module, uri_dict, idrac_response_attr, system_response_attr, lc_response_attr): |
| 518 |
resp = {} |
| 519 |
idrac_payload = module.params.get("idrac_attributes") |
| 520 |
system_payload = module.params.get("system_attributes") |
| 521 |
lc_payload = module.params.get("lifecycle_controller_attributes") |
| 522 |
if idrac_payload is not None and idrac_response_attr is not None: |
| 523 |
idrac_response = idrac.invoke_request(uri_dict.get(MANAGER_ID), "PATCH", data={ATTR: idrac_payload}) |
| 524 |
resp["iDRAC"] = idrac_response.json_data |
| 525 |
if system_payload is not None and system_response_attr is not None: |
| 526 |
system_response = idrac.invoke_request(uri_dict.get(SYSTEM_ID), "PATCH", data={ATTR: system_payload}) |
| 527 |
resp["System"] = system_response.json_data |
| 528 |
if lc_payload is not None and lc_response_attr is not None: |
| 529 |
lc_response = idrac.invoke_request(uri_dict.get(LC_ID), "PATCH", data={ATTR: lc_payload}) |
| 530 |
resp["Lifecycle Controller"] = lc_response.json_data |
| 531 |
return resp |
| 532 |
|
| 533 |
|
| 534 |
def main(): |
| 535 |
specs = { |
| 536 |
"idrac_attributes": {"required": False, "type": 'dict'}, |
| 537 |
"system_attributes": {"required": False, "type": 'dict'}, |
| 538 |
"lifecycle_controller_attributes": {"required": False, "type": 'dict'}, |
| 539 |
"resource_id": {"required": False, "type": 'str'} |
| 540 |
} |
| 541 |
|
| 542 |
module = IdracAnsibleModule( |
| 543 |
argument_spec=specs, |
| 544 |
required_one_of=[('idrac_attributes', 'system_attributes', 'lifecycle_controller_attributes')], |
| 545 |
supports_check_mode=True |
| 546 |
) |
| 547 |
try: |
| 548 |
with iDRACRedfishAPI(module.params, req_session=True) as idrac: |
| 549 |
res_id = module.params.get('resource_id') |
| 550 |
if not res_id: |
| 551 |
res_id = get_manager_res_id(idrac) |
| 552 |
diff, uri_dict, idrac_response_attr, system_response_attr, lc_response_attr = fetch_idrac_uri_attr(idrac, module, res_id) |
| 553 |
process_check_mode(module, diff) |
| 554 |
update_idrac_attributes(idrac, module, uri_dict, idrac_response_attr, system_response_attr, lc_response_attr) |
| 555 |
module.exit_json(msg=SUCCESS_MSG, changed=True) |
| 556 |
except HTTPError as err: |
| 557 |
module.exit_json(msg=str(err), error_info=json.load(err), failed=True) |
| 558 |
except URLError as err: |
| 559 |
module.exit_json(msg=str(err), unreachable=True) |
| 560 |
except (IOError, ValueError, TypeError, ConnectionError, AttributeError, IndexError, KeyError) as err: |
| 561 |
module.exit_json(msg=str(err), error_info=json.load(err), failed=True) |
| 562 |
|
| 563 |
|
| 564 |
if __name__ == '__main__': |
| 565 |
main() |