-
Notifications
You must be signed in to change notification settings - Fork 0
/
vds.py
308 lines (240 loc) · 8.72 KB
/
vds.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#!/usr/bin/python
#
# (c) 2015, Joseph Callen <jcallen () csc.com>
# Portions Copyright (c) 2015 VMware, Inc. All rights reserved.
#
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
DOCUMENTATION = '''
module: vds
short_description: virtual distributed switch
description:
- create update delete virtual distributed switch.
options:
datacenter_name:
description:
- The name of the datacenter the cluster will be created in.
required: True
vds_name:
description:
- The name of the new vds
numUplinks:
description:
- The number of uplinks for the vds
numPorts:
description:
- The number of ports *
mtu:
description:
- The mtu for the vds. Upstream physical switch must match or more
discovery_protocol:
description:
-
discovery_operation:
description:
-
productVersion:
description:
-
state:
description:
- If the datacenter should be present or absent
choices: ['present', 'absent']
required: True
'''
EXAMPLES = '''
- name: Create VDS
vds:
hostname: '172.16.78.15'
username: '[email protected]'
password: 'VMware1!'
validate_certs: False
datacenter_name: "test-dc-01"
vds_name: "vds001"
numUplinks: 4
numPorts: 16
mtu: 9000
discovery_protocol: 'lldp'
discovery_operation: 'both'
productVersion: '6.0.0'
state: 'present'
'''
try:
from pyVmomi import vim, vmodl
from pyVim import connect
HAS_PYVMOMI = True
except ImportError:
HAS_PYVMOMI = False
def find_vcenter_object_by_name(content, vimtype, object_name):
vcenter_object = get_all_objs(content, [vimtype])
for k, v in vcenter_object.items():
if v == object_name:
return k
else:
return None
def find_vds_by_name(content, vds_name):
vdSwitches = get_all_objs(content, [vim.dvs.VmwareDistributedVirtualSwitch])
for vds in vdSwitches:
if vds_name == vds.name:
return vds
return None
def _create_vds_spec(si, update, module):
vds_name = module.params['vds_name']
mtu = module.params['mtu']
discovery_protocol = module.params['discovery_protocol']
discovery_operation = module.params['discovery_operation']
productVersion = module.params['productVersion']
numUplinks = module.params['numUplinks']
numPorts = module.params['numPorts']
uplink_port_names = []
for x in range(int(numUplinks)):
uplink_port_names.append("%s_Uplink_%d" % (vds_name, x + 1))
prod_info = vim.dvs.ProductSpec(
version = productVersion,
name = "DVS",
vendor = "VMware, Inc."
)
uplink = vim.DistributedVirtualSwitch.NameArrayUplinkPortPolicy(
uplinkPortName = uplink_port_names
)
linkConfig = vim.host.LinkDiscoveryProtocolConfig(
protocol = discovery_protocol,
operation = discovery_operation
)
configSpec = vim.dvs.VmwareDistributedVirtualSwitch.ConfigSpec(
name = vds_name,
numStandalonePorts = numPorts,
maxMtu = mtu,
uplinkPortPolicy = uplink,
linkDiscoveryProtocolConfig = linkConfig,
lacpApiVersion = "multipleLag",
)
if update:
vds = find_vds_by_name(si, vds_name)
configSpec.configVersion = vds.config.configVersion
return configSpec
spec = vim.DistributedVirtualSwitch.CreateSpec()
spec.configSpec = configSpec
spec.productInfo = prod_info
return spec
def _check_vds_config_spec(vds, module):
vds_name = module.params['vds_name']
mtu = module.params['mtu']
discovery_protocol = module.params['discovery_protocol']
discovery_operation = module.params['discovery_operation']
productVersion = module.params['productVersion']
numUplinks = module.params['numUplinks']
numPorts = module.params['numPorts']
current_spec = vds.config
check_vals = [
(vds_name == current_spec.name),
(mtu == current_spec.maxMtu),
(discovery_protocol == current_spec.linkDiscoveryProtocolConfig.protocol),
(discovery_operation == current_spec.linkDiscoveryProtocolConfig.operation),
(productVersion == current_spec.productInfo.version),
(numUplinks == len(current_spec.uplinkPortPolicy.uplinkPortName))
]
if False in check_vals:
return False
else:
return True
def state_update_vds(si, module):
vds_name = module.params['vds_name']
vds = vds = find_vds_by_name(si, vds_name)
config_spec = _create_vds_spec(si, True, module)
try:
reconfig_task = vds.ReconfigureDvs_Task(config_spec)
changed, result = wait_for_task(reconfig_task)
except Exception as e:
module.fail_json(msg="Failed reconfiguring vds: {}".format(e))
module.exit_json(changed=changed, result=str(result))
def state_exit_unchanged(si, module):
module.exit_json(changed=False, msg="EXIT UNCHANGED")
def state_destroy_vds(si, module):
vds_name = module.params['vds_name']
vds = find_vds_by_name(si, vds_name)
if vds is None:
module.exit_json(msg="Could not find vds: {}".format(vds_name))
try:
task = vds.Destroy_Task()
changed, result = wait_for_task(task)
except Exception as e:
module.fail_json(msg="Failed to destroy vds: {}".format(str(e)))
module.exit_json(changed=changed, result=result)
def state_create_vds(si, module):
datacenter = find_datacenter_by_name(si, module.params['datacenter_name'])
network_folder = datacenter.networkFolder
vds_create_spec = _create_vds_spec(si, False, module)
try:
if not module.check_mode:
task = network_folder.CreateDVS_Task(vds_create_spec)
changed, vds_created = wait_for_task(task)
module.exit_json(changed=changed, result=vds_created.name)
except Exception, e:
module.fail_json(msg=str(e))
def check_vds_state(si, module):
vds_name = module.params['vds_name']
try:
vds = find_vds_by_name(si, vds_name)
if vds is None:
return 'absent'
elif not _check_vds_config_spec(vds, module):
return 'update'
else:
return 'present'
except vmodl.RuntimeFault as runtime_fault:
module.fail_json(msg=runtime_fault.msg)
except vmodl.MethodFault as method_fault:
module.fail_json(msg=method_fault.msg)
def main():
argument_spec = vmware_argument_spec()
argument_spec.update(
dict(
datacenter_name=dict(type='str', required=True),
vds_name=dict(type='str', required=True),
numUplinks=dict(type='int', required=True),
numPorts=dict(type='int', required=True),
mtu=dict(type='int', required=True),
discovery_protocol=dict(required=True, choices=['cdp', 'lldp'], type='str'),
discovery_operation=dict(required=True, choices=['both', 'none', 'advertise', 'listen'], type='str'),
productVersion=dict(type='str', required=True, choices=['6.0.0', '5.5.0', '5.1.0', '5.0.0']),
state=dict(required=True, choices=['present', 'absent'], type='str')
)
)
module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True)
if not HAS_PYVMOMI:
module.fail_json(msg='pyvmomi is required for this module')
vds_states = {
'absent': {
'present': state_destroy_vds,
'absent': state_exit_unchanged,
},
'present': {
'present': state_exit_unchanged,
'update': state_update_vds,
'absent': state_create_vds,
}
}
si = connect_to_api(module)
datacenter = find_datacenter_by_name(si, module.params['datacenter_name'])
if datacenter is None:
module.fail_json(msg="Could not find datacenter: {}".format(module.params['datacenter_name']))
desired_state = module.params['state']
current_state = check_vds_state(si, module)
vds_states[desired_state][current_state](si, module)
from ansible.module_utils.basic import *
from ansible.module_utils.vmware import *
if __name__ == '__main__':
main()