| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
from __future__ import (absolute_import, division, print_function) |
| 11 |
|
| 12 |
__metaclass__ = type |
| 13 |
|
| 14 |
DOCUMENTATION = """ |
| 15 |
--- |
| 16 |
module: ome_profile_info |
| 17 |
short_description: Retrieve profiles with attribute details |
| 18 |
version_added: "7.2.0" |
| 19 |
description: |
| 20 |
- "This module retrieve profiles with attributes on OpenManage Enterprise or OpenManage Enterprise Modular." |
| 21 |
extends_documentation_fragment: |
| 22 |
- dellemc.openmanage.ome_auth_options |
| 23 |
options: |
| 24 |
profile_id: |
| 25 |
description: |
| 26 |
- Id of the profile. |
| 27 |
- This is mutually exclusive with I(profile_name), I(system_query_options), I(template_id), and I(template_name). |
| 28 |
type: int |
| 29 |
profile_name: |
| 30 |
description: |
| 31 |
- Name of the profile. |
| 32 |
- This is mutually exclusive with I(template_id), I(profile_id), I(system_query_options), and I(template_name). |
| 33 |
type: str |
| 34 |
template_id: |
| 35 |
description: |
| 36 |
- Provide the ID of the template to retrieve the list of profile(s) linked to it. |
| 37 |
- This is mutually exclusive with I(profile_name), I(profile_id), I(system_query_options), and I(template_name). |
| 38 |
type: int |
| 39 |
template_name: |
| 40 |
description: |
| 41 |
- Provide the name of the template to retrieve the list of profile(s) linked to it. |
| 42 |
- This is mutually exclusive with I(profile_name), I(profile_id), I(template_id), and I(system_query_options). |
| 43 |
type: str |
| 44 |
system_query_options: |
| 45 |
description: |
| 46 |
- Option for providing supported odata filters. |
| 47 |
- "The profile list can be fetched and sorted based on ProfileName, TemplateName, TargetTypeId, TargetName, |
| 48 |
ChassisName, ProfileState, LastRunStatus, or ProfileModified." |
| 49 |
- This is mutually exclusive with I(profile_name), I(profile_id), I(template_id), and I(template_name). |
| 50 |
- "C(Note) If I(profile_name), I(profile_id), I(template_id), or I(template_name) option is not provided, the |
| 51 |
module retrieves all the profiles." |
| 52 |
type: dict |
| 53 |
requirements: |
| 54 |
- "python >= 3.9.6" |
| 55 |
author: |
| 56 |
- Jagadeesh N V(@jagadeeshnv) |
| 57 |
notes: |
| 58 |
- Run this module on a system that has direct access to Dell OpenManage Enterprise. |
| 59 |
- This module supports C(check_mode). |
| 60 |
""" |
| 61 |
|
| 62 |
EXAMPLES = """ |
| 63 |
--- |
| 64 |
- name: Retrieve all profiles |
| 65 |
dellemc.openmanage.ome_profile_info: |
| 66 |
hostname: "192.168.0.1" |
| 67 |
username: "username" |
| 68 |
password: "password" |
| 69 |
ca_path: "/path/to/ca_cert.pem" |
| 70 |
|
| 71 |
- name: Retrieve profile using the name |
| 72 |
dellemc.openmanage.ome_profile_info: |
| 73 |
hostname: "192.168.0.1" |
| 74 |
username: "username" |
| 75 |
password: "password" |
| 76 |
ca_path: "/path/to/ca_cert.pem" |
| 77 |
profile_name: eprof 00001 |
| 78 |
|
| 79 |
- name: Retrieve profile using the id |
| 80 |
dellemc.openmanage.ome_profile_info: |
| 81 |
hostname: "192.168.0.1" |
| 82 |
username: "username" |
| 83 |
password: "password" |
| 84 |
ca_path: "/path/to/ca_cert.pem" |
| 85 |
profile_id: 10129 |
| 86 |
|
| 87 |
- name: Retrieve the profiles using the template name |
| 88 |
dellemc.openmanage.ome_profile_info: |
| 89 |
hostname: "192.168.0.1" |
| 90 |
username: "username" |
| 91 |
password: "password" |
| 92 |
ca_path: "/path/to/ca_cert.pem" |
| 93 |
template_name: t2 |
| 94 |
|
| 95 |
- name: Retrieve the profiles using the template id |
| 96 |
dellemc.openmanage.ome_profile_info: |
| 97 |
hostname: "192.168.0.1" |
| 98 |
username: "username" |
| 99 |
password: "password" |
| 100 |
ca_path: "/path/to/ca_cert.pem" |
| 101 |
template_id: 11 |
| 102 |
|
| 103 |
- name: Retrieve the profiles based on the odata filters |
| 104 |
dellemc.openmanage.ome_profile_info: |
| 105 |
hostname: "192.168.0.1" |
| 106 |
username: "username" |
| 107 |
password: "password" |
| 108 |
ca_path: "/path/to/ca_cert.pem" |
| 109 |
system_query_options: |
| 110 |
filter: TemplateName eq 'mytemplate' |
| 111 |
orderby: ProfileState |
| 112 |
""" |
| 113 |
|
| 114 |
RETURN = r''' |
| 115 |
--- |
| 116 |
msg: |
| 117 |
description: Status of profile information retrieval. |
| 118 |
returned: always |
| 119 |
type: str |
| 120 |
sample: "Successfully retrieved the profile information." |
| 121 |
profile_info: |
| 122 |
description: Information about the profile. |
| 123 |
returned: success |
| 124 |
type: list |
| 125 |
elements: dict |
| 126 |
sample: |
| 127 |
[ |
| 128 |
{ |
| 129 |
"Id": 71460, |
| 130 |
"ProfileName": "Profile 00001", |
| 131 |
"ProfileDescription": "from source template: (Template)", |
| 132 |
"TemplateId": 8, |
| 133 |
"TemplateName": "Template", |
| 134 |
"DataSchemaId": 8, |
| 135 |
"TargetId": 0, |
| 136 |
"TargetName": null, |
| 137 |
"TargetTypeId": 0, |
| 138 |
"DeviceIdInSlot": 0, |
| 139 |
"ChassisId": 0, |
| 140 |
"ChassisName": null, |
| 141 |
"GroupId": 0, |
| 142 |
"GroupName": null, |
| 143 |
"NetworkBootToIso": null, |
| 144 |
"ProfileState": 0, |
| 145 |
"DeploymentTaskId": 0, |
| 146 |
"LastRunStatus": 2200, |
| 147 |
"ProfileModified": 0, |
| 148 |
"CreatedBy": "admin", |
| 149 |
"EditedBy": null, |
| 150 |
"CreatedDate": "2019-09-26 13:56:41.924966", |
| 151 |
"LastEditDate": "2020-12-11 08:27:20.500564", |
| 152 |
"LastDeployDate": "", |
| 153 |
"AttributeIdMap": { |
| 154 |
"4965": { |
| 155 |
"Value": "hostname", |
| 156 |
"IsReadOnly": false, |
| 157 |
"IsIgnored": true |
| 158 |
}, |
| 159 |
"4963": { |
| 160 |
"Value": "second floor", |
| 161 |
"IsReadOnly": false, |
| 162 |
"IsIgnored": true |
| 163 |
}, |
| 164 |
"4960": { |
| 165 |
"Value": "10A", |
| 166 |
"IsReadOnly": false, |
| 167 |
"IsIgnored": true |
| 168 |
}, |
| 169 |
"4959": { |
| 170 |
"Value": "OMAMDEV", |
| 171 |
"IsReadOnly": false, |
| 172 |
"IsIgnored": true |
| 173 |
}, |
| 174 |
"4957": { |
| 175 |
"Value": "Dell LAB", |
| 176 |
"IsReadOnly": false, |
| 177 |
"IsIgnored": true |
| 178 |
}, |
| 179 |
"4958": { |
| 180 |
"Value": null, |
| 181 |
"IsReadOnly": false, |
| 182 |
"IsIgnored": true |
| 183 |
}, |
| 184 |
"4066": { |
| 185 |
"Value": null, |
| 186 |
"IsReadOnly": false, |
| 187 |
"IsIgnored": true |
| 188 |
}, |
| 189 |
"4231": { |
| 190 |
"Value": "1", |
| 191 |
"IsReadOnly": false, |
| 192 |
"IsIgnored": false |
| 193 |
}, |
| 194 |
"4229": { |
| 195 |
"Value": "Disabled", |
| 196 |
"IsReadOnly": false, |
| 197 |
"IsIgnored": false |
| 198 |
} |
| 199 |
}, |
| 200 |
"AttributeDetails": { |
| 201 |
"System": { |
| 202 |
"Server Operating System": { |
| 203 |
"ServerOS 1 Server Host Name": 4965 |
| 204 |
}, |
| 205 |
"Server Topology": { |
| 206 |
"ServerTopology 1 Room Name": 4963, |
| 207 |
"ServerTopology 1 Rack Slot": 4960, |
| 208 |
"ServerTopology 1 Rack Name": 4959, |
| 209 |
"ServerTopology 1 Data Center Name": 4957, |
| 210 |
"ServerTopology 1 Aisle Name": 4958 |
| 211 |
} |
| 212 |
}, |
| 213 |
"iDRAC": { |
| 214 |
"Active Directory": { |
| 215 |
"ActiveDirectory 1 Active Directory RAC Name": 4066 |
| 216 |
}, |
| 217 |
"NIC Information": { |
| 218 |
"NIC 1 VLAN ID": 4231, |
| 219 |
"NIC 1 Enable VLAN": 4229 |
| 220 |
} |
| 221 |
} |
| 222 |
} |
| 223 |
} |
| 224 |
] |
| 225 |
error_info: |
| 226 |
description: Details of the HTTP Error. |
| 227 |
returned: on HTTP error |
| 228 |
type: dict |
| 229 |
sample: { |
| 230 |
"error": { |
| 231 |
"code": "Base.1.0.GeneralError", |
| 232 |
"message": "A general error has occurred. See ExtendedInfo for more information.", |
| 233 |
"@Message.ExtendedInfo": [ |
| 234 |
{ |
| 235 |
"MessageId": "GEN1234", |
| 236 |
"RelatedProperties": [], |
| 237 |
"Message": "Unable to process the request because an error occurred.", |
| 238 |
"MessageArgs": [], |
| 239 |
"Severity": "Critical", |
| 240 |
"Resolution": "Retry the operation. If the issue persists, contact your system administrator." |
| 241 |
} |
| 242 |
] |
| 243 |
} |
| 244 |
} |
| 245 |
''' |
| 246 |
|
| 247 |
import json |
| 248 |
from ansible.module_utils.six.moves.urllib.error import URLError, HTTPError |
| 249 |
from ansible.module_utils.urls import ConnectionError |
| 250 |
from ansible_collections.dellemc.openmanage.plugins.module_utils.ome import RestOME, OmeAnsibleModule |
| 251 |
from ansible_collections.dellemc.openmanage.plugins.module_utils.utils import strip_substr_dict |
| 252 |
|
| 253 |
|
| 254 |
PROFILE_VIEW = "ProfileService/Profiles" |
| 255 |
TEMPLATE_VIEW = "TemplateService/Templates" |
| 256 |
SUCCESS_MSG = "Successfully retrieved the profile information." |
| 257 |
NO_PROFILES_MSG = "Profiles with {0} {1} not found." |
| 258 |
SEPRTR = ',' |
| 259 |
QUERY_FILTER_PARAM = "$filter" |
| 260 |
|
| 261 |
|
| 262 |
def get_template_details(module, rest_obj): |
| 263 |
templt_id = module.params.get('template_id') |
| 264 |
query_param = {QUERY_FILTER_PARAM: "Id eq {0}".format(templt_id)} |
| 265 |
srch = 'Id' |
| 266 |
t_id = 'template_id' |
| 267 |
if not templt_id: |
| 268 |
templt_id = module.params.get('template_name') |
| 269 |
query_param = {QUERY_FILTER_PARAM: "Name eq '{0}'".format(templt_id)} |
| 270 |
srch = 'Name' |
| 271 |
t_id = 'template_name' |
| 272 |
resp = rest_obj.invoke_request('GET', TEMPLATE_VIEW, query_param=query_param) |
| 273 |
if resp.success and resp.json_data.get('value'): |
| 274 |
tlist = resp.json_data.get('value', []) |
| 275 |
for xtype in tlist: |
| 276 |
if xtype.get(srch) == templt_id: |
| 277 |
return xtype, templt_id, t_id |
| 278 |
module.exit_json(failed=True, msg="Template with {0} '{1}' not found.".format(srch.lower(), templt_id)) |
| 279 |
|
| 280 |
|
| 281 |
def get_profile_query(rest_obj, query, url_prm): |
| 282 |
prof_list = [] |
| 283 |
try: |
| 284 |
if query: |
| 285 |
resp = rest_obj.get_all_items_with_pagination(PROFILE_VIEW, query_param=query) |
| 286 |
prof_list = resp.get("value") |
| 287 |
if url_prm: |
| 288 |
url_resp = rest_obj.invoke_request("GET", "{0}{1}".format(PROFILE_VIEW, url_prm)) |
| 289 |
prof_list = [url_resp.json_data] |
| 290 |
except Exception: |
| 291 |
prof_list = [] |
| 292 |
return prof_list |
| 293 |
|
| 294 |
|
| 295 |
def construct_tree_str(nprfx, attr_detailed): |
| 296 |
str_lst = nprfx.split(SEPRTR) |
| 297 |
br = attr_detailed |
| 298 |
for xs in str_lst: |
| 299 |
if xs not in br: |
| 300 |
br[xs] = {} |
| 301 |
br = br.get(xs) |
| 302 |
return br |
| 303 |
|
| 304 |
|
| 305 |
def recurse_subattr_list(subgroup, prefix, attr_detailed, attr_map): |
| 306 |
rq_attr = ["Value", "IsReadOnly", "IsIgnored"] |
| 307 |
if isinstance(subgroup, list): |
| 308 |
for each_sub in subgroup: |
| 309 |
nprfx = "{0}{1}{2}".format(prefix, SEPRTR, each_sub.get("DisplayName")) |
| 310 |
if each_sub.get("SubAttributeGroups"): |
| 311 |
recurse_subattr_list(each_sub.get("SubAttributeGroups"), nprfx, attr_detailed, attr_map) |
| 312 |
else: |
| 313 |
for attr in each_sub.get('Attributes'): |
| 314 |
nd = construct_tree_str(nprfx, attr_detailed) |
| 315 |
nd[attr['DisplayName']] = attr['AttributeId'] |
| 316 |
vlist = {xf: attr.get(xf) for xf in rq_attr} |
| 317 |
attr_map[attr['AttributeId']] = vlist |
| 318 |
|
| 319 |
|
| 320 |
def get_subattr_all(attr_dtls): |
| 321 |
attr_detailed = {} |
| 322 |
attr_map = {} |
| 323 |
for each in attr_dtls: |
| 324 |
recurse_subattr_list(each.get('SubAttributeGroups'), each.get('DisplayName'), attr_detailed, attr_map) |
| 325 |
return attr_detailed, attr_map |
| 326 |
|
| 327 |
|
| 328 |
def get_attribute_detail_tree(rest_obj, prof_id): |
| 329 |
try: |
| 330 |
resp = rest_obj.invoke_request('GET', "{0}({1})/AttributeDetails".format(PROFILE_VIEW, prof_id)) |
| 331 |
attr_list = resp.json_data.get("AttributeGroups") |
| 332 |
attr_detailed, attr_map = get_subattr_all(attr_list) |
| 333 |
except Exception: |
| 334 |
attr_detailed, attr_map = {}, {} |
| 335 |
return attr_detailed, attr_map |
| 336 |
|
| 337 |
|
| 338 |
def prepare_query_objects(module, rest_obj): |
| 339 |
query = {} |
| 340 |
url_prm = None |
| 341 |
name = value = None |
| 342 |
op_params = module.params |
| 343 |
if op_params.get("template_id") or op_params.get("template_name"): |
| 344 |
tmplt, value, name = get_template_details(module, rest_obj) |
| 345 |
query[QUERY_FILTER_PARAM] = "TemplateName eq '{0}'".format(tmplt.get('Name')) |
| 346 |
elif op_params.get("profile_id"): |
| 347 |
value = op_params.get("profile_id") |
| 348 |
url_prm = f"({value})" |
| 349 |
name = "profile_id" |
| 350 |
elif op_params.get("profile_name"): |
| 351 |
value = op_params.get("profile_name") |
| 352 |
query[QUERY_FILTER_PARAM] = f"ProfileName eq '{value}'" |
| 353 |
name = "profile_name" |
| 354 |
elif op_params.get("system_query_options"): |
| 355 |
name = "provided" |
| 356 |
value = "system_query_options" |
| 357 |
for k, v in op_params.get("system_query_options").items(): |
| 358 |
query[f"${k}"] = v |
| 359 |
return query, url_prm, name, value |
| 360 |
|
| 361 |
|
| 362 |
def fetch_profile_list(module, rest_obj, query, url_prm): |
| 363 |
if query or url_prm: |
| 364 |
prof_list = get_profile_query(rest_obj, query, url_prm) |
| 365 |
if module.params.get("profile_name"): |
| 366 |
xprofs = [] |
| 367 |
pname = module.params.get("profile_name") |
| 368 |
for xp in prof_list: |
| 369 |
if xp.get("ProfileName") == pname: |
| 370 |
xprofs.append(xp) |
| 371 |
break |
| 372 |
prof_list = xprofs |
| 373 |
else: |
| 374 |
resp = rest_obj.get_all_items_with_pagination(PROFILE_VIEW) |
| 375 |
prof_list = resp.get("value", []) |
| 376 |
if not prof_list: |
| 377 |
module.exit_json(msg=SUCCESS_MSG, profile_info=prof_list) |
| 378 |
return prof_list |
| 379 |
|
| 380 |
|
| 381 |
def main(): |
| 382 |
argument_spec = { |
| 383 |
"profile_id": {"type": 'int'}, |
| 384 |
"profile_name": {"type": 'str'}, |
| 385 |
"template_id": {"type": 'int'}, |
| 386 |
"template_name": {"type": 'str'}, |
| 387 |
"system_query_options": {"type": 'dict'} |
| 388 |
} |
| 389 |
module = OmeAnsibleModule(argument_spec=argument_spec, |
| 390 |
mutually_exclusive=[('profile_id', 'profile_name', 'template_name', 'template_id', |
| 391 |
'system_query_options')], |
| 392 |
supports_check_mode=True) |
| 393 |
try: |
| 394 |
with RestOME(module.params, req_session=True) as rest_obj: |
| 395 |
query, url_prm, name, value = prepare_query_objects(module, rest_obj) |
| 396 |
prof_list = fetch_profile_list(module, rest_obj, query, url_prm) |
| 397 |
for xp in prof_list: |
| 398 |
attr_tree, attr_map = get_attribute_detail_tree(rest_obj, xp["Id"]) |
| 399 |
xp["AttributeIdMap"] = attr_map |
| 400 |
xp["AttributeDetails"] = attr_tree |
| 401 |
strip_substr_dict(xp) |
| 402 |
if prof_list: |
| 403 |
module.exit_json(msg=SUCCESS_MSG, profile_info=prof_list) |
| 404 |
else: |
| 405 |
module.exit_json(msg=NO_PROFILES_MSG.format(name, value), failed=True) |
| 406 |
except HTTPError as err: |
| 407 |
module.exit_json(msg=str(err), error_info=json.load(err), failed=True) |
| 408 |
except URLError as err: |
| 409 |
module.exit_json(msg=str(err), unreachable=True) |
| 410 |
except (IOError, ValueError, TypeError, ConnectionError, |
| 411 |
AttributeError, IndexError, KeyError, OSError) as err: |
| 412 |
module.exit_json(msg=str(err), failed=True) |
| 413 |
|
| 414 |
|
| 415 |
if __name__ == '__main__': |
| 416 |
main() |