Debian Codegraph @

ansible_collections/dellemc/openmanage/plugins/modules/ome_configuration_compliance_info.py

raw symbols SCIP
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 #
5 # Dell OpenManage Ansible Modules
6 # Version 10.0.1
7 # Copyright (C) 2021-2025 Dell Inc. or its subsidiaries. All Rights Reserved.
8
9 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
10 #
11
12
13 from __future__ import (absolute_import, division, print_function)
14 __metaclass__ = type
15
16
17 DOCUMENTATION = r'''
18 ---
19 module: ome_configuration_compliance_info
20 short_description: Device compliance report for devices managed in OpenManage Enterprise
21 version_added: "3.2.0"
22 description: This module allows the generation of a compliance report of a specific or all
23 of devices in a configuration compliance baseline.
24 extends_documentation_fragment:
25 - dellemc.openmanage.oment_auth_options
26 options:
27 baseline:
28 required: true
29 description:
30 - The name of the created baseline.
31 - A compliance report is generated even when the template is not associated with the baseline.
32 type: str
33 device_id:
34 required: false
35 description:
36 - The ID of the target device which is associated with the I(baseline).
37 type: int
38 device_service_tag:
39 required: false
40 description:
41 - The device service tag of the target device associated with the I(baseline).
42 - I(device_service_tag) is mutually exclusive with I(device_id).
43 type: str
44 requirements:
45 - "python >= 3.9.6"
46 author:
47 - "Felix Stephen A (@felixs88)"
48 - "Kritika Bhateja (@Kritika-Bhateja)"
49 - "Meenakshi Dembi (@meenakshidembi691)"
50 notes:
51 - Run this module from a system that has direct access to Dell OpenManage Enterprise.
52 - This module supports C(check_mode).
53 '''
54
55 EXAMPLES = r'''
56 ---
57 - name: Retrieve the compliance report of all of the devices in the specified configuration compliance baseline.
58 dellemc.openmanage.ome_configuration_compliance_info:
59 hostname: "192.168.0.1"
60 username: "username"
61 password: "password"
62 ca_path: "/path/to/ca_cert.pem"
63 baseline: baseline_name
64
65 - name: Retrieve the compliance report for a specific device associated with the baseline using the device ID.
66 dellemc.openmanage.ome_configuration_compliance_info:
67 hostname: "192.168.0.1"
68 username: "username"
69 password: "password"
70 ca_path: "/path/to/ca_cert.pem"
71 baseline: baseline_name
72 device_id: 10001
73
74 - name: Retrieve the compliance report for a specific device associated with the baseline using the device service tag.
75 dellemc.openmanage.ome_configuration_compliance_info:
76 hostname: "192.168.0.1"
77 username: "username"
78 password: "password"
79 ca_path: "/path/to/ca_cert.pem"
80 baseline: baseline_name
81 device_service_tag: 2HFGH3
82 '''
83
84 RETURN = r'''
85 ---
86 msg:
87 type: str
88 description: Over all compliance report status.
89 returned: on error
90 sample: "Unable to complete the operation because the entered target baseline name 'baseline' is invalid."
91 compliance_info:
92 type: dict
93 description: Returns the compliance report information.
94 returned: success
95 sample: [{
96 "ComplianceAttributeGroups": [{
97 "Attributes": [],
98 "ComplianceReason": "One or more attributes on the target device(s) does not match the compliance template.",
99 "ComplianceStatus": 2,
100 "ComplianceSubAttributeGroups": [{
101 "Attributes": [{
102 "AttributeId": 75369,
103 "ComplianceReason": "Attribute has different value from template",
104 "ComplianceStatus": 3,
105 "CustomId": 0,
106 "Description": null,
107 "DisplayName": "Workload Profile",
108 "ExpectedValue": "HpcProfile",
109 "Value": "NotAvailable"
110 }],
111 "ComplianceReason": "One or more attributes on the target device(s) does not match the compliance template.",
112 "ComplianceStatus": 2,
113 "ComplianceSubAttributeGroups": [],
114 "DisplayName": "System Profile Settings",
115 "GroupNameId": 1
116 }],
117 "DisplayName": "BIOS",
118 "GroupNameId": 1
119 }],
120 "ComplianceStatus": "NONCOMPLIANT",
121 "DeviceName": "WIN-PLOV8MPIP40",
122 "DeviceType": 1000,
123 "Id": 25011,
124 "InventoryTime": "2021-03-18 00:01:57.809771",
125 "Model": "PowerEdge R7525",
126 "ServiceTag": "JHMBX53"
127 }]
128 error_info:
129 description: Details of the HTTP Error.
130 returned: on HTTP error
131 type: dict
132 sample: {
133 "error": {
134 "code": "Base.1.0.GeneralError",
135 "message": "A general error has occurred. See ExtendedInfo for more information.",
136 "@Message.ExtendedInfo": [
137 {
138 "MessageId": "GEN1234",
139 "RelatedProperties": [],
140 "Message": "Unable to process the request because an error occurred.",
141 "MessageArgs": [],
142 "Severity": "Critical",
143 "Resolution": "Retry the operation. If the issue persists, contact your system administrator."
144 }
145 ]
146 }
147 }
148 '''
149
150 from ssl import SSLError
151 from ansible_collections.dellemc.openmanage.plugins.module_utils.ome import RestOME, OmeAnsibleModule
152 from ansible.module_utils.six.moves.urllib.error import URLError, HTTPError
153 from ansible.module_utils.urls import ConnectionError, SSLValidationError
154
155 BASELINE_URI = "TemplateService/Baselines"
156 CONFIG_COMPLIANCE_URI = "TemplateService/Baselines({0})/DeviceConfigComplianceReports"
157 COMPLIANCE_URI = "TemplateService/Baselines({0})/DeviceConfigComplianceReports({1})/DeviceComplianceDetails"
158
159
160 def validate_device(module, report, device_id=None, service_tag=None, base_id=None):
161 for each in report.get("value"):
162 if each["Id"] == device_id:
163 break
164 if each["ServiceTag"] == service_tag:
165 device_id = each["Id"]
166 break
167 else:
168 device_name = device_id if device_id is not None else service_tag
169 module.exit_json(msg="Unable to complete the operation because the entered "
170 "target device id or service tag '{0}' is invalid.".format(device_name), failed=True)
171 return device_id
172
173
174 def get_baseline_id(module, baseline_name, rest_obj):
175 report = rest_obj.get_all_report_details(BASELINE_URI)
176 base_id, template_id = None, None
177 for base in report["report_list"]:
178 if base["Name"] == baseline_name:
179 base_id = base["Id"]
180 template_id = base["TemplateId"]
181 break
182 else:
183 module.exit_json(msg="Unable to complete the operation because the entered "
184 "target baseline name '{0}' is invalid.".format(baseline_name), failed=True)
185 return base_id, template_id
186
187
188 def compliance_report(module, rest_obj):
189 baseline_name = module.params.get("baseline")
190 device_id = module.params.get("device_id")
191 device_service_tag = module.params.get("device_service_tag")
192 baseline_id, template_id = get_baseline_id(module, baseline_name, rest_obj)
193 report = []
194 if device_id:
195 compliance_uri = COMPLIANCE_URI.format(baseline_id, device_id)
196 baseline_report = rest_obj.invoke_request("GET", compliance_uri)
197 if not baseline_report.json_data.get("ComplianceAttributeGroups") and template_id == 0:
198 module.exit_json(msg="The compliance report of the device not found as "
199 "there is no template associated with the baseline.", failed=True)
200 device_compliance = baseline_report.json_data.get("ComplianceAttributeGroups")
201 else:
202 baseline_report = rest_obj.get_all_items_with_pagination(CONFIG_COMPLIANCE_URI.format(baseline_id))
203 if device_service_tag:
204 device_id = validate_device(module, baseline_report, device_id=device_id,
205 service_tag=device_service_tag, base_id=baseline_id)
206 report = list(filter(lambda d: d['Id'] in [device_id], baseline_report.get("value")))
207 else:
208 report = baseline_report.get("value")
209 device_compliance = report
210 if device_compliance:
211 for each in device_compliance:
212 compliance_uri = COMPLIANCE_URI.format(baseline_id, each["Id"])
213 attr_group = rest_obj.invoke_request("GET", compliance_uri)
214 each["ComplianceAttributeGroups"] = attr_group.json_data.get("ComplianceAttributeGroups")
215 return device_compliance
216
217
218 def main():
219 specs = {
220 "baseline": {"required": True, "type": "str"},
221 "device_id": {"required": False, "type": "int"},
222 "device_service_tag": {"required": False, "type": "str"},
223 }
224
225 module = OmeAnsibleModule(
226 argument_spec=specs,
227 mutually_exclusive=[["device_id", "device_service_tag"]],
228 supports_check_mode=True
229 )
230 try:
231 with RestOME(module.params, req_session=True) as rest_obj:
232 report = compliance_report(module, rest_obj)
233 module.exit_json(compliance_info=report)
234 except HTTPError as err:
235 module.exit_json(msg=str(err), failed=True)
236 except URLError as err:
237 module.exit_json(msg=str(err), unreachable=True)
238 except (IOError, ValueError, TypeError, SSLError, ConnectionError, SSLValidationError, OSError) as err:
239 module.exit_json(msg=str(err), failed=True)
240
241
242 if __name__ == '__main__':
243 main()