| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
from __future__ import (absolute_import, division, print_function) |
| 11 |
__metaclass__ = type |
| 12 |
|
| 13 |
DOCUMENTATION = r''' |
| 14 |
--- |
| 15 |
module: ome_firmware_baseline_compliance_info |
| 16 |
short_description: Retrieves baseline compliance details on OpenManage Enterprise |
| 17 |
version_added: "2.0.0" |
| 18 |
description: |
| 19 |
- This module allows to retrieve firmware compliance for a list of devices, |
| 20 |
or against a specified baseline on OpenManage Enterprise. |
| 21 |
extends_documentation_fragment: |
| 22 |
- dellemc.openmanage.ome_auth_options |
| 23 |
options: |
| 24 |
baseline_name: |
| 25 |
description: |
| 26 |
- Name of the baseline, for which the device compliance report is generated. |
| 27 |
- This option is mandatory for generating baseline based device compliance report. |
| 28 |
- I(baseline_name) is mutually exclusive with I(device_ids), I(device_service_tags) and I(device_group_names). |
| 29 |
type: str |
| 30 |
device_ids: |
| 31 |
description: |
| 32 |
- A list of unique identifier for device based compliance report. |
| 33 |
- Either I(device_ids), I(device_service_tags) or I(device_group_names) |
| 34 |
is required to generate device based compliance report. |
| 35 |
- I(device_ids) is mutually exclusive with I(device_service_tags), |
| 36 |
I(device_group_names) and I(baseline_name). |
| 37 |
- Devices without reports are ignored. |
| 38 |
type: list |
| 39 |
elements: int |
| 40 |
device_service_tags: |
| 41 |
description: |
| 42 |
- A list of service tags for device based compliance report. |
| 43 |
- Either I(device_ids), I(device_service_tags) or I(device_group_names) |
| 44 |
is required to generate device based compliance report. |
| 45 |
- I(device_service_tags) is mutually exclusive with I(device_ids), |
| 46 |
I(device_group_names) and I(baseline_name). |
| 47 |
- Devices without reports are ignored. |
| 48 |
type: list |
| 49 |
elements: str |
| 50 |
device_group_names: |
| 51 |
description: |
| 52 |
- A list of group names for device based compliance report. |
| 53 |
- Either I(device_ids), I(device_service_tags) or I(device_group_names) |
| 54 |
is required to generate device based compliance report. |
| 55 |
- I(device_group_names) is mutually exclusive with I(device_ids), |
| 56 |
I(device_service_tags) and I(baseline_name). |
| 57 |
- Devices without reports are ignored. |
| 58 |
type: list |
| 59 |
elements: str |
| 60 |
requirements: |
| 61 |
- "python >= 3.9.6" |
| 62 |
author: "Sajna Shetty(@Sajna-Shetty)" |
| 63 |
notes: |
| 64 |
- Run this module from a system that has direct access to Dell OpenManage Enterprise. |
| 65 |
- This module supports C(check_mode). |
| 66 |
''' |
| 67 |
|
| 68 |
EXAMPLES = r''' |
| 69 |
--- |
| 70 |
- name: Retrieves device based compliance report for specified device IDs |
| 71 |
dellemc.openmanage.ome_firmware_baseline_compliance_info: |
| 72 |
hostname: "192.168.0.1" |
| 73 |
username: "username" |
| 74 |
password: "password" |
| 75 |
ca_path: "/path/to/ca_cert.pem" |
| 76 |
device_ids: |
| 77 |
- 11111 |
| 78 |
- 22222 |
| 79 |
|
| 80 |
- name: Retrieves device based compliance report for specified service Tags |
| 81 |
dellemc.openmanage.ome_firmware_baseline_compliance_info: |
| 82 |
hostname: "192.168.0.1" |
| 83 |
username: "username" |
| 84 |
password: "password" |
| 85 |
ca_path: "/path/to/ca_cert.pem" |
| 86 |
device_service_tags: |
| 87 |
- MXL1234 |
| 88 |
- MXL4567 |
| 89 |
|
| 90 |
- name: Retrieves device based compliance report for specified group names |
| 91 |
dellemc.openmanage.ome_firmware_baseline_compliance_info: |
| 92 |
hostname: "192.168.0.1" |
| 93 |
username: "username" |
| 94 |
password: "password" |
| 95 |
ca_path: "/path/to/ca_cert.pem" |
| 96 |
device_group_names: |
| 97 |
- "group1" |
| 98 |
- "group2" |
| 99 |
|
| 100 |
- name: Retrieves device compliance report for a specified baseline |
| 101 |
dellemc.openmanage.ome_firmware_baseline_compliance_info: |
| 102 |
hostname: "192.168.0.1" |
| 103 |
username: "username" |
| 104 |
password: "password" |
| 105 |
ca_path: "/path/to/ca_cert.pem" |
| 106 |
baseline_name: "baseline_name" |
| 107 |
''' |
| 108 |
|
| 109 |
RETURN = r''' |
| 110 |
--- |
| 111 |
msg: |
| 112 |
type: str |
| 113 |
description: Overall baseline compliance report status. |
| 114 |
returned: on error |
| 115 |
sample: "Failed to fetch the compliance baseline information." |
| 116 |
baseline_compliance_info: |
| 117 |
type: dict |
| 118 |
description: Details of the baseline compliance report. |
| 119 |
returned: success |
| 120 |
sample: [ |
| 121 |
{ |
| 122 |
"CatalogId": 53, |
| 123 |
"ComplianceSummary": { |
| 124 |
"ComplianceStatus": "CRITICAL", |
| 125 |
"NumberOfCritical": 2, |
| 126 |
"NumberOfDowngrade": 0, |
| 127 |
"NumberOfNormal": 0, |
| 128 |
"NumberOfWarning": 0 |
| 129 |
}, |
| 130 |
"Description": "", |
| 131 |
"DeviceComplianceReports": [ |
| 132 |
{ |
| 133 |
"ComplianceStatus": "CRITICAL", |
| 134 |
"ComponentComplianceReports": [ |
| 135 |
{ |
| 136 |
"ComplianceDependencies": [], |
| 137 |
"ComplianceStatus": "DOWNGRADE", |
| 138 |
"Criticality": "Ok", |
| 139 |
"CurrentVersion": "OSC_1.1", |
| 140 |
"Id": 1258, |
| 141 |
"ImpactAssessment": "", |
| 142 |
"Name": "OS COLLECTOR 2.1", |
| 143 |
"Path": "FOLDER04118304M/2/Diagnostics_Application_JCCH7_WN64_4.0_A00_01.EXE", |
| 144 |
"PrerequisiteInfo": "", |
| 145 |
"RebootRequired": false, |
| 146 |
"SourceName": "DCIM:INSTALLED#802__OSCollector.Embedded.1", |
| 147 |
"TargetIdentifier": "101734", |
| 148 |
"UniqueIdentifier": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", |
| 149 |
"UpdateAction": "DOWNGRADE", |
| 150 |
"Uri": "http://www.dell.com/support/home/us/en/19/Drivers/DriversDetails?driverId=XXXXX", |
| 151 |
"Version": "4.0" |
| 152 |
}, |
| 153 |
{ |
| 154 |
"ComplianceDependencies": [], |
| 155 |
"ComplianceStatus": "CRITICAL", |
| 156 |
"Criticality": "Recommended", |
| 157 |
"CurrentVersion": "DN02", |
| 158 |
"Id": 1259, |
| 159 |
"ImpactAssessment": "", |
| 160 |
"Name": "TOSHIBA AL14SE 1.8 TB 2.5 12Gb 10K 512n SAS HDD Drive", |
| 161 |
"Path": "FOLDER04086111M/1/SAS-Drive_Firmware_VDGFM_WN64_DN03_A00.EXE", |
| 162 |
"PrerequisiteInfo": "", |
| 163 |
"RebootRequired": true, |
| 164 |
"SourceName": "DCIM:INSTALLED#304_C_Disk.Bay.1:Enclosure.Internal.0-1:RAID.Integrated.1-1", |
| 165 |
"TargetIdentifier": "103730", |
| 166 |
"UniqueIdentifier": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", |
| 167 |
"UpdateAction": "UPGRADE", |
| 168 |
"Uri": "http://www.dell.com/support/home/us/en/19/Drivers/DriversDetails?driverId=XXXXX", |
| 169 |
"Version": "DN03" |
| 170 |
} |
| 171 |
], |
| 172 |
"DeviceId": 11603, |
| 173 |
"DeviceModel": "PowerEdge R630", |
| 174 |
"DeviceName": null, |
| 175 |
"DeviceTypeId": 1000, |
| 176 |
"DeviceTypeName": "CPGCGS", |
| 177 |
"FirmwareStatus": "Non-Compliant", |
| 178 |
"Id": 194, |
| 179 |
"RebootRequired": true, |
| 180 |
"ServiceTag": "MXL1234" |
| 181 |
} |
| 182 |
], |
| 183 |
"DowngradeEnabled": true, |
| 184 |
"Id": 53, |
| 185 |
"Is64Bit": false, |
| 186 |
"LastRun": "2019-09-27 05:08:16.301", |
| 187 |
"Name": "baseline1", |
| 188 |
"RepositoryId": 43, |
| 189 |
"RepositoryName": "catalog2", |
| 190 |
"RepositoryType": "CIFS", |
| 191 |
"Targets": [ |
| 192 |
{ |
| 193 |
"Id": 11603, |
| 194 |
"Type": { |
| 195 |
"Id": 1000, |
| 196 |
"Name": "DEVICE" |
| 197 |
} |
| 198 |
} |
| 199 |
], |
| 200 |
"TaskId": 11710, |
| 201 |
"TaskStatusId": 0 |
| 202 |
} |
| 203 |
] |
| 204 |
error_info: |
| 205 |
type: dict |
| 206 |
description: Details of http error. |
| 207 |
returned: on http error |
| 208 |
sample: { |
| 209 |
"error": { |
| 210 |
"@Message.ExtendedInfo": [ |
| 211 |
{ |
| 212 |
"Message": "Unable to retrieve baseline list either because the device ID(s) entered are invalid", |
| 213 |
"Resolution": "Make sure the entered device ID(s) are valid and retry the operation.", |
| 214 |
"Severity": "Critical" |
| 215 |
} |
| 216 |
], |
| 217 |
"code": "Base.1.0.GeneralError", |
| 218 |
"message": "A general error has occurred. See ExtendedInfo for more information." |
| 219 |
} |
| 220 |
} |
| 221 |
''' |
| 222 |
|
| 223 |
import json |
| 224 |
from ansible_collections.dellemc.openmanage.plugins.module_utils.ome import RestOME, OmeAnsibleModule |
| 225 |
from ansible.module_utils.six.moves.urllib.error import URLError, HTTPError |
| 226 |
from ansible.module_utils.urls import ConnectionError, SSLValidationError |
| 227 |
|
| 228 |
|
| 229 |
base_line_path = "UpdateService/Baselines" |
| 230 |
baselines_report_by_device_ids_path = "UpdateService/Actions/UpdateService.GetBaselinesReportByDeviceids" |
| 231 |
device_is_list_path = "DeviceService/Devices" |
| 232 |
baselines_compliance_report_path = "UpdateService/Baselines({Id})/DeviceComplianceReports" |
| 233 |
group_service_path = "GroupService/Groups" |
| 234 |
EXIT_MESSAGE = "Unable to retrieve baseline list either because the device ID(s) entered are invalid, " \ |
| 235 |
"the ID(s) provided are not associated with a baseline or a group is used as a target for a baseline." |
| 236 |
MSG_ID = "CUPD3090" |
| 237 |
|
| 238 |
|
| 239 |
def _get_device_id_from_service_tags(service_tags, rest_obj, module): |
| 240 |
""" |
| 241 |
Get device ids from device service tag |
| 242 |
Returns :dict : device_id to service_tag map |
| 243 |
:arg service_tags: service tag |
| 244 |
:arg rest_obj: RestOME class object in case of request with session. |
| 245 |
:returns: dict eg: {1345:"MXL1245"} |
| 246 |
""" |
| 247 |
try: |
| 248 |
resp = rest_obj.get_all_report_details("DeviceService/Devices") |
| 249 |
devices_list = resp["report_list"] |
| 250 |
if devices_list: |
| 251 |
service_tag_dict = {} |
| 252 |
for item in devices_list: |
| 253 |
if item["DeviceServiceTag"] in service_tags: |
| 254 |
service_tag_dict.update({item["Id"]: item["DeviceServiceTag"]}) |
| 255 |
return service_tag_dict |
| 256 |
else: |
| 257 |
module.exit_json(msg="Unable to fetch the device information.", baseline_compliance_info=[]) |
| 258 |
except (URLError, HTTPError, SSLValidationError, ConnectionError, TypeError, ValueError) as err: |
| 259 |
module.exit_json(msg=str(err), failed=True) |
| 260 |
|
| 261 |
|
| 262 |
def get_device_ids_from_group_ids(module, grou_id_list, rest_obj): |
| 263 |
try: |
| 264 |
device_id_list = [] |
| 265 |
for group_id in grou_id_list: |
| 266 |
group_id_path = group_service_path + "({group_id})/Devices".format(group_id=group_id) |
| 267 |
resp_val = rest_obj.get_all_items_with_pagination(group_id_path) |
| 268 |
grp_list_value = resp_val["value"] |
| 269 |
if grp_list_value: |
| 270 |
for device_item in grp_list_value: |
| 271 |
device_id_list.append(device_item["Id"]) |
| 272 |
if len(device_id_list) == 0: |
| 273 |
module.exit_json(msg="Unable to fetch the device ids from specified device_group_names.", |
| 274 |
baseline_compliance_info=[]) |
| 275 |
return device_id_list |
| 276 |
except (URLError, HTTPError, SSLValidationError, ConnectionError, TypeError, ValueError) as err: |
| 277 |
module.exit_json(msg=str(err), failed=True) |
| 278 |
|
| 279 |
|
| 280 |
def get_device_ids_from_group_names(module, rest_obj): |
| 281 |
try: |
| 282 |
grp_name_list = module.params.get("device_group_names") |
| 283 |
resp = rest_obj.get_all_report_details(group_service_path) |
| 284 |
group_id_list = [] |
| 285 |
grp_list_resp = resp["report_list"] |
| 286 |
if grp_list_resp: |
| 287 |
for name in grp_name_list: |
| 288 |
for group in grp_list_resp: |
| 289 |
if group["Name"] == name: |
| 290 |
group_id_list.append(group['Id']) |
| 291 |
break |
| 292 |
else: |
| 293 |
module.exit_json(msg="Unable to fetch the specified device_group_names.", |
| 294 |
baseline_compliance_info=[]) |
| 295 |
return get_device_ids_from_group_ids(module, group_id_list, rest_obj) |
| 296 |
except (URLError, HTTPError, SSLValidationError, ConnectionError, TypeError, ValueError) as err: |
| 297 |
module.exit_json(msg=str(err), failed=True) |
| 298 |
|
| 299 |
|
| 300 |
def get_identifiers(rest_obj, module): |
| 301 |
if module.params.get("device_ids") is not None: |
| 302 |
return module.params.get("device_ids"), "device_ids" |
| 303 |
elif module.params.get("device_group_names") is not None: |
| 304 |
return get_device_ids_from_group_names(module, rest_obj), "device_group_names" |
| 305 |
else: |
| 306 |
service_tags = module.params.get("device_service_tags") |
| 307 |
service_tags_mapper = _get_device_id_from_service_tags(service_tags, rest_obj, module) |
| 308 |
return list(service_tags_mapper.keys()), "device_service_tags" |
| 309 |
|
| 310 |
|
| 311 |
def get_baseline_id_from_name(rest_obj, module): |
| 312 |
try: |
| 313 |
baseline_name = module.params.get("baseline_name") |
| 314 |
baseline_id = 0 |
| 315 |
if baseline_name is not None: |
| 316 |
resp_val = rest_obj.get_all_items_with_pagination(base_line_path) |
| 317 |
baseline_list = resp_val["value"] |
| 318 |
if baseline_list: |
| 319 |
for baseline in baseline_list: |
| 320 |
if baseline["Name"] == baseline_name: |
| 321 |
baseline_id = baseline["Id"] |
| 322 |
break |
| 323 |
else: |
| 324 |
module.exit_json(msg="Specified baseline_name does not exist in the system.", |
| 325 |
baseline_compliance_info=[]) |
| 326 |
else: |
| 327 |
module.exit_json(msg="No baseline exists in the system.", baseline_compliance_info=[]) |
| 328 |
else: |
| 329 |
module.exit_json(msg="baseline_name is a mandatory option.", failed=True) |
| 330 |
return baseline_id |
| 331 |
except (URLError, HTTPError, SSLValidationError, ConnectionError, TypeError, ValueError) as err: |
| 332 |
module.exit_json(msg=str(err), failed=True) |
| 333 |
|
| 334 |
|
| 335 |
def get_baselines_report_by_device_ids(rest_obj, module): |
| 336 |
try: |
| 337 |
device_ids, identifier = get_identifiers(rest_obj, module) |
| 338 |
if device_ids or identifier == "device_ids": |
| 339 |
resp = rest_obj.invoke_request('POST', baselines_report_by_device_ids_path, data={"Ids": device_ids}) |
| 340 |
return resp.json_data |
| 341 |
else: |
| 342 |
identifier_map = { |
| 343 |
"device_group_names": "Device details not available as the group name(s) provided are invalid.", |
| 344 |
"device_service_tags": "Device details not available as the service tag(s) provided are invalid." |
| 345 |
} |
| 346 |
message = identifier_map[identifier] |
| 347 |
module.exit_json(msg=message) |
| 348 |
except HTTPError as err: |
| 349 |
err_message = json.load(err) |
| 350 |
err_list = err_message.get('error', {}).get('@Message.ExtendedInfo', [{"Message": EXIT_MESSAGE}]) |
| 351 |
if err_list: |
| 352 |
err_reason = err_list[0].get("Message", EXIT_MESSAGE) |
| 353 |
if MSG_ID in err_list[0].get('MessageId'): |
| 354 |
module.exit_json(msg=err_reason) |
| 355 |
module.exit_json(msg=str(err), failed=True) |
| 356 |
except (URLError, SSLValidationError, ConnectionError, TypeError, ValueError) as err: |
| 357 |
module.exit_json(msg=str(err), failed=True) |
| 358 |
|
| 359 |
|
| 360 |
def get_baseline_compliance_reports(rest_obj, module): |
| 361 |
try: |
| 362 |
baseline_id = get_baseline_id_from_name(rest_obj, module) |
| 363 |
path = baselines_compliance_report_path.format(Id=baseline_id) |
| 364 |
resp_val = rest_obj.get_all_items_with_pagination(path) |
| 365 |
resp_data = resp_val["value"] |
| 366 |
return resp_data |
| 367 |
except (URLError, HTTPError, SSLValidationError, ConnectionError, TypeError, ValueError) as err: |
| 368 |
module.exit_json(msg=str(err), failed=True) |
| 369 |
|
| 370 |
|
| 371 |
def validate_inputs(module): |
| 372 |
module_params = module.params |
| 373 |
device_service_tags = module_params.get("device_service_tags") |
| 374 |
device_group_names = module_params.get("device_group_names") |
| 375 |
device_ids = module_params.get("device_ids") |
| 376 |
baseline_name = module_params.get("baseline_name") |
| 377 |
if all(not identifer for identifer in [device_ids, device_service_tags, device_group_names, baseline_name]): |
| 378 |
module.exit_json(msg="one of the following is required: device_ids, device_service_tags, " |
| 379 |
"device_group_names, baseline_name to generate device based compliance report.", failed=True) |
| 380 |
|
| 381 |
|
| 382 |
def main(): |
| 383 |
specs = { |
| 384 |
"baseline_name": {"type": 'str', "required": False}, |
| 385 |
"device_service_tags": {"required": False, "type": "list", "elements": 'str'}, |
| 386 |
"device_ids": {"required": False, "type": "list", "elements": 'int'}, |
| 387 |
"device_group_names": {"required": False, "type": "list", "elements": 'str'}, |
| 388 |
} |
| 389 |
|
| 390 |
module = OmeAnsibleModule( |
| 391 |
argument_spec=specs, |
| 392 |
mutually_exclusive=[['baseline_name', 'device_service_tags', 'device_ids', 'device_group_names']], |
| 393 |
required_one_of=[['device_ids', 'device_service_tags', 'device_group_names', 'baseline_name']], |
| 394 |
supports_check_mode=True |
| 395 |
) |
| 396 |
try: |
| 397 |
validate_inputs(module) |
| 398 |
with RestOME(module.params, req_session=True) as rest_obj: |
| 399 |
baseline_name = module.params.get("baseline_name") |
| 400 |
if baseline_name is not None: |
| 401 |
data = get_baseline_compliance_reports(rest_obj, module) |
| 402 |
else: |
| 403 |
data = get_baselines_report_by_device_ids(rest_obj, module) |
| 404 |
if data: |
| 405 |
module.exit_json(baseline_compliance_info=data) |
| 406 |
else: |
| 407 |
module.exit_json(msg="Unable to fetch the compliance baseline information.") |
| 408 |
except HTTPError as err: |
| 409 |
module.exit_json(msg=str(err), error_info=json.load(err), failed=True) |
| 410 |
except (URLError, SSLValidationError, ConnectionError, TypeError, ValueError, OSError) as err: |
| 411 |
module.exit_json(msg=str(err), failed=True) |
| 412 |
|
| 413 |
|
| 414 |
if __name__ == '__main__': |
| 415 |
main() |