Debian Codegraph @

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

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2015, Joseph Callen <jcallen () csc.com>
5 # Copyright: (c) 2018, Ansible Project
6 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
7 # SPDX-License-Identifier: GPL-3.0-or-later
8
9 from __future__ import absolute_import, division, print_function
10 __metaclass__ = type
11
12
13 DOCUMENTATION = r'''
14 ---
15 module: vmware_cluster_vsan
16 short_description: Manages virtual storage area network (vSAN) configuration on VMware vSphere clusters
17 description:
18 - Manages vSAN on VMware vSphere clusters.
19 author:
20 - Joseph Callen (@jcpowermac)
21 - Abhijeet Kasurde (@Akasurde)
22 - Mario Lenz (@mariolenz)
23 requirements:
24 - vSAN Management SDK, which needs to be downloaded from VMware and installed manually.
25 options:
26 cluster_name:
27 description:
28 - The name of the cluster to be managed.
29 type: str
30 required: true
31 datacenter:
32 description:
33 - The name of the datacenter.
34 type: str
35 required: true
36 aliases: [ datacenter_name ]
37 enable:
38 description:
39 - Whether to enable vSAN.
40 type: bool
41 default: true
42 vsan_auto_claim_storage:
43 description:
44 - Whether the VSAN service is configured to automatically claim local storage
45 on VSAN-enabled hosts in the cluster.
46 type: bool
47 default: false
48 advanced_options:
49 description:
50 - Advanced VSAN Options.
51 suboptions:
52 automatic_rebalance:
53 description:
54 - If enabled, vSAN automatically rebalances (moves the data among disks) when a capacity disk fullness hits proactive rebalance threshold.
55 type: bool
56 disable_site_read_locality:
57 description:
58 - For vSAN stretched clusters, reads to vSAN objects occur on the site the VM resides on.
59 - Setting to V(true) will force reads across all mirrors.
60 type: bool
61 large_cluster_support:
62 description:
63 - Allow > 32 VSAN hosts per cluster; if this is changed on an existing vSAN cluster, all hosts are required to reboot to apply this change.
64 type: bool
65 object_repair_timer:
66 description:
67 - Delay time in minutes for VSAN to wait for the absent component to come back before starting to repair it.
68 type: int
69 thin_swap:
70 description:
71 - When V(true), swap objects would not reserve 100% space of their size on vSAN datastore.
72 type: bool
73 type: dict
74 extends_documentation_fragment:
75 - vmware.vmware.base_options
76
77 '''
78
79 EXAMPLES = r'''
80 - name: Enable vSAN
81 community.vmware.vmware_cluster_vsan:
82 hostname: '{{ vcenter_hostname }}'
83 username: '{{ vcenter_username }}'
84 password: '{{ vcenter_password }}'
85 datacenter_name: datacenter
86 cluster_name: cluster
87 enable: true
88 delegate_to: localhost
89
90 - name: Enable vSAN and automatic rebalancing
91 community.vmware.vmware_cluster_vsan:
92 hostname: '{{ vcenter_hostname }}'
93 username: '{{ vcenter_username }}'
94 password: '{{ vcenter_password }}'
95 datacenter_name: datacenter
96 cluster_name: cluster
97 enable: true
98 advanced_options:
99 automatic_rebalance: true
100 delegate_to: localhost
101
102 - name: Enable vSAN and claim storage automatically
103 community.vmware.vmware_cluster_vsan:
104 hostname: "{{ vcenter_hostname }}"
105 username: "{{ vcenter_username }}"
106 password: "{{ vcenter_password }}"
107 datacenter_name: DC0
108 cluster_name: "{{ cluster_name }}"
109 enable: true
110 vsan_auto_claim_storage: true
111 delegate_to: localhost
112 '''
113
114 RETURN = r'''#
115 '''
116
117 import traceback
118
119 try:
120 from pyVmomi import vim, vmodl
121 except ImportError:
122 pass
123
124 VSANPYTHONSDK_IMP_ERR = None
125 try:
126 import vsanapiutils
127 HAS_VSANPYTHONSDK = True
128 except ImportError:
129 VSANPYTHONSDK_IMP_ERR = traceback.format_exc()
130 HAS_VSANPYTHONSDK = False
131
132 from ansible.module_utils.basic import AnsibleModule, missing_required_lib
133 from ansible_collections.community.vmware.plugins.module_utils.vmware import (
134 PyVmomi,
135 TaskError,
136 find_datacenter_by_name,
137 wait_for_task)
138 from ansible_collections.vmware.vmware.plugins.module_utils.argument_spec import base_argument_spec
139 from ansible.module_utils.common.text.converters import to_native
140
141
142 class VMwareCluster(PyVmomi):
143 def __init__(self, module):
144 super(VMwareCluster, self).__init__(module)
145 self.cluster_name = module.params['cluster_name']
146 self.datacenter_name = module.params['datacenter']
147 self.enable_vsan = module.params['enable']
148 self.datacenter = None
149 self.cluster = None
150 self.advanced_options = None
151
152 self.datacenter = find_datacenter_by_name(self.content, self.datacenter_name)
153 if self.datacenter is None:
154 self.module.fail_json(msg="Datacenter %s does not exist." % self.datacenter_name)
155
156 self.cluster = self.find_cluster_by_name(cluster_name=self.cluster_name, datacenter_name=self.datacenter)
157 if self.cluster is None:
158 self.module.fail_json(msg="Cluster %s does not exist." % self.cluster_name)
159
160 if module.params['advanced_options'] is not None:
161 self.advanced_options = module.params['advanced_options']
162
163 client_stub = self.si._GetStub()
164 ssl_context = client_stub.schemeArgs.get('context')
165 apiVersion = vsanapiutils.GetLatestVmodlVersion(module.params['hostname'])
166 vcMos = vsanapiutils.GetVsanVcMos(client_stub, context=ssl_context, version=apiVersion)
167 self.vsanClusterConfigSystem = vcMos['vsan-cluster-config-system']
168
169 def check_vsan_config_diff(self):
170 """
171 Check VSAN configuration diff
172 Returns: True if there is diff, else False
173
174 """
175 vsan_config = self.cluster.configurationEx.vsanConfigInfo
176
177 if vsan_config.enabled != self.enable_vsan or \
178 vsan_config.defaultConfig.autoClaimStorage != self.params.get('vsan_auto_claim_storage'):
179 return True
180
181 if self.advanced_options is not None:
182 vsan_config_info = self.vsanClusterConfigSystem.GetConfigInfoEx(self.cluster).extendedConfig
183 if self.advanced_options['automatic_rebalance'] is not None and \
184 self.advanced_options['automatic_rebalance'] != vsan_config_info.proactiveRebalanceInfo.enabled:
185 return True
186 if self.advanced_options['disable_site_read_locality'] is not None and \
187 self.advanced_options['disable_site_read_locality'] != vsan_config_info.disableSiteReadLocality:
188 return True
189 if self.advanced_options['large_cluster_support'] is not None and \
190 self.advanced_options['large_cluster_support'] != vsan_config_info.largeScaleClusterSupport:
191 return True
192 if self.advanced_options['object_repair_timer'] is not None and \
193 self.advanced_options['object_repair_timer'] != vsan_config_info.objectRepairTimer:
194 return True
195 if self.advanced_options['thin_swap'] is not None and \
196 self.advanced_options['thin_swap'] != vsan_config_info.enableCustomizedSwapObject:
197 return True
198
199 return False
200
201 def configure_vsan(self):
202 """
203 Manage VSAN configuration
204
205 """
206 changed, result = False, None
207
208 if self.check_vsan_config_diff():
209 if not self.module.check_mode:
210 vSanSpec = vim.vsan.ReconfigSpec(
211 modify=True,
212 )
213 vSanSpec.vsanClusterConfig = vim.vsan.cluster.ConfigInfo(
214 enabled=self.enable_vsan
215 )
216 vSanSpec.vsanClusterConfig.defaultConfig = vim.vsan.cluster.ConfigInfo.HostDefaultInfo(
217 autoClaimStorage=self.params.get('vsan_auto_claim_storage')
218 )
219 if self.advanced_options is not None:
220 vSanSpec.extendedConfig = vim.vsan.VsanExtendedConfig()
221 if self.advanced_options['automatic_rebalance'] is not None:
222 vSanSpec.extendedConfig.proactiveRebalanceInfo = vim.vsan.ProactiveRebalanceInfo(
223 enabled=self.advanced_options['automatic_rebalance']
224 )
225 if self.advanced_options['disable_site_read_locality'] is not None:
226 vSanSpec.extendedConfig.disableSiteReadLocality = self.advanced_options['disable_site_read_locality']
227 if self.advanced_options['large_cluster_support'] is not None:
228 vSanSpec.extendedConfig.largeScaleClusterSupport = self.advanced_options['large_cluster_support']
229 if self.advanced_options['object_repair_timer'] is not None:
230 vSanSpec.extendedConfig.objectRepairTimer = self.advanced_options['object_repair_timer']
231 if self.advanced_options['thin_swap'] is not None:
232 vSanSpec.extendedConfig.enableCustomizedSwapObject = self.advanced_options['thin_swap']
233 try:
234 task = self.vsanClusterConfigSystem.VsanClusterReconfig(self.cluster, vSanSpec)
235 changed, result = wait_for_task(vim.Task(task._moId, self.si._stub))
236 except vmodl.RuntimeFault as runtime_fault:
237 self.module.fail_json(msg=to_native(runtime_fault.msg))
238 except vmodl.MethodFault as method_fault:
239 self.module.fail_json(msg=to_native(method_fault.msg))
240 except TaskError as task_e:
241 self.module.fail_json(msg=to_native(task_e))
242 except Exception as generic_exc:
243 self.module.fail_json(msg="Failed to update cluster"
244 " due to generic exception %s" % to_native(generic_exc))
245 else:
246 changed = True
247
248 self.module.exit_json(changed=changed, result=result)
249
250
251 def main():
252 argument_spec = base_argument_spec()
253 argument_spec.update(dict(
254 cluster_name=dict(type='str', required=True),
255 datacenter=dict(type='str', required=True, aliases=['datacenter_name']),
256 # VSAN
257 enable=dict(type='bool', default=True),
258 vsan_auto_claim_storage=dict(type='bool', default=False),
259 advanced_options=dict(type='dict', options=dict(
260 automatic_rebalance=dict(type='bool', required=False),
261 disable_site_read_locality=dict(type='bool', required=False),
262 large_cluster_support=dict(type='bool', required=False),
263 object_repair_timer=dict(type='int', required=False),
264 thin_swap=dict(type='bool', required=False),
265 )),
266 ))
267
268 module = AnsibleModule(
269 argument_spec=argument_spec,
270 supports_check_mode=True,
271 )
272
273 if not HAS_VSANPYTHONSDK:
274 module.fail_json(msg=missing_required_lib('vSAN Management SDK for Python'), exception=VSANPYTHONSDK_IMP_ERR)
275
276 vmware_cluster_vsan = VMwareCluster(module)
277 vmware_cluster_vsan.configure_vsan()
278
279
280 if __name__ == '__main__':
281 main()