Debian Codegraph @

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

raw symbols SCIP
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 #
5 # Dell OpenManage Ansible Modules
6 # Version 9.9.0
7 # Copyright (C) 2024-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
15 __metaclass__ = type
16
17 DOCUMENTATION = r"""
18 ---
19 module: omevv_firmware_compliance_info
20 short_description: Retrieve firmware compliance report.
21 version_added: "9.9.0"
22 description:
23 - This module allows you to retrieve firmware compliance reports of all the
24 hosts of the cluster, a specific host of the cluster, or multiple clusters.
25 extends_documentation_fragment:
26 - dellemc.openmanage.omevv_auth_options
27 options:
28 clusters:
29 description:
30 - Cluster details to retrieve the firmware compliance report.
31 type: list
32 elements: dict
33 suboptions:
34 cluster_name:
35 description:
36 - Cluster name of the hosts for which the firmware compliance report should be retrieved.
37 - If I(servicetags) or I(hosts) is provided, then the firmware compliance report of only the specified hosts
38 is retrieved and displayed.
39 required: true
40 type: str
41 servicetags:
42 description:
43 - The service tag of the hosts for which the firmware compliance
44 reports must be retrieved.
45 type: list
46 elements: str
47 hosts:
48 description:
49 - The IP address or hostname of the hosts for which the firmware
50 compliance reports must be retrieved.
51 type: list
52 elements: str
53 requirements:
54 - "python >= 3.9.6"
55 author:
56 - "Abhishek Sinha(@ABHISHEK-SINHA10)"
57 attributes:
58 check_mode:
59 description: Runs task to validate without performing action on the target machine.
60 support: full
61 diff_mode:
62 description: Runs the task to report the changes that are made or the changes that must be applied.
63 support: full
64 notes:
65 - Run this module from a system that has direct access to Dell OpenManage Enterprise.
66 """
67
68 EXAMPLES = r"""
69 ---
70 - name: Retrieve a firmware compliance report of all the clusters
71 dellemc.openmanage.omevv_firmware_compliance_info:
72 hostname: "192.168.0.1"
73 vcenter_uuid: "xxxxx"
74 vcenter_username: "username"
75 vcenter_password: "password"
76 ca_path: "path/to/ca_file"
77
78 - name: Retrieve a firmware compliance report of all the hosts in a specific cluster
79 dellemc.openmanage.omevv_firmware_compliance_info:
80 hostname: "192.168.0.1"
81 vcenter_uuid: "xxxxx"
82 vcenter_username: "username"
83 vcenter_password: "password"
84 ca_path: "path/to/ca_file"
85 clusters:
86 - cluster_name: cluster_a
87
88 - name: Retrieve a firmware compliance report of specific hosts in the cluster
89 dellemc.openmanage.omevv_firmware_compliance_info:
90 hostname: "192.168.0.1"
91 vcenter_uuid: "xxxxx"
92 vcenter_username: "username"
93 vcenter_password: "password"
94 ca_path: "path/to/ca_file"
95 clusters:
96 - cluster_name: cluster_a
97 servicetags:
98 - SVCTAG1
99 - SVCTAG2
100 hosts:
101 - host1
102 - xx.xx.xx.xx
103
104 - name: Retrieve a firmware compliance report of multiple clusters
105 dellemc.openmanage.omevv_firmware_compliance_info:
106 hostname: "192.168.0.1"
107 vcenter_uuid: "xxxxx"
108 vcenter_username: "username"
109 vcenter_password: "password"
110 ca_path: "path/to/ca_file"
111 clusters:
112 - cluster_name: cluster_a
113 - cluster_name: cluster_b
114 """
115
116 RETURN = r'''
117 ---
118 msg:
119 type: str
120 description: Retrive the firmware compliance report.
121 returned: always
122 sample: "Successfully fetched the firmware compliance report."
123 firmware_compliance_info:
124 description: Details of the compliance report.
125 returned: on HTTP error
126 type: list
127 elements: dict
128 sample:
129 [{
130 "complianceStatus": "NonCompliant",
131 "cluster": "cluster_a",
132 "hostComplianceReports": [
133 {
134 "hostId": 1002,
135 "hostAddress": "XX.XX.XX.XX",
136 "serviceTag": "SVCTAG",
137 "deviceModel": "PowerEdge R660xs",
138 "complianceStatus": "WARNING",
139 "componentCompliances": [
140 {
141 "driftStatus": "NonCompliant",
142 "componentName": "Enterprise UEFI Diagnostics",
143 "currentValue": "4303A15",
144 "baselineValue": "4303A19",
145 "criticality": "Optional",
146 "updateAction": "UPGRADE",
147 "sourceName": "DCIM:INSTALLED#802__Diagnostics.Embedded.1:LC.Embedded.1",
148 "complianceStatus": "WARNING",
149 "rebootRequired": false
150 }]
151 }]
152 }]
153 '''
154 import json
155 from copy import deepcopy
156 from urllib.error import URLError, HTTPError
157 from ansible.module_utils.urls import ConnectionError
158 from ansible_collections.dellemc.openmanage.plugins.module_utils.omevv import RestOMEVV, OMEVVAnsibleModule
159 from ansible_collections.dellemc.openmanage.plugins.module_utils.omevv_utils.omevv_info_utils import OMEVVInfo
160
161 PARTIAL_HOST_WARN_MSG = "Unable to fetch the firmware compliance report of few of the host(s) - {0}"
162 PARTIAL_CLUSTER_WARN_MSG = "Unable to fetch the firmware compliance report of few of the cluster(s) - {0}"
163 CLUSTER_NOT_VALID_MSG = "Unable to complete the operation because the {cluster_name} is not valid."
164 ALL_HOST_CLUSTER_NOT_VALID_MSG = "Unable to complete the operation because none of clusters and hosts are valid."
165 SUCCESS_FETCHED_MSG = "Successfully fetched the firmware compliance report."
166
167
168 class FirmwareComplianceInfo:
169
170 def __init__(self, module, rest_obj):
171 self.module = module
172 self.info = OMEVVInfo(rest_obj)
173
174 def extract_host_id(self, svctag, hostname, cluster_name):
175 host_id = []
176 uuid = self.module.params.get("vcenter_uuid")
177 managed_hosts, invalid_result = self.info.get_managed_host_details(uuid=uuid,
178 servicetags=svctag,
179 hostnames=hostname)
180 if invalid_result:
181 hostnames = invalid_result["hostnames"]
182 servicetags = invalid_result["servicetags"]
183 if hostnames or servicetags:
184 host_invalid_list = ', '.join(hostnames + servicetags)
185 self.module.warn(PARTIAL_HOST_WARN_MSG.format(host_invalid_list))
186
187 for each_host in managed_hosts:
188 if each_host.get("clusterName") == cluster_name:
189 host_id.append(each_host.get('id'))
190 return host_id
191
192 def get_hostid_groupid_and_cluster_name(self):
193 flatten_data = {}
194 clusters = self.module.params.get("clusters")
195 uuid = self.module.params.get("vcenter_uuid")
196 invalid_cluster_name = []
197 if clusters:
198 for cluster in clusters:
199 cluster_name = cluster.get("cluster_name")
200 svctag = cluster.get("servicetags")
201 hostname = cluster.get("hosts")
202 group_id = self.info.get_group_id_of_cluster(uuid, cluster_name)
203 if group_id == -1:
204 invalid_cluster_name.append(cluster_name)
205 continue
206 host_id_list = self.extract_host_id(svctag, hostname, cluster_name)
207 for each_host_id in host_id_list:
208 if cluster_name not in flatten_data:
209 flatten_data[cluster_name] = {"hostId": [each_host_id],
210 "groupId": group_id}
211 else:
212 flatten_data[cluster_name]["hostId"].append(each_host_id)
213 return flatten_data, invalid_cluster_name
214
215 def execute(self):
216 output = None
217 uuid = self.module.params.get("vcenter_uuid")
218 clusters = self.module.params.get("clusters")
219 flattend_data, invalid_cluster_name = self.get_hostid_groupid_and_cluster_name()
220 if invalid_cluster_name:
221 inv_cls = ', '.join(invalid_cluster_name)
222 self.module.warn(PARTIAL_CLUSTER_WARN_MSG.format(inv_cls))
223 if not flattend_data:
224 if clusters is None:
225 output = self.get_all_cluster_drift_info(uuid)
226 else:
227 output = self.get_host_drift_info(uuid, flattend_data)
228 if output:
229 self.module.exit_json(msg=SUCCESS_FETCHED_MSG, firmware_compliance_info=output)
230 else:
231 self.module.exit_json(msg=ALL_HOST_CLUSTER_NOT_VALID_MSG, skipped=True)
232
233 def get_host_drift_info(self, uuid, flattend_data):
234 output = []
235 for each_cluster, each_value in flattend_data.items():
236 try:
237 drift_info = deepcopy(self.info.get_firmware_drift_info_for_multiple_host(uuid=uuid,
238 groupid=each_value["groupId"],
239 hostidlist=each_value["hostId"]))
240 drift_info[0]["cluster"] = each_cluster
241 output.append(drift_info[0])
242 except HTTPError as err:
243 error_info = json.load(err)
244 msg = error_info.get("message")
245 self.module.warn(msg)
246 return output
247
248 def get_all_cluster_drift_info(self, uuid):
249 output = []
250 all_cluster = self.info.get_cluster_info(uuid=uuid)
251 cluster_names = [each_cluster.get("name") for each_cluster in all_cluster]
252 group_id_cluster_name = [{"groupId": self.info.get_group_id_of_cluster(uuid, each_name),
253 "cluster_name": each_name} for each_name in cluster_names]
254 for each_group in group_id_cluster_name:
255 try:
256 drift_info = deepcopy(self.info.get_firmware_drift_info_for_single_cluster(uuid=uuid,
257 groupid=each_group["groupId"]))
258 drift_info["cluster"] = each_group["cluster_name"]
259 output.append(drift_info)
260 except HTTPError as err:
261 error_info = json.load(err)
262 msg = error_info.get("message")
263 self.module.warn(msg)
264 return output
265
266
267 def main():
268 argument_spec = {
269 "clusters": {"type": "list", "elements": "dict",
270 "options": {"cluster_name": {"type": "str", "required": True},
271 "servicetags": {"type": "list", "elements": "str"},
272 "hosts": {"type": "list", "elements": "str"}}}}
273
274 module = OMEVVAnsibleModule(
275 argument_spec=argument_spec,
276 supports_check_mode=True)
277 try:
278 with RestOMEVV(module.params) as rest_obj:
279 obj = FirmwareComplianceInfo(module, rest_obj)
280 obj.execute()
281 except HTTPError as err:
282 msg = json.load(err) if isinstance(err, dict) else err
283 module.exit_json(msg=str(msg), failed=True)
284 except URLError as err:
285 module.exit_json(msg=str(err), unreachable=True)
286 except (IOError, ValueError, TypeError, ConnectionError,
287 AttributeError, IndexError, KeyError, OSError) as err:
288 module.exit_json(msg=str(err), failed=True)
289
290
291 if __name__ == '__main__':
292 main()