| 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 = r''' |
| 16 |
--- |
| 17 |
module: idrac_reset |
| 18 |
short_description: Factory reset the iDRACs |
| 19 |
version_added: "2.1.0" |
| 20 |
description: |
| 21 |
- This module resets the iDRAC to factory default settings. |
| 22 |
extends_documentation_fragment: |
| 23 |
- dellemc.openmanage.idrac_x_auth_options |
| 24 |
options: |
| 25 |
reset_to_default: |
| 26 |
type: str |
| 27 |
description: |
| 28 |
- If this value is not set the default behaviour is to restart the iDRAC. |
| 29 |
- C(All) Discards all settings and reset to default credentials. |
| 30 |
- C(ResetAllWithRootDefaults) Discards all settings and reset the default username to root and password to the shipping value. |
| 31 |
- C(Default) Discards all settings, but preserves user and network settings. |
| 32 |
- C(CustomDefaults) All configuration is set to custom defaults.This option is supported on firmware version 7.00.00.00 and newer versions. |
| 33 |
choices: ['Default', 'All', 'ResetAllWithRootDefaults', 'CustomDefaults'] |
| 34 |
version_added: 9.2.0 |
| 35 |
custom_defaults_file: |
| 36 |
description: |
| 37 |
- Name of the custom default configuration file in the XML format. |
| 38 |
- This option is applicable when I(reset_to_default) is C(CustomDefaults). |
| 39 |
- I(custom_defaults_file) is mutually exclusive with I(custom_defaults_buffer). |
| 40 |
type: str |
| 41 |
version_added: 9.2.0 |
| 42 |
custom_defaults_buffer: |
| 43 |
description: |
| 44 |
- This parameter provides the option to import the buffer input in XML format as a custom default configuration. |
| 45 |
- This option is applicable when I(reset_to_default) is C(CustomDefaults). |
| 46 |
- I(custom_defaults_buffer) is mutually exclusive with I(custom_defaults_file). |
| 47 |
type: str |
| 48 |
version_added: 9.2.0 |
| 49 |
wait_for_idrac: |
| 50 |
description: |
| 51 |
- This parameter provides the option to wait for the iDRAC to reset and lifecycle controller status to be ready. |
| 52 |
type: bool |
| 53 |
default: true |
| 54 |
version_added: 9.2.0 |
| 55 |
job_wait_timeout: |
| 56 |
description: |
| 57 |
- Time in seconds to wait for job completion. |
| 58 |
- This is applicable when I(wait_for_idrac) is C(true). |
| 59 |
type: int |
| 60 |
default: 600 |
| 61 |
version_added: 9.2.0 |
| 62 |
force_reset: |
| 63 |
description: |
| 64 |
- This parameter provides the option to force reset the iDRAC without checking the iDRAC lifecycle controller status. |
| 65 |
- This option is applicable only for iDRAC9. |
| 66 |
type: bool |
| 67 |
default: false |
| 68 |
version_added: 9.2.0 |
| 69 |
default_username: |
| 70 |
description: |
| 71 |
- This parameter is only applied when I(reset_to_default) is C(All) or C(ResetAllWithRootDefaults). |
| 72 |
- This parameter is required to track LifeCycle status of the server after the reset operation is |
| 73 |
performed. If this parameter is not provided, then the LifeCycle status is not tracked after the |
| 74 |
reset operation. |
| 75 |
type: str |
| 76 |
version_added: 9.4.0 |
| 77 |
default_password: |
| 78 |
description: |
| 79 |
- This parameter is only applied when I(reset_to_default) is C(All) or C(ResetAllWithRootDefaults). |
| 80 |
- This parameter is required to track LifeCycle status of the server after the reset operation is |
| 81 |
performed. If this parameter is not provided, then the LifeCycle status is not tracked after the |
| 82 |
reset operation. |
| 83 |
type: str |
| 84 |
version_added: 9.4.0 |
| 85 |
|
| 86 |
|
| 87 |
requirements: |
| 88 |
- "python >= 3.9.6" |
| 89 |
author: |
| 90 |
- "Felix Stephen (@felixs88)" |
| 91 |
- "Anooja Vardhineni (@anooja-vardhineni)" |
| 92 |
- "Lovepreet Singh (@singh-lovepreet1)" |
| 93 |
- "Abhishek Sinha (@ABHISHEK-SINHA10)" |
| 94 |
- "Rounak Adhikary (@rounak-adhikary)" |
| 95 |
notes: |
| 96 |
- Run this module from a system that has direct access to Dell iDRAC. |
| 97 |
- This module supports both IPv4 and IPv6 address for I(idrac_ip). |
| 98 |
- This module supports C(check_mode). |
| 99 |
- If reset_to_default option is not specified, then this module triggers a graceful restart. |
| 100 |
- This module skips the execution if reset options are not supported by the iDRAC. |
| 101 |
''' |
| 102 |
|
| 103 |
EXAMPLES = r''' |
| 104 |
--- |
| 105 |
- name: Reset the iDRAC to default and do not wait till the iDRAC is accessible. |
| 106 |
dellemc.openmanage.idrac_reset: |
| 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 |
reset_to_default: "Default" |
| 112 |
wait_for_idrac: false |
| 113 |
|
| 114 |
- name: Reset the iDRAC to All and wait for lifecycle controller status to be ready. |
| 115 |
dellemc.openmanage.idrac_reset: |
| 116 |
idrac_ip: "192.168.0.1" |
| 117 |
idrac_user: "user_name" |
| 118 |
idrac_password: "user_password" |
| 119 |
ca_path: "/path/to/ca_cert.pem" |
| 120 |
reset_to_default: "All" |
| 121 |
wait_for_idrac: true |
| 122 |
default_username: "user_name" |
| 123 |
default_password: "user_password" |
| 124 |
|
| 125 |
- name: Force reset the iDRAC to default. |
| 126 |
dellemc.openmanage.idrac_reset: |
| 127 |
idrac_ip: "192.168.0.1" |
| 128 |
idrac_user: "user_name" |
| 129 |
idrac_password: "user_password" |
| 130 |
ca_path: "/path/to/ca_cert.pem" |
| 131 |
reset_to_default: "Default" |
| 132 |
force_reset: true |
| 133 |
|
| 134 |
- name: Gracefully restart the iDRAC. |
| 135 |
dellemc.openmanage.idrac_reset: |
| 136 |
idrac_ip: "192.168.0.1" |
| 137 |
idrac_user: "user_name" |
| 138 |
idrac_password: "user_password" |
| 139 |
ca_path: "/path/to/ca_cert.pem" |
| 140 |
|
| 141 |
- name: Reset the iDRAC to custom defaults XML and do not wait till the iDRAC is accessible. |
| 142 |
dellemc.openmanage.idrac_reset: |
| 143 |
idrac_ip: "192.168.0.1" |
| 144 |
idrac_user: "user_name" |
| 145 |
idrac_password: "user_password" |
| 146 |
ca_path: "/path/to/ca_cert.pem" |
| 147 |
reset_to_default: "CustomDefaults" |
| 148 |
custom_defaults_file: "/path/to/custom_defaults.xml" |
| 149 |
|
| 150 |
- name: Reset the iDRAC to custom defaults buffer input and do not wait till the iDRAC is accessible. |
| 151 |
dellemc.openmanage.idrac_reset: |
| 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 |
reset_to_default: "CustomDefaults" |
| 157 |
custom_defaults_buffer: "<SystemConfiguration Model=\"PowerEdge R7525\" ServiceTag=\"ABCD123\">\n<Component FQDD=\"iDRAC.Embedded.1\">\n |
| 158 |
<Attribute Name=\"IPMILan.1#Enable\">Disabled</Attribute>\n </Component>\n\n</SystemConfiguration>" |
| 159 |
''' |
| 160 |
|
| 161 |
RETURN = r''' |
| 162 |
--- |
| 163 |
msg: |
| 164 |
type: str |
| 165 |
description: Status of the iDRAC reset operation. |
| 166 |
returned: always |
| 167 |
sample: "Successfully performed iDRAC reset." |
| 168 |
reset_status: |
| 169 |
type: dict |
| 170 |
description: Details of iDRAC reset operation. |
| 171 |
returned: reset operation is triggered. |
| 172 |
sample: { |
| 173 |
"idracreset": { |
| 174 |
"Data": { |
| 175 |
"StatusCode": 204 |
| 176 |
}, |
| 177 |
"Message": "none", |
| 178 |
"Status": "Success", |
| 179 |
"StatusCode": 204, |
| 180 |
"retval": true |
| 181 |
} |
| 182 |
} |
| 183 |
error_info: |
| 184 |
description: Details of the HTTP Error. |
| 185 |
returned: on HTTP error |
| 186 |
type: dict |
| 187 |
sample: { |
| 188 |
"error": { |
| 189 |
"code": "Base.1.0.GeneralError", |
| 190 |
"message": "A general error has occurred. See ExtendedInfo for more information.", |
| 191 |
"@Message.ExtendedInfo": [ |
| 192 |
{ |
| 193 |
"MessageId": "GEN1234", |
| 194 |
"RelatedProperties": [], |
| 195 |
"Message": "Unable to process the request because an error occurred.", |
| 196 |
"MessageArgs": [], |
| 197 |
"Severity": "Critical", |
| 198 |
"Resolution": "Retry the operation. If the issue persists, contact your system administrator." |
| 199 |
} |
| 200 |
] |
| 201 |
} |
| 202 |
} |
| 203 |
''' |
| 204 |
|
| 205 |
import os |
| 206 |
import json |
| 207 |
import time |
| 208 |
from typing import List, Tuple |
| 209 |
from urllib.error import HTTPError, URLError |
| 210 |
from ansible_collections.dellemc.openmanage.plugins.module_utils.idrac_redfish import iDRACRedfishAPI, IdracAnsibleModule |
| 211 |
from ansible.module_utils.compat.version import LooseVersion |
| 212 |
from ansible.module_utils.urls import ConnectionError, SSLValidationError |
| 213 |
from ansible_collections.dellemc.openmanage.plugins.module_utils.utils import ( |
| 214 |
remove_key, get_dynamic_uri, validate_and_get_first_resource_id_uri, idrac_redfish_job_tracking, wait_after_idrac_reset) |
| 215 |
|
| 216 |
|
| 217 |
MANAGERS_URI = "/redfish/v1/Managers" |
| 218 |
OEM = "Oem" |
| 219 |
MANUFACTURER = "Dell" |
| 220 |
ACTIONS = "Actions" |
| 221 |
IDRAC_RESET_RETRIES = 50 |
| 222 |
LC_STATUS_CHECK_SLEEP = 30 |
| 223 |
IDRAC_JOB_URI = "/redfish/v1/Managers/iDRAC.Embedded.1/Oem/Dell/Jobs/{job_id}" |
| 224 |
RESET_TO_DEFAULT_ERROR = "{reset_to_default} is not supported. The supported values are {supported_values}. Enter the valid values and retry the operation." |
| 225 |
RESET_TO_DEFAULT_ERROR_MSG = "{reset_to_default} is not supported." |
| 226 |
CUSTOM_ERROR = "{reset_to_default} is not supported on this firmware version of iDRAC. The supported values are {supported_values}. \ |
| 227 |
Enter the valid values and retry the operation." |
| 228 |
IDRAC_RESET_RESTART_SUCCESS_MSG = "iDRAC restart operation completed successfully." |
| 229 |
IDRAC_RESET_SUCCESS_MSG = "Successfully performed iDRAC reset." |
| 230 |
IDRAC_RESET_RESET_TRIGGER_MSG = "iDRAC reset operation triggered successfully." |
| 231 |
IDRAC_RESET_RESTART_TRIGGER_MSG = "iDRAC restart operation triggered successfully." |
| 232 |
INVALID_DIRECTORY_MSG = "Provided directory path '{path}' is invalid." |
| 233 |
FAILED_RESET_MSG = "Failed to perform the reset operation." |
| 234 |
FAILED_TO_LOGIN_POST_RESET_MSG = "Login failed after the iDRAC reset. \ |
| 235 |
Either the iDRAC is taking longer than expected time to come online \ |
| 236 |
or the iDRAC credentials are invalid." |
| 237 |
RESET_UNTRACK = "iDRAC reset is in progress. Changes will apply once the iDRAC reset operation is successfully completed." |
| 238 |
TIMEOUT_NEGATIVE_OR_ZERO_MSG = "The value of `job_wait_timeout` parameter cannot be negative or zero. Enter the valid value and retry the operation." |
| 239 |
INVALID_FILE_MSG = "File extension is invalid. Supported extension for 'custom_default_file' is: .xml." |
| 240 |
LC_STATUS_MSG = "Lifecycle controller status check is {lc_status} after {retries} number of retries, Exiting.." |
| 241 |
INSUFFICIENT_DIRECTORY_PERMISSION_MSG = "Provided directory path '{path}' is not writable. Please check if the directory has appropriate permissions." |
| 242 |
UNSUPPORTED_LC_STATUS_MSG = "Lifecycle controller status check is not supported." |
| 243 |
MINIMUM_SUPPORTED_FIRMWARE_VERSION = "7.00.00" |
| 244 |
CHANGES_NOT_FOUND = "No changes found to commit!" |
| 245 |
CHANGES_FOUND = "Changes found to commit!" |
| 246 |
ODATA_ID = "@odata.id" |
| 247 |
ODATA_REGEX = "(.*?)@odata" |
| 248 |
ATTRIBUTE = "</Attribute>" |
| 249 |
SUCCESS_STATUS = "Success" |
| 250 |
FAILED_STATUS = "Failed" |
| 251 |
STATUS_SUCCESS = [200, 202, 204] |
| 252 |
ERR_STATUS_CODE = [400, 404] |
| 253 |
PASSWORD_CHANGE_OPTIONS = ['All', 'ResetAllWithRootDefaults'] |
| 254 |
RESET_KEY = "Oem.#DellManager.ResetToDefaults" |
| 255 |
GRACEFUL_RESTART_KEY = "#Manager.Reset" |
| 256 |
|
| 257 |
|
| 258 |
class Validation(): |
| 259 |
def __init__(self, idrac, module, generation: int): |
| 260 |
self.idrac = idrac |
| 261 |
self.module = module |
| 262 |
self.base_uri = self.get_base_uri() |
| 263 |
self.idrac_model_version = generation |
| 264 |
|
| 265 |
def get_base_uri(self): |
| 266 |
uri, error_msg = validate_and_get_first_resource_id_uri( |
| 267 |
self.module, self.idrac, MANAGERS_URI) |
| 268 |
if error_msg: |
| 269 |
self.module.exit_json(msg=error_msg, failed=True) |
| 270 |
return uri |
| 271 |
|
| 272 |
def validate_reset_options(self, api_key): |
| 273 |
res = self.idrac.invoke_request(self.base_uri, "GET") |
| 274 |
reset_to_default = self.module.params.get('reset_to_default') |
| 275 |
key_list = api_key.split(".", 1) |
| 276 |
is_valid = True |
| 277 |
allowed_values = None |
| 278 |
if key_list[0] in res.json_data["Actions"] and key_list[1] in res.json_data["Actions"][key_list[0]]: |
| 279 |
reset_to_defaults_val = res.json_data["Actions"][key_list[0]][key_list[1]] |
| 280 |
reset_type_values = reset_to_defaults_val["[email protected]"] |
| 281 |
allowed_values = reset_type_values |
| 282 |
if self.idrac_model_version >= 13 and "CustomDefaults" not in allowed_values: |
| 283 |
allowed_values.append("CustomDefaults") |
| 284 |
if reset_to_default not in reset_type_values: |
| 285 |
is_valid = False |
| 286 |
else: |
| 287 |
is_valid = False |
| 288 |
return allowed_values, is_valid |
| 289 |
|
| 290 |
def validate_graceful_restart_option(self, api_key): |
| 291 |
res = self.idrac.invoke_request(self.base_uri, "GET") |
| 292 |
is_valid = True |
| 293 |
if api_key in res.json_data["Actions"]: |
| 294 |
reset_to_defaults_val = res.json_data["Actions"][api_key] |
| 295 |
reset_type_values = reset_to_defaults_val["[email protected]"] |
| 296 |
if "GracefulRestart" not in reset_type_values: |
| 297 |
is_valid = False |
| 298 |
else: |
| 299 |
is_valid = False |
| 300 |
return is_valid |
| 301 |
|
| 302 |
def validate_job_timeout(self): |
| 303 |
if self.module.params.get("wait_for_idrac") and self.module.params.get("job_wait_timeout") <= 0: |
| 304 |
self.module.exit_json(msg=TIMEOUT_NEGATIVE_OR_ZERO_MSG, failed=True) |
| 305 |
|
| 306 |
def validate_path(self, file_path): |
| 307 |
if not (os.path.exists(file_path)): |
| 308 |
self.module.exit_json(msg=INVALID_DIRECTORY_MSG.format(path=file_path), failed=True) |
| 309 |
if not os.access(file_path, os.W_OK): |
| 310 |
self.module.exit_json(msg=INSUFFICIENT_DIRECTORY_PERMISSION_MSG.format(path=file_path), failed=True) |
| 311 |
|
| 312 |
def validate_file_format(self, file_name): |
| 313 |
if not (file_name.endswith(".xml")): |
| 314 |
self.module.exit_json(msg=INVALID_FILE_MSG, failed=True) |
| 315 |
|
| 316 |
def validate_custom_option(self, reset_to_default=None, allowed_choices=None): |
| 317 |
url = None |
| 318 |
resp = get_dynamic_uri(self.idrac, self.base_uri, OEM) |
| 319 |
if resp: |
| 320 |
url = resp.get(MANUFACTURER, {}).get('CustomDefaultsDownloadURI', {}) |
| 321 |
try: |
| 322 |
if url: |
| 323 |
self.idrac.invoke_request(url, "GET") |
| 324 |
return True |
| 325 |
return False |
| 326 |
except HTTPError as err: |
| 327 |
if err.code in ERR_STATUS_CODE: |
| 328 |
self.module.exit_json(msg=RESET_TO_DEFAULT_ERROR.format(reset_to_default=reset_to_default, supported_values=allowed_choices), skipped=True) |
| 329 |
|
| 330 |
|
| 331 |
def _get_server_version(idrac: iDRACRedfishAPI) -> Tuple[int, str]: |
| 332 |
""" |
| 333 |
Function wrapping idrac.get_server_generation. Helps with mocked testing and linting. |
| 334 |
|
| 335 |
Args: |
| 336 |
idrac (iDRACRedfishAPI): iDRACRedfishAPI object. |
| 337 |
|
| 338 |
Returns: |
| 339 |
Tuple[int, str]: A tuple containing the server generation and firmware version. |
| 340 |
""" |
| 341 |
t = idrac.get_server_generation |
| 342 |
return t[0], t[1] |
| 343 |
|
| 344 |
|
| 345 |
class FactoryReset(): |
| 346 |
def __init__(self, idrac: iDRACRedfishAPI, module: IdracAnsibleModule, allowed_choices: List[str]): |
| 347 |
self.idrac = idrac |
| 348 |
self.module = module |
| 349 |
self.allowed_choices = allowed_choices |
| 350 |
self.reset_to_default = self.module.params.get('reset_to_default') |
| 351 |
self.force_reset = self.module.params.get('force_reset') |
| 352 |
self.wait_for_idrac = self.module.params.get('wait_for_idrac') |
| 353 |
self.idrac_generation, self.idrac_firmware_version = \ |
| 354 |
_get_server_version(self.idrac) |
| 355 |
self.validate_obj = Validation(self.idrac, self.module, self.idrac_generation) |
| 356 |
self.uri = self.validate_obj.base_uri |
| 357 |
|
| 358 |
def execute(self): |
| 359 |
msg_res, job_res = None, None |
| 360 |
self.validate_obj.validate_job_timeout() |
| 361 |
if self.module.check_mode: |
| 362 |
self.check_mode_output() |
| 363 |
if self.reset_to_default and not self.force_reset: |
| 364 |
self.check_lcstatus(post_op=False) |
| 365 |
reset_status_mapping = {key: self.reset_to_default_mapped for key in ['Default', 'All', 'ResetAllWithRootDefaults']} |
| 366 |
reset_status_mapping.update({ |
| 367 |
'CustomDefaults': self.reset_custom_defaults, |
| 368 |
'None': self.graceful_restart |
| 369 |
}) |
| 370 |
msg_res, job_res = reset_status_mapping[str(self.reset_to_default)]() |
| 371 |
if self.wait_for_idrac and self.reset_to_default: |
| 372 |
self.check_lcstatus() |
| 373 |
return msg_res, job_res |
| 374 |
|
| 375 |
def check_mode_output(self): |
| 376 |
if not self.custom_defaults_supported() and self.reset_to_default == 'CustomDefaults': |
| 377 |
self.module.exit_json(msg=CHANGES_NOT_FOUND) |
| 378 |
if self.reset_to_default: |
| 379 |
allowed_values, is_valid_option = self.validate_obj.validate_reset_options(RESET_KEY) |
| 380 |
else: |
| 381 |
is_valid_option = self.validate_obj.validate_graceful_restart_option(GRACEFUL_RESTART_KEY) |
| 382 |
custom_default_file = self.module.params.get('custom_defaults_file') |
| 383 |
custom_default_buffer = self.module.params.get('custom_defaults_buffer') |
| 384 |
if is_valid_option: |
| 385 |
self.module.exit_json(msg=CHANGES_FOUND, changed=True) |
| 386 |
elif self.reset_to_default and self.reset_to_default == 'CustomDefaults' and (custom_default_file or custom_default_buffer): |
| 387 |
self.module.exit_json(msg=CHANGES_FOUND, changed=True) |
| 388 |
else: |
| 389 |
self.module.exit_json(msg=CHANGES_NOT_FOUND) |
| 390 |
|
| 391 |
def update_credentials_for_post_lc_statuc_check(self): |
| 392 |
if (default_username := self.module.params.get("default_username")) and ( |
| 393 |
default_password := self.module.params.get("default_password") |
| 394 |
): |
| 395 |
self.idrac.username = default_username |
| 396 |
self.idrac.password = default_password |
| 397 |
return True |
| 398 |
|
| 399 |
def check_lcstatus(self, post_op=True): |
| 400 |
if self.reset_to_default in PASSWORD_CHANGE_OPTIONS and post_op and \ |
| 401 |
not self.update_credentials_for_post_lc_statuc_check(): |
| 402 |
return |
| 403 |
|
| 404 |
if post_op: |
| 405 |
|
| 406 |
svc_down = wait_after_idrac_reset(self.idrac, 6 * 60, 60)[0] |
| 407 |
if svc_down: |
| 408 |
self.module.exit_json( |
| 409 |
msg=FAILED_TO_LOGIN_POST_RESET_MSG, |
| 410 |
failed=True, changed=True, |
| 411 |
) |
| 412 |
|
| 413 |
|
| 414 |
manager_links_resp = get_dynamic_uri(self.idrac, self.uri, "Links") |
| 415 |
|
| 416 |
url = manager_links_resp.get(OEM, {}).get(MANUFACTURER, {}).get('DellLCService', {}).get(ODATA_ID, {}) |
| 417 |
if not url: |
| 418 |
self.module.exit_json(msg=UNSUPPORTED_LC_STATUS_MSG, failed=True) |
| 419 |
|
| 420 |
action_resp = get_dynamic_uri(self.idrac, url) |
| 421 |
lc_url = action_resp.get(ACTIONS, {}).get('#DellLCService.GetRemoteServicesAPIStatus', {}).get('target', {}) |
| 422 |
|
| 423 |
|
| 424 |
self._check_lc_status_with_url(lc_url) |
| 425 |
|
| 426 |
def _check_lc_status_with_url(self, lc_url: str): |
| 427 |
""" |
| 428 |
This function checks the Lifecycle Controller status by sending a POST request to the given lc_url. |
| 429 |
The function will retry the request up to IDRAC_RESET_RETRIES times with a 10-second delay between retries. |
| 430 |
If the Lifecycle Controller is not ready after IDRAC_RESET_RETRIES attempts, the module will exit with an error message. |
| 431 |
The function takes one parameter: lc_url (str) - The URL of the Lifecycle Controller API. |
| 432 |
""" |
| 433 |
lc_status : str = "" |
| 434 |
retry_count = 1 |
| 435 |
while retry_count < IDRAC_RESET_RETRIES: |
| 436 |
try: |
| 437 |
lcstatus_resp = self.idrac.invoke_request(lc_url, "POST", data="{}", dump=False) |
| 438 |
lc_status = lcstatus_resp.json_data.get('LCStatus') |
| 439 |
if lc_status == 'Ready': |
| 440 |
break |
| 441 |
time.sleep(10) |
| 442 |
retry_count = retry_count + 1 |
| 443 |
except URLError: |
| 444 |
time.sleep(10) |
| 445 |
retry_count = retry_count + 1 |
| 446 |
if retry_count == IDRAC_RESET_RETRIES: |
| 447 |
self.module.exit_json(msg=LC_STATUS_MSG.format(lc_status='unreachable', retries=IDRAC_RESET_RETRIES), unreachable=True) |
| 448 |
|
| 449 |
if retry_count == IDRAC_RESET_RETRIES and lc_status != "Ready": |
| 450 |
self.module.exit_json(msg=LC_STATUS_MSG.format(lc_status=lc_status, retries=retry_count), failed=True) |
| 451 |
|
| 452 |
def create_output(self, status): |
| 453 |
result = {} |
| 454 |
tmp_res = {} |
| 455 |
result['idracreset'] = {} |
| 456 |
result['idracreset']['Data'] = {'StatusCode': status} |
| 457 |
result['idracreset']['StatusCode'] = status |
| 458 |
track_failed, wait_msg = None, None |
| 459 |
if status in STATUS_SUCCESS: |
| 460 |
if self.wait_for_idrac: |
| 461 |
track_failed, status_code, wait_msg = self.wait_for_port_open() |
| 462 |
if track_failed: |
| 463 |
self.module.exit_json(msg=wait_msg, changed=True) |
| 464 |
tmp_res['msg'] = IDRAC_RESET_SUCCESS_MSG if self.wait_for_idrac else IDRAC_RESET_RESET_TRIGGER_MSG |
| 465 |
tmp_res['changed'] = True |
| 466 |
result['idracreset']['Message'] = IDRAC_RESET_SUCCESS_MSG if self.wait_for_idrac else IDRAC_RESET_RESET_TRIGGER_MSG |
| 467 |
result['idracreset']['Status'] = 'Success' |
| 468 |
result['idracreset']['retVal'] = True |
| 469 |
else: |
| 470 |
tmp_res['msg'] = FAILED_RESET_MSG |
| 471 |
tmp_res['changed'] = False |
| 472 |
result['idracreset']['Message'] = FAILED_RESET_MSG |
| 473 |
result['idracreset']['Status'] = 'FAILED' |
| 474 |
result['idracreset']['retVal'] = False |
| 475 |
if self.reset_to_default: |
| 476 |
result = None |
| 477 |
return tmp_res, result |
| 478 |
|
| 479 |
def perform_operation(self, payload): |
| 480 |
tmp_res, res = None, None |
| 481 |
url = None |
| 482 |
resp = get_dynamic_uri(self.idrac, self.uri, ACTIONS) |
| 483 |
if resp: |
| 484 |
url = resp.get(OEM, {}).get('#DellManager.ResetToDefaults', {}).get('target', {}) |
| 485 |
run_reset_status = self.idrac.invoke_request(url, "POST", data=payload) |
| 486 |
status = run_reset_status.status_code |
| 487 |
tmp_res, res = self.create_output(status) |
| 488 |
return tmp_res, res |
| 489 |
|
| 490 |
def upload_cd_content(self, data): |
| 491 |
payload = {"CustomDefaults": data} |
| 492 |
job_wait_timeout = self.module.params.get('job_wait_timeout') |
| 493 |
url = None |
| 494 |
resp = get_dynamic_uri(self.idrac, self.uri, ACTIONS) |
| 495 |
if resp: |
| 496 |
url = resp.get(OEM, {}).get('#DellManager.SetCustomDefaults', {}).get('target', {}) |
| 497 |
job_resp = self.idrac.invoke_request(url, "POST", data=payload) |
| 498 |
if (job_tracking_uri := job_resp.headers.get("Location")): |
| 499 |
job_id = job_tracking_uri.split("/")[-1] |
| 500 |
job_uri = IDRAC_JOB_URI.format(job_id=job_id) |
| 501 |
job_failed, msg, job_dict, wait_time = idrac_redfish_job_tracking(self.idrac, job_uri, |
| 502 |
max_job_wait_sec=job_wait_timeout, |
| 503 |
sleep_interval_secs=1) |
| 504 |
job_dict = remove_key(job_dict, regex_pattern='(.*?)@odata') |
| 505 |
if job_failed: |
| 506 |
self.module.exit_json(msg=job_dict.get("Message"), job_status=job_dict, failed=True) |
| 507 |
|
| 508 |
def wait_for_port_open(self, interval=45): |
| 509 |
timeout_wait = self.module.params.get('job_wait_timeout') |
| 510 |
time.sleep(interval) |
| 511 |
msg = RESET_UNTRACK |
| 512 |
wait = timeout_wait |
| 513 |
track_failed = True |
| 514 |
status_code = 503 |
| 515 |
while int(wait) > 0: |
| 516 |
try: |
| 517 |
self.idrac.invoke_request(MANAGERS_URI, 'GET') |
| 518 |
time.sleep(interval) |
| 519 |
msg = IDRAC_RESET_SUCCESS_MSG |
| 520 |
track_failed = False |
| 521 |
status_code = 200 |
| 522 |
break |
| 523 |
except HTTPError as err: |
| 524 |
status_code = err.code |
| 525 |
if status_code == 401: |
| 526 |
time.sleep(interval // 2) |
| 527 |
msg = IDRAC_RESET_SUCCESS_MSG |
| 528 |
track_failed = False |
| 529 |
break |
| 530 |
except Exception: |
| 531 |
time.sleep(interval) |
| 532 |
wait = wait - interval |
| 533 |
return track_failed, status_code, msg |
| 534 |
|
| 535 |
def reset_to_default_mapped(self): |
| 536 |
payload = {"ResetType": self.reset_to_default} |
| 537 |
self.allowed_choices, is_valid_option = self.validate_obj.validate_reset_options(RESET_KEY) |
| 538 |
if not is_valid_option: |
| 539 |
self.module.exit_json(msg=RESET_TO_DEFAULT_ERROR.format(reset_to_default=self.reset_to_default, supported_values=self.allowed_choices), |
| 540 |
skipped=True) |
| 541 |
return self.perform_operation(payload) |
| 542 |
|
| 543 |
def get_xml_content(self, file_path): |
| 544 |
with open(file_path, 'r') as file: |
| 545 |
xml_content = file.read() |
| 546 |
return xml_content |
| 547 |
|
| 548 |
def reset_custom_defaults(self): |
| 549 |
self.allowed_choices, is_valid_option = self.validate_obj.validate_reset_options(RESET_KEY) |
| 550 |
if not self.custom_defaults_supported(): |
| 551 |
self.module.exit_json(msg=CUSTOM_ERROR.format(reset_to_default=self.reset_to_default, |
| 552 |
supported_values=self.allowed_choices), skipped=True) |
| 553 |
custom_default_file = self.module.params.get('custom_defaults_file') |
| 554 |
custom_default_buffer = self.module.params.get('custom_defaults_buffer') |
| 555 |
upload_perfom = False |
| 556 |
default_data = None |
| 557 |
if custom_default_file: |
| 558 |
self.validate_obj.validate_path(custom_default_file) |
| 559 |
self.validate_obj.validate_file_format(custom_default_file) |
| 560 |
upload_perfom = True |
| 561 |
default_data = self.get_xml_content(custom_default_file) |
| 562 |
elif custom_default_buffer: |
| 563 |
upload_perfom = True |
| 564 |
default_data = custom_default_buffer |
| 565 |
if upload_perfom: |
| 566 |
self.upload_cd_content(default_data) |
| 567 |
self.validate_obj.validate_custom_option(self.reset_to_default, self.allowed_choices) |
| 568 |
return self.reset_to_default_mapped() |
| 569 |
|
| 570 |
def custom_defaults_supported(self) -> bool: |
| 571 |
|
| 572 |
|
| 573 |
return self.idrac_generation > 16 or ( |
| 574 |
self.idrac_generation == 16 and |
| 575 |
LooseVersion(self.idrac_firmware_version) >= MINIMUM_SUPPORTED_FIRMWARE_VERSION |
| 576 |
) |
| 577 |
|
| 578 |
def graceful_restart(self): |
| 579 |
url = None |
| 580 |
resp = get_dynamic_uri(self.idrac, self.uri, ACTIONS) |
| 581 |
if resp: |
| 582 |
url = resp.get('#Manager.Reset', {}).get('target', {}) |
| 583 |
payload = {"ResetType": "GracefulRestart"} |
| 584 |
run_reset_status = self.idrac.invoke_request(url, "POST", data=payload) |
| 585 |
status = run_reset_status.status_code |
| 586 |
tmp_res, resp = self.create_output(status) |
| 587 |
if status in STATUS_SUCCESS: |
| 588 |
tmp_res['msg'] = IDRAC_RESET_SUCCESS_MSG |
| 589 |
resp['idracreset']['Message'] = IDRAC_RESET_RESTART_SUCCESS_MSG if self.wait_for_idrac else IDRAC_RESET_RESTART_TRIGGER_MSG |
| 590 |
return tmp_res, resp |
| 591 |
|
| 592 |
|
| 593 |
def main(): |
| 594 |
specs = { |
| 595 |
"reset_to_default": {"choices": ['All', 'ResetAllWithRootDefaults', 'Default', 'CustomDefaults']}, |
| 596 |
"custom_defaults_file": {"type": "str"}, |
| 597 |
"custom_defaults_buffer": {"type": "str"}, |
| 598 |
"wait_for_idrac": {"type": "bool", "default": True}, |
| 599 |
"job_wait_timeout": {"type": 'int', "default": 600}, |
| 600 |
"force_reset": {"type": "bool", "default": False}, |
| 601 |
"default_username": {"type": "str"}, |
| 602 |
"default_password": {"type": "str", "no_log": True} |
| 603 |
} |
| 604 |
|
| 605 |
module = IdracAnsibleModule( |
| 606 |
argument_spec=specs, |
| 607 |
mutually_exclusive=[("custom_defaults_file", "custom_defaults_buffer")], |
| 608 |
required_together=[('default_username', 'default_password')], |
| 609 |
supports_check_mode=True) |
| 610 |
try: |
| 611 |
with iDRACRedfishAPI(module.params) as idrac: |
| 612 |
allowed_choices = specs['reset_to_default']['choices'] |
| 613 |
reset_obj = FactoryReset(idrac, module, allowed_choices) |
| 614 |
message_resp, output = reset_obj.execute() |
| 615 |
if output: |
| 616 |
if not message_resp.get('changed'): |
| 617 |
module.exit_json(msg=message_resp.get('msg'), reset_status=output, failed=True) |
| 618 |
module.exit_json(msg=message_resp.get('msg'), reset_status=output, changed=True) |
| 619 |
else: |
| 620 |
if not message_resp.get('changed'): |
| 621 |
module.exit_json(msg=message_resp.get('msg'), failed=True) |
| 622 |
module.exit_json(msg=message_resp.get('msg'), changed=True) |
| 623 |
except HTTPError as err: |
| 624 |
module.exit_json(msg=str(err), error_info=json.load(err), failed=True) |
| 625 |
except URLError as err: |
| 626 |
module.exit_json(msg=str(err), unreachable=True) |
| 627 |
except (RuntimeError, SSLValidationError, ConnectionError, TypeError, KeyError, ValueError, OSError) as err: |
| 628 |
module.exit_json(msg=str(err), failed=True) |
| 629 |
|
| 630 |
|
| 631 |
if __name__ == '__main__': |
| 632 |
main() |