| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright (c) 2018 Dell EMC Inc. |
| 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: idrac_redfish_command |
| 11 |
short_description: Manages Out-Of-Band controllers using iDRAC OEM Redfish APIs |
| 12 |
description: |
| 13 |
- Builds Redfish URIs locally and sends them to remote OOB controllers to perform an action. |
| 14 |
- For use with Dell iDRAC operations that require Redfish OEM extensions. |
| 15 |
extends_documentation_fragment: |
| 16 |
- community.general._attributes |
| 17 |
- community.general._redfish |
| 18 |
attributes: |
| 19 |
check_mode: |
| 20 |
support: none |
| 21 |
diff_mode: |
| 22 |
support: none |
| 23 |
options: |
| 24 |
category: |
| 25 |
required: true |
| 26 |
description: |
| 27 |
- Category to execute on iDRAC. |
| 28 |
type: str |
| 29 |
command: |
| 30 |
required: true |
| 31 |
description: |
| 32 |
- List of commands to execute on iDRAC. |
| 33 |
type: list |
| 34 |
elements: str |
| 35 |
baseuri: |
| 36 |
required: true |
| 37 |
description: |
| 38 |
- Base URI of iDRAC. |
| 39 |
type: str |
| 40 |
username: |
| 41 |
description: |
| 42 |
- Username for authenticating to iDRAC. |
| 43 |
type: str |
| 44 |
password: |
| 45 |
description: |
| 46 |
- Password for authenticating to iDRAC. |
| 47 |
type: str |
| 48 |
auth_token: |
| 49 |
description: |
| 50 |
- Security token for authenticating to iDRAC. |
| 51 |
type: str |
| 52 |
version_added: 2.3.0 |
| 53 |
timeout: |
| 54 |
description: |
| 55 |
- Timeout in seconds for HTTP requests to iDRAC. |
| 56 |
default: 10 |
| 57 |
type: int |
| 58 |
resource_id: |
| 59 |
description: |
| 60 |
- ID of the System, Manager or Chassis to modify. |
| 61 |
type: str |
| 62 |
version_added: '0.2.0' |
| 63 |
validate_certs: |
| 64 |
version_added: 10.6.0 |
| 65 |
ca_path: |
| 66 |
version_added: 10.6.0 |
| 67 |
ciphers: |
| 68 |
version_added: 10.6.0 |
| 69 |
|
| 70 |
author: "Jose Delarosa (@jose-delarosa)" |
| 71 |
""" |
| 72 |
|
| 73 |
EXAMPLES = r""" |
| 74 |
- name: Create BIOS configuration job (schedule BIOS setting update) |
| 75 |
community.general.idrac_redfish_command: |
| 76 |
category: Systems |
| 77 |
command: CreateBiosConfigJob |
| 78 |
resource_id: System.Embedded.1 |
| 79 |
baseuri: "{{ baseuri }}" |
| 80 |
username: "{{ username }}" |
| 81 |
password: "{{ password }}" |
| 82 |
""" |
| 83 |
|
| 84 |
RETURN = r""" |
| 85 |
msg: |
| 86 |
description: Message with action result or error description. |
| 87 |
returned: always |
| 88 |
type: str |
| 89 |
sample: "Action was successful" |
| 90 |
return_values: |
| 91 |
description: Dictionary containing command-specific response data from the action. |
| 92 |
returned: on success |
| 93 |
type: dict |
| 94 |
version_added: 6.6.0 |
| 95 |
sample: {"job_id": "/redfish/v1/Managers/iDRAC.Embedded.1/Jobs/JID_471269252011"} |
| 96 |
""" |
| 97 |
|
| 98 |
import re |
| 99 |
|
| 100 |
from ansible.module_utils.basic import AnsibleModule |
| 101 |
|
| 102 |
from ansible_collections.community.general.plugins.module_utils._redfish_utils import ( |
| 103 |
REDFISH_COMMON_ARGUMENT_SPEC, |
| 104 |
RedfishUtils, |
| 105 |
) |
| 106 |
|
| 107 |
|
| 108 |
class IdracRedfishUtils(RedfishUtils): |
| 109 |
def create_bios_config_job(self): |
| 110 |
result = {} |
| 111 |
key = "Bios" |
| 112 |
jobs = "Jobs" |
| 113 |
|
| 114 |
# Search for 'key' entry and extract URI from it |
| 115 |
response = self.get_request(self.root_uri + self.systems_uris[0]) |
| 116 |
if response["ret"] is False: |
| 117 |
return response |
| 118 |
result["ret"] = True |
| 119 |
data = response["data"] |
| 120 |
|
| 121 |
if key not in data: |
| 122 |
return {"ret": False, "msg": f"Key {key} not found"} |
| 123 |
|
| 124 |
bios_uri = data[key]["@odata.id"] |
| 125 |
|
| 126 |
# Extract proper URI |
| 127 |
response = self.get_request(self.root_uri + bios_uri) |
| 128 |
if response["ret"] is False: |
| 129 |
return response |
| 130 |
result["ret"] = True |
| 131 |
data = response["data"] |
| 132 |
set_bios_attr_uri = data["@Redfish.Settings"]["SettingsObject"]["@odata.id"] |
| 133 |
|
| 134 |
payload = {"TargetSettingsURI": set_bios_attr_uri} |
| 135 |
response = self.post_request(f"{self.root_uri}{self.manager_uri}/{jobs}", payload) |
| 136 |
if response["ret"] is False: |
| 137 |
return response |
| 138 |
|
| 139 |
response_output = response["resp"].__dict__ |
| 140 |
job_id_full = response_output["headers"]["Location"] |
| 141 |
job_id = re.search("JID_.+", job_id_full).group() |
| 142 |
return {"ret": True, "msg": f"Config job {job_id} created", "job_id": job_id_full} |
| 143 |
|
| 144 |
|
| 145 |
CATEGORY_COMMANDS_ALL = {"Systems": ["CreateBiosConfigJob"], "Accounts": [], "Manager": []} |
| 146 |
|
| 147 |
|
| 148 |
def main(): |
| 149 |
result = {} |
| 150 |
return_values = {} |
| 151 |
argument_spec = dict( |
| 152 |
category=dict(required=True), |
| 153 |
command=dict(required=True, type="list", elements="str"), |
| 154 |
baseuri=dict(required=True), |
| 155 |
username=dict(), |
| 156 |
password=dict(no_log=True), |
| 157 |
auth_token=dict(no_log=True), |
| 158 |
timeout=dict(type="int", default=10), |
| 159 |
resource_id=dict(), |
| 160 |
) |
| 161 |
argument_spec.update(REDFISH_COMMON_ARGUMENT_SPEC) |
| 162 |
module = AnsibleModule( |
| 163 |
argument_spec, |
| 164 |
required_together=[ |
| 165 |
("username", "password"), |
| 166 |
], |
| 167 |
required_one_of=[ |
| 168 |
("username", "auth_token"), |
| 169 |
], |
| 170 |
mutually_exclusive=[ |
| 171 |
("username", "auth_token"), |
| 172 |
], |
| 173 |
supports_check_mode=False, |
| 174 |
) |
| 175 |
|
| 176 |
category = module.params["category"] |
| 177 |
command_list = module.params["command"] |
| 178 |
|
| 179 |
# admin credentials used for authentication |
| 180 |
creds = {"user": module.params["username"], "pswd": module.params["password"], "token": module.params["auth_token"]} |
| 181 |
|
| 182 |
# timeout |
| 183 |
timeout = module.params["timeout"] |
| 184 |
|
| 185 |
# System, Manager or Chassis ID to modify |
| 186 |
resource_id = module.params["resource_id"] |
| 187 |
|
| 188 |
# Build root URI |
| 189 |
root_uri = f"https://{module.params['baseuri']}" |
| 190 |
rf_utils = IdracRedfishUtils(creds, root_uri, timeout, module, resource_id=resource_id, data_modification=True) |
| 191 |
|
| 192 |
# Check that Category is valid |
| 193 |
if category not in CATEGORY_COMMANDS_ALL: |
| 194 |
module.fail_json(msg=f"Invalid Category '{category}'. Valid Categories = {list(CATEGORY_COMMANDS_ALL.keys())}") |
| 195 |
|
| 196 |
# Check that all commands are valid |
| 197 |
for cmd in command_list: |
| 198 |
# Fail if even one command given is invalid |
| 199 |
if cmd not in CATEGORY_COMMANDS_ALL[category]: |
| 200 |
module.fail_json(msg=f"Invalid Command '{cmd}'. Valid Commands = {CATEGORY_COMMANDS_ALL[category]}") |
| 201 |
|
| 202 |
# Organize by Categories / Commands |
| 203 |
|
| 204 |
if category == "Systems": |
| 205 |
# execute only if we find a System resource |
| 206 |
# NOTE: Currently overriding the usage of 'data_modification' due to |
| 207 |
# how 'resource_id' is processed. In the case of CreateBiosConfigJob, |
| 208 |
# we interact with BOTH systems and managers, so you currently cannot |
| 209 |
# specify a single 'resource_id' to make both '_find_systems_resource' |
| 210 |
# and '_find_managers_resource' return success. Since |
| 211 |
# CreateBiosConfigJob doesn't use the matched 'resource_id' for a |
| 212 |
# system regardless of what's specified, disabling the 'resource_id' |
| 213 |
# inspection for the next call allows a specific manager to be |
| 214 |
# specified with 'resource_id'. If we ever need to expand the input |
| 215 |
# to inspect a specific system and manager in parallel, this will need |
| 216 |
# updates. |
| 217 |
rf_utils.data_modification = False |
| 218 |
result = rf_utils._find_systems_resource() |
| 219 |
rf_utils.data_modification = True |
| 220 |
if result["ret"] is False: |
| 221 |
module.fail_json(msg=result["msg"]) |
| 222 |
|
| 223 |
for command in command_list: |
| 224 |
if command == "CreateBiosConfigJob": |
| 225 |
# execute only if we find a Managers resource |
| 226 |
result = rf_utils._find_managers_resource() |
| 227 |
if result["ret"] is False: |
| 228 |
module.fail_json(msg=result["msg"]) |
| 229 |
result = rf_utils.create_bios_config_job() |
| 230 |
if "job_id" in result: |
| 231 |
return_values["job_id"] = result["job_id"] |
| 232 |
|
| 233 |
# Return data back or fail with proper message |
| 234 |
if result["ret"] is True: |
| 235 |
del result["ret"] |
| 236 |
module.exit_json(changed=True, msg="Action was successful", return_values=return_values) |
| 237 |
else: |
| 238 |
module.fail_json(msg=result["msg"]) |
| 239 |
|
| 240 |
|
| 241 |
if __name__ == "__main__": |
| 242 |
main() |