| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2018, Christian Kotte <[email protected]> |
| 5 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 7 |
|
| 8 |
from __future__ import absolute_import, division, print_function |
| 9 |
__metaclass__ = type |
| 10 |
|
| 11 |
|
| 12 |
DOCUMENTATION = r''' |
| 13 |
--- |
| 14 |
module: vmware_dvswitch_lacp |
| 15 |
short_description: Manage LACP configuration on a Distributed Switch |
| 16 |
description: |
| 17 |
- This module can be used to configure Link Aggregation Control Protocol (LACP) support mode and Link Aggregation Groups (LAGs). |
| 18 |
author: |
| 19 |
- Christian Kotte (@ckotte) |
| 20 |
notes: |
| 21 |
- You need to run the task two times if you want to remove all LAGs and change the support mode to 'basic' |
| 22 |
options: |
| 23 |
switch: |
| 24 |
description: |
| 25 |
- The name of the Distributed Switch to manage. |
| 26 |
required: true |
| 27 |
aliases: ['dvswitch'] |
| 28 |
type: str |
| 29 |
support_mode: |
| 30 |
description: |
| 31 |
- The LACP support mode. |
| 32 |
- 'V(basic): One Link Aggregation Control Protocol group in the switch (singleLag).' |
| 33 |
- 'V(enhanced): Multiple Link Aggregation Control Protocol groups in the switch (multipleLag).' |
| 34 |
type: str |
| 35 |
default: 'basic' |
| 36 |
choices: ['basic', 'enhanced'] |
| 37 |
link_aggregation_groups: |
| 38 |
description: |
| 39 |
- Can only be used if O(support_mode=enhanced). |
| 40 |
suboptions: |
| 41 |
name: |
| 42 |
type: str |
| 43 |
description: Name of the LAG. |
| 44 |
uplink_number: |
| 45 |
type: int |
| 46 |
description: |
| 47 |
- Number of uplinks. |
| 48 |
- Can 1 to 30. |
| 49 |
mode: |
| 50 |
type: str |
| 51 |
description: |
| 52 |
- The negotiating state of the uplinks/ports. |
| 53 |
choices: [ active, passive ] |
| 54 |
load_balancing_mode: |
| 55 |
type: str |
| 56 |
description: |
| 57 |
- Load balancing algorithm. |
| 58 |
- Valid values are as follows |
| 59 |
- '- srcTcpUdpPort: Source TCP/UDP port number.' |
| 60 |
- '- srcDestIpTcpUdpPortVlan: Source and destination IP, source and destination TCP/UDP port number and VLAN.' |
| 61 |
- '- srcIpVlan: Source IP and VLAN.' |
| 62 |
- '- srcDestTcpUdpPort: Source and destination TCP/UDP port number.' |
| 63 |
- '- srcMac: Source MAC address.' |
| 64 |
- '- destIp: Destination IP.' |
| 65 |
- '- destMac: Destination MAC address.' |
| 66 |
- '- vlan: VLAN only.' |
| 67 |
- '- srcDestIp: Source and Destination IP.' |
| 68 |
- '- srcIpTcpUdpPortVlan: Source IP, TCP/UDP port number and VLAN.' |
| 69 |
- '- srcDestIpTcpUdpPort: Source and destination IP and TCP/UDP port number.' |
| 70 |
- '- srcDestMac: Source and destination MAC address.' |
| 71 |
- '- destIpTcpUdpPort: Destination IP and TCP/UDP port number.' |
| 72 |
- '- srcPortId: Source Virtual Port Id.' |
| 73 |
- '- srcIp: Source IP.' |
| 74 |
- '- srcIpTcpUdpPort: Source IP and TCP/UDP port number.' |
| 75 |
- '- destIpTcpUdpPortVlan: Destination IP, TCP/UDP port number and VLAN.' |
| 76 |
- '- destTcpUdpPort: Destination TCP/UDP port number.' |
| 77 |
- '- destIpVlan: Destination IP and VLAN.' |
| 78 |
- '- srcDestIpVlan: Source and destination IP and VLAN.' |
| 79 |
- Please see examples for more information. |
| 80 |
default: 'srcDestIpTcpUdpPortVlan' |
| 81 |
elements: dict |
| 82 |
type: list |
| 83 |
default: [] |
| 84 |
extends_documentation_fragment: |
| 85 |
- vmware.vmware.base_options |
| 86 |
|
| 87 |
''' |
| 88 |
|
| 89 |
EXAMPLES = r''' |
| 90 |
- name: Enable enhanced mode on a Distributed Switch |
| 91 |
community.vmware.vmware_dvswitch_lacp: |
| 92 |
hostname: '{{ inventory_hostname }}' |
| 93 |
username: '{{ vcenter_username }}' |
| 94 |
password: '{{ vcenter_password }}' |
| 95 |
switch: dvSwitch |
| 96 |
support_mode: enhanced |
| 97 |
validate_certs: "{{ validate_vcenter_certs }}" |
| 98 |
delegate_to: localhost |
| 99 |
loop_control: |
| 100 |
label: "{{ item.name }}" |
| 101 |
with_items: "{{ vcenter_distributed_switches }}" |
| 102 |
|
| 103 |
- name: Enable enhanced mode and create two LAGs on a Distributed Switch |
| 104 |
community.vmware.vmware_dvswitch_lacp: |
| 105 |
hostname: '{{ inventory_hostname }}' |
| 106 |
username: '{{ vcenter_username }}' |
| 107 |
password: '{{ vcenter_password }}' |
| 108 |
switch: dvSwitch |
| 109 |
support_mode: enhanced |
| 110 |
link_aggregation_groups: |
| 111 |
- name: lag1 |
| 112 |
uplink_number: 2 |
| 113 |
mode: active |
| 114 |
load_balancing_mode: srcDestIpTcpUdpPortVlan |
| 115 |
- name: lag2 |
| 116 |
uplink_number: 2 |
| 117 |
mode: passive |
| 118 |
load_balancing_mode: srcDestIp |
| 119 |
validate_certs: "{{ validate_vcenter_certs }}" |
| 120 |
delegate_to: localhost |
| 121 |
loop_control: |
| 122 |
label: "{{ item.name }}" |
| 123 |
with_items: "{{ vcenter_distributed_switches }}" |
| 124 |
''' |
| 125 |
|
| 126 |
RETURN = r''' |
| 127 |
result: |
| 128 |
description: information about performed operation |
| 129 |
returned: always |
| 130 |
type: str |
| 131 |
sample: { |
| 132 |
"changed": true, |
| 133 |
"dvswitch": "dvSwitch", |
| 134 |
"link_aggregation_groups": [ |
| 135 |
{"load_balancing_mode": "srcDestIpTcpUdpPortVlan", "mode": "active", "name": "lag1", "uplink_number": 2}, |
| 136 |
{"load_balancing_mode": "srcDestIp", "mode": "active", "name": "lag2", "uplink_number": 2} |
| 137 |
], |
| 138 |
"link_aggregation_groups_previous": [], |
| 139 |
"support_mode": "enhanced", |
| 140 |
"result": "lacp lags changed" |
| 141 |
} |
| 142 |
''' |
| 143 |
|
| 144 |
try: |
| 145 |
from pyVmomi import vim, vmodl |
| 146 |
except ImportError: |
| 147 |
pass |
| 148 |
|
| 149 |
from ansible.module_utils.basic import AnsibleModule |
| 150 |
from ansible.module_utils.common.text.converters import to_native |
| 151 |
from ansible_collections.community.vmware.plugins.module_utils.vmware import ( |
| 152 |
PyVmomi, TaskError, find_dvs_by_name, wait_for_task |
| 153 |
) |
| 154 |
from ansible_collections.vmware.vmware.plugins.module_utils.argument_spec import base_argument_spec |
| 155 |
|
| 156 |
|
| 157 |
class VMwareDvSwitchLacp(PyVmomi): |
| 158 |
"""Class to manage a LACP on a Distributed Virtual Switch""" |
| 159 |
|
| 160 |
def __init__(self, module): |
| 161 |
super(VMwareDvSwitchLacp, self).__init__(module) |
| 162 |
self.switch_name = self.module.params['switch'] |
| 163 |
self.support_mode = self.module.params['support_mode'] |
| 164 |
self.link_aggregation_groups = self.module.params['link_aggregation_groups'] |
| 165 |
if self.support_mode == 'basic' and ( |
| 166 |
self.link_aggregation_groups and not ( |
| 167 |
len(self.link_aggregation_groups) == 1 and self.link_aggregation_groups[0] == '')): |
| 168 |
self.module.fail_json( |
| 169 |
msg="LAGs can only be configured if 'support_mode' is set to 'enhanced'!" |
| 170 |
) |
| 171 |
self.dvs = find_dvs_by_name(self.content, self.switch_name) |
| 172 |
if self.dvs is None: |
| 173 |
self.module.fail_json(msg="Failed to find DVS %s" % self.switch_name) |
| 174 |
|
| 175 |
def ensure(self): |
| 176 |
"""Manage LACP configuration""" |
| 177 |
changed = changed_support_mode = changed_lags = False |
| 178 |
results = dict(changed=changed) |
| 179 |
results['dvswitch'] = self.switch_name |
| 180 |
changed_list = [] |
| 181 |
message = '' |
| 182 |
|
| 183 |
spec = vim.dvs.VmwareDistributedVirtualSwitch.ConfigSpec() |
| 184 |
spec.configVersion = self.dvs.config.configVersion |
| 185 |
|
| 186 |
# Check support mode |
| 187 |
results['support_mode'] = self.support_mode |
| 188 |
lacp_support_mode = self.get_lacp_support_mode(self.support_mode) |
| 189 |
if self.dvs.config.lacpApiVersion != lacp_support_mode: |
| 190 |
changed = changed_support_mode = True |
| 191 |
changed_list.append("support mode") |
| 192 |
results['support_mode_previous'] = self.get_lacp_support_mode(self.dvs.config.lacpApiVersion) |
| 193 |
spec.lacpApiVersion = lacp_support_mode |
| 194 |
|
| 195 |
# Check LAGs |
| 196 |
results["link_aggregation_groups"] = self.link_aggregation_groups |
| 197 |
if self.link_aggregation_groups and not ( |
| 198 |
len(self.link_aggregation_groups) == 1 |
| 199 |
and self.link_aggregation_groups[0] == "" |
| 200 |
): |
| 201 |
if self.dvs.config.lacpGroupConfig: |
| 202 |
lacp_lag_list = [] |
| 203 |
# Check if desired LAGs are configured |
| 204 |
for lag in self.link_aggregation_groups: |
| 205 |
( |
| 206 |
lag_name, |
| 207 |
lag_mode, |
| 208 |
lag_uplink_number, |
| 209 |
lag_load_balancing_mode, |
| 210 |
) = self.get_lacp_lag_options(lag) |
| 211 |
lag_found = False |
| 212 |
for lacp_group in self.dvs.config.lacpGroupConfig: |
| 213 |
if lacp_group.name == lag_name: |
| 214 |
lag_found = True |
| 215 |
if ( |
| 216 |
lag_mode != lacp_group.mode |
| 217 |
or lag_uplink_number != lacp_group.uplinkNum |
| 218 |
or lag_load_balancing_mode |
| 219 |
!= lacp_group.loadbalanceAlgorithm |
| 220 |
): |
| 221 |
changed = changed_lags = True |
| 222 |
lacp_lag_list.append( |
| 223 |
self.create_lacp_group_spec( |
| 224 |
"edit", |
| 225 |
lacp_group.key, |
| 226 |
lag_name, |
| 227 |
lag_uplink_number, |
| 228 |
lag_mode, |
| 229 |
lag_load_balancing_mode, |
| 230 |
) |
| 231 |
) |
| 232 |
break |
| 233 |
if lag_found is False: |
| 234 |
changed = changed_lags = True |
| 235 |
lacp_lag_list.append( |
| 236 |
self.create_lacp_group_spec( |
| 237 |
'add', None, lag_name, lag_uplink_number, lag_mode, lag_load_balancing_mode |
| 238 |
) |
| 239 |
) |
| 240 |
# Check if LAGs need to be removed |
| 241 |
for lacp_group in self.dvs.config.lacpGroupConfig: |
| 242 |
lag_found = False |
| 243 |
for lag in self.link_aggregation_groups: |
| 244 |
result = self.get_lacp_lag_options(lag) |
| 245 |
if lacp_group.name == result[0]: |
| 246 |
lag_found = True |
| 247 |
break |
| 248 |
if lag_found is False: |
| 249 |
changed = changed_lags = True |
| 250 |
lacp_lag_list.append( |
| 251 |
self.create_lacp_group_spec('remove', lacp_group.key, lacp_group.name, None, None, None) |
| 252 |
) |
| 253 |
else: |
| 254 |
changed = changed_lags = True |
| 255 |
lacp_lag_list = [] |
| 256 |
for lag in self.link_aggregation_groups: |
| 257 |
lag_name, lag_mode, lag_uplink_number, lag_load_balancing_mode = self.get_lacp_lag_options(lag) |
| 258 |
lacp_lag_list.append( |
| 259 |
self.create_lacp_group_spec( |
| 260 |
'add', None, lag_name, lag_uplink_number, lag_mode, lag_load_balancing_mode |
| 261 |
) |
| 262 |
) |
| 263 |
else: |
| 264 |
if self.dvs.config.lacpGroupConfig: |
| 265 |
changed = changed_lags = True |
| 266 |
lacp_lag_list = [] |
| 267 |
for lacp_group in self.dvs.config.lacpGroupConfig: |
| 268 |
lacp_lag_list.append( |
| 269 |
self.create_lacp_group_spec('remove', lacp_group.key, lacp_group.name, None, None, None) |
| 270 |
) |
| 271 |
if changed_lags: |
| 272 |
changed_list.append("link aggregation groups") |
| 273 |
current_lags_list = [] |
| 274 |
for lacp_group in self.dvs.config.lacpGroupConfig: |
| 275 |
temp_lag = dict() |
| 276 |
temp_lag['name'] = lacp_group.name |
| 277 |
temp_lag['uplink_number'] = lacp_group.uplinkNum |
| 278 |
temp_lag['mode'] = lacp_group.mode |
| 279 |
temp_lag['load_balancing_mode'] = lacp_group.loadbalanceAlgorithm |
| 280 |
current_lags_list.append(temp_lag) |
| 281 |
results['link_aggregation_groups_previous'] = current_lags_list |
| 282 |
|
| 283 |
if changed: |
| 284 |
if self.module.check_mode: |
| 285 |
changed_suffix = ' would be changed' |
| 286 |
else: |
| 287 |
changed_suffix = ' changed' |
| 288 |
if len(changed_list) > 2: |
| 289 |
message = ', '.join(changed_list[:-1]) + ', and ' + str(changed_list[-1]) |
| 290 |
elif len(changed_list) == 2: |
| 291 |
message = ' and '.join(changed_list) |
| 292 |
elif len(changed_list) == 1: |
| 293 |
message = changed_list[0] |
| 294 |
message += changed_suffix |
| 295 |
if not self.module.check_mode: |
| 296 |
if changed_support_mode and self.support_mode == 'basic' and changed_lags: |
| 297 |
self.update_lacp_group_config(self.dvs, lacp_lag_list) |
| 298 |
# NOTE: You need to run the task again to change the support mode to 'basic' as well |
| 299 |
# No matter how long you sleep, you will always get the following error in vCenter: |
| 300 |
# 'Cannot complete operation due to concurrent modification by another operation.' |
| 301 |
# self.update_dvs_config(self.dvs, spec) |
| 302 |
else: |
| 303 |
if changed_support_mode: |
| 304 |
self.update_dvs_config(self.dvs, spec) |
| 305 |
if changed_lags: |
| 306 |
self.update_lacp_group_config(self.dvs, lacp_lag_list) |
| 307 |
else: |
| 308 |
message = "LACP already configured properly" |
| 309 |
results['changed'] = changed |
| 310 |
results['result'] = message |
| 311 |
|
| 312 |
self.module.exit_json(**results) |
| 313 |
|
| 314 |
@staticmethod |
| 315 |
def get_lacp_support_mode(mode): |
| 316 |
"""Get LACP support mode""" |
| 317 |
return_mode = None |
| 318 |
if mode == 'basic': |
| 319 |
return_mode = 'singleLag' |
| 320 |
elif mode == 'enhanced': |
| 321 |
return_mode = 'multipleLag' |
| 322 |
elif mode == 'singleLag': |
| 323 |
return_mode = 'basic' |
| 324 |
elif mode == 'multipleLag': |
| 325 |
return_mode = 'enhanced' |
| 326 |
return return_mode |
| 327 |
|
| 328 |
def get_lacp_lag_options(self, lag): |
| 329 |
"""Get and check LACP LAG options""" |
| 330 |
lag_name = lag.get('name', None) |
| 331 |
if lag_name is None: |
| 332 |
self.module.fail_json(msg="Please specify name in lag options as it's a required parameter") |
| 333 |
lag_mode = lag.get('mode', None) |
| 334 |
if lag_mode is None: |
| 335 |
self.module.fail_json(msg="Please specify mode in lag options as it's a required parameter") |
| 336 |
lag_uplink_number = lag.get('uplink_number', None) |
| 337 |
if lag_uplink_number is None: |
| 338 |
self.module.fail_json(msg="Please specify uplink_number in lag options as it's a required parameter") |
| 339 |
try: |
| 340 |
lag_uplink_number = int(lag_uplink_number) |
| 341 |
except ValueError: |
| 342 |
self.module.fail_json(msg="Failed to parse uplink_number in lag options") |
| 343 |
|
| 344 |
if lag_uplink_number > 30: |
| 345 |
self.module.fail_json(msg="More than 30 uplinks are not supported in a single LAG!") |
| 346 |
lag_load_balancing_mode = lag.get('load_balancing_mode', None) |
| 347 |
supported_lb_modes = ['srcTcpUdpPort', 'srcDestIpTcpUdpPortVlan', 'srcIpVlan', 'srcDestTcpUdpPort', |
| 348 |
'srcMac', 'destIp', 'destMac', 'vlan', 'srcDestIp', 'srcIpTcpUdpPortVlan', |
| 349 |
'srcDestIpTcpUdpPort', 'srcDestMac', 'destIpTcpUdpPort', 'srcPortId', 'srcIp', |
| 350 |
'srcIpTcpUdpPort', 'destIpTcpUdpPortVlan', 'destTcpUdpPort', 'destIpVlan', 'srcDestIpVlan'] |
| 351 |
if lag_load_balancing_mode is None: |
| 352 |
self.module.fail_json(msg="Please specify load_balancing_mode in lag options as it's a required parameter") |
| 353 |
elif lag_load_balancing_mode not in supported_lb_modes: |
| 354 |
self.module.fail_json(msg="The specified load balancing mode '%s' isn't supported!" % lag_load_balancing_mode) |
| 355 |
return lag_name, lag_mode, lag_uplink_number, lag_load_balancing_mode |
| 356 |
|
| 357 |
@staticmethod |
| 358 |
def create_lacp_group_spec(operation, key, name, uplink_number, mode, load_balancing_mode): |
| 359 |
""" |
| 360 |
Create LACP group spec |
| 361 |
operation: add, edit, or remove |
| 362 |
Returns: LACP group spec |
| 363 |
""" |
| 364 |
lacp_spec = vim.dvs.VmwareDistributedVirtualSwitch.LacpGroupSpec() |
| 365 |
lacp_spec.operation = operation |
| 366 |
lacp_spec.lacpGroupConfig = vim.dvs.VmwareDistributedVirtualSwitch.LacpGroupConfig() |
| 367 |
lacp_spec.lacpGroupConfig.name = name |
| 368 |
if operation in ('edit', 'remove'): |
| 369 |
lacp_spec.lacpGroupConfig.key = key |
| 370 |
if not operation == 'remove': |
| 371 |
lacp_spec.lacpGroupConfig.uplinkNum = uplink_number |
| 372 |
lacp_spec.lacpGroupConfig.mode = mode |
| 373 |
lacp_spec.lacpGroupConfig.loadbalanceAlgorithm = load_balancing_mode |
| 374 |
# greyed out in vSphere Client!? |
| 375 |
# lacp_spec.vlan = vim.dvs.VmwareDistributedVirtualSwitch.LagVlanConfig() |
| 376 |
# lacp_spec.vlan.vlanId = [vim.NumericRange(...)] |
| 377 |
# lacp_spec.ipfix = vim.dvs.VmwareDistributedVirtualSwitch.LagIpfixConfig() |
| 378 |
# lacp_spec.ipfix.ipfixEnabled = True/False |
| 379 |
return lacp_spec |
| 380 |
|
| 381 |
def update_dvs_config(self, switch_object, spec): |
| 382 |
"""Update DVS config""" |
| 383 |
try: |
| 384 |
task = switch_object.ReconfigureDvs_Task(spec) |
| 385 |
result = wait_for_task(task) |
| 386 |
except TaskError as invalid_argument: |
| 387 |
self.module.fail_json( |
| 388 |
msg="Failed to update DVS : %s" % to_native(invalid_argument) |
| 389 |
) |
| 390 |
return result |
| 391 |
|
| 392 |
def update_lacp_group_config(self, switch_object, lacp_group_spec): |
| 393 |
"""Update LACP group config""" |
| 394 |
try: |
| 395 |
task = switch_object.UpdateDVSLacpGroupConfig_Task(lacpGroupSpec=lacp_group_spec) |
| 396 |
result = wait_for_task(task) |
| 397 |
except vim.fault.DvsFault as dvs_fault: |
| 398 |
self.module.fail_json(msg="Update failed due to DVS fault : %s" % to_native(dvs_fault)) |
| 399 |
except vmodl.fault.NotSupported as not_supported: |
| 400 |
self.module.fail_json( |
| 401 |
msg="Multiple Link Aggregation Control Protocol groups not supported on the switch : %s" % |
| 402 |
to_native(not_supported) |
| 403 |
) |
| 404 |
except TaskError as invalid_argument: |
| 405 |
self.module.fail_json( |
| 406 |
msg="Failed to update Link Aggregation Group : %s" % to_native(invalid_argument) |
| 407 |
) |
| 408 |
return result |
| 409 |
|
| 410 |
|
| 411 |
def main(): |
| 412 |
"""Main""" |
| 413 |
argument_spec = base_argument_spec() |
| 414 |
argument_spec.update( |
| 415 |
dict( |
| 416 |
switch=dict(required=True, aliases=['dvswitch']), |
| 417 |
support_mode=dict(default='basic', choices=['basic', 'enhanced']), |
| 418 |
link_aggregation_groups=dict(default=[], type='list', elements='dict'), |
| 419 |
) |
| 420 |
) |
| 421 |
|
| 422 |
module = AnsibleModule( |
| 423 |
argument_spec=argument_spec, |
| 424 |
supports_check_mode=True, |
| 425 |
) |
| 426 |
|
| 427 |
vmware_dvswitch_lacp = VMwareDvSwitchLacp(module) |
| 428 |
vmware_dvswitch_lacp.ensure() |
| 429 |
|
| 430 |
|
| 431 |
if __name__ == '__main__': |
| 432 |
main() |