Debian Codegraph @

ansible_collections/community/vmware/plugins/modules/vmware_vsan_health_info.py

raw
1 #!/usr/bin/python
2
3 # Copyright: (c) 2019, OVH SAS
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 absolute_import, division, print_function
8 __metaclass__ = type
9
10
11 DOCUMENTATION = r'''
12 ---
13 module: vmware_vsan_health_info
14 short_description: Gather information about a VMware vSAN cluster's health
15 description:
16 - "Gather information about a VMware vSAN cluster's health."
17 options:
18 datacenter:
19 description:
20 - Name of the Datacenter.
21 required: false
22 type: str
23 aliases: [ 'datacenter_name' ]
24 cluster_name:
25 description:
26 - Name of the vSAN cluster.
27 required: true
28 type: str
29 fetch_from_cache:
30 description:
31 - V(true) to return the result from cache directly instead of running the full health check.
32 required: false
33 default: false
34 type: bool
35 requirements:
36 - VMware vSAN Python's SDK
37 extends_documentation_fragment:
38 - vmware.vmware.base_options
39 author:
40 - Erwan Quelin (@equelin)
41 '''
42
43 EXAMPLES = r'''
44 - name: Gather health info from a vSAN's cluster
45 community.vmware.vmware_vsan_health_info:
46 hostname: "{{ vcenter_hostname }}"
47 username: "{{ vcenter_username }}"
48 password: "{{ vcenter_password }}"
49 cluster_name: 'vSAN01'
50 fetch_from_cache: false
51
52 - name: Gather health info from a vSAN's cluster with datacenter
53 community.vmware.vmware_vsan_health_info:
54 hostname: "{{ vcenter_hostname }}"
55 username: "{{ vcenter_username }}"
56 password: "{{ vcenter_password }}"
57 cluster_name: 'vSAN01'
58 datacenter: 'Datacenter_01'
59 fetch_from_cache: true
60 '''
61
62 RETURN = r'''
63 vsan_health_info:
64 description: vSAN cluster health info
65 returned: on success
66 type: dict
67 sample: {
68 "_vimtype": "vim.cluster.VsanClusterHealthSummary",
69 "burnInTest": null,
70 "clusterStatus": {
71 "_vimtype": "vim.cluster.VsanClusterHealthSystemStatusResult",
72 "goalState": "installed",
73 "status": "green",
74 "trackedHostsStatus": [
75 {
76 "_vimtype": "vim.host.VsanHostHealthSystemStatusResult",
77 "hostname": "esxi01.example.com",
78 "issues": [],
79 "status": "green"
80 },
81 {
82 "_vimtype": "vim.host.VsanHostHealthSystemStatusResult",
83 "hostname": "esxi04.example.com",
84 "issues": [],
85 "status": "green"
86 },
87 {
88 "_vimtype": "vim.host.VsanHostHealthSystemStatusResult",
89 "hostname": "esxi02.example.com",
90 "issues": [],
91 "status": "green"
92 },
93 {
94 "_vimtype": "vim.host.VsanHostHealthSystemStatusResult",
95 "hostname": "esxi03.example.com",
96 "issues": [],
97 "status": "green"
98 }
99 ],
100 "untrackedHosts": []
101 }
102 }
103 '''
104
105 import json
106 import traceback
107
108 try:
109 from pyVmomi import vmodl, VmomiJSONEncoder
110 except ImportError:
111 pass
112
113 VSANPYTHONSDK_IMP_ERR = None
114 try:
115 import vsanapiutils
116 HAS_VSANPYTHONSDK = True
117 except ImportError:
118 VSANPYTHONSDK_IMP_ERR = traceback.format_exc()
119 HAS_VSANPYTHONSDK = False
120
121 from ansible.module_utils.basic import AnsibleModule, missing_required_lib
122 from ansible_collections.community.vmware.plugins.module_utils.vmware import PyVmomi
123 from ansible_collections.vmware.vmware.plugins.module_utils.argument_spec import base_argument_spec
124
125
126 class VSANInfoManager(PyVmomi):
127 def __init__(self, module):
128 super(VSANInfoManager, self).__init__(module)
129 self.datacenter = None
130 self.cluster = None
131
132 def gather_info(self):
133 datacenter_name = self.module.params.get('datacenter')
134 if datacenter_name:
135 self.datacenter = self.find_datacenter_by_name(datacenter_name)
136 if self.datacenter is None:
137 self.module.fail_json(msg="Datacenter %s does not exist." % datacenter_name)
138
139 cluster_name = self.module.params.get('cluster_name')
140 self.cluster = self.find_cluster_by_name(cluster_name=cluster_name, datacenter_name=self.datacenter)
141 if self.cluster is None:
142 self.module.fail_json(msg="Cluster %s does not exist." % cluster_name)
143
144 fetch_from_cache = self.module.params.get('fetch_from_cache')
145
146 client_stub = self.si._GetStub()
147 ssl_context = client_stub.schemeArgs.get('context')
148
149 api_version = vsanapiutils.GetLatestVmodlVersion(self.module.params['hostname'])
150 vc_mos = vsanapiutils.GetVsanVcMos(client_stub, context=ssl_context, version=api_version)
151
152 vsan_cluster_health_system = vc_mos['vsan-cluster-health-system']
153
154 cluster_health = {}
155 try:
156 cluster_health = vsan_cluster_health_system.VsanQueryVcClusterHealthSummary(
157 cluster=self.cluster,
158 fetchFromCache=fetch_from_cache,
159 )
160 except vmodl.fault.NotFound as not_found:
161 self.module.fail_json(msg=not_found.msg)
162 except vmodl.fault.RuntimeFault as runtime_fault:
163 self.module.fail_json(msg=runtime_fault.msg)
164
165 health = json.dumps(cluster_health, cls=VmomiJSONEncoder.VmomiJSONEncoder, sort_keys=True, strip_dynamic=True)
166
167 self.module.exit_json(changed=False, vsan_health_info=json.loads(health))
168
169
170 def main():
171 argument_spec = base_argument_spec()
172 argument_spec.update(
173 datacenter=dict(required=False, type='str', aliases=['datacenter_name']),
174 cluster_name=dict(required=True, type='str'),
175 fetch_from_cache=dict(required=False, type='bool', default=False)
176 )
177
178 module = AnsibleModule(
179 argument_spec=argument_spec,
180 supports_check_mode=True
181 )
182
183 if not HAS_VSANPYTHONSDK:
184 module.fail_json(msg=missing_required_lib('vSAN Management SDK for Python'), exception=VSANPYTHONSDK_IMP_ERR)
185
186 vsan_info_manager = VSANInfoManager(module)
187 vsan_info_manager.gather_info()
188
189
190 if __name__ == '__main__':
191 main()