| 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 |
# Copyright: (c) 2018, Abhijeet Kasurde <[email protected]> |
| 7 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 8 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 9 |
|
| 10 |
from __future__ import absolute_import, division, print_function |
| 11 |
__metaclass__ = type |
| 12 |
|
| 13 |
|
| 14 |
DOCUMENTATION = r''' |
| 15 |
--- |
| 16 |
module: vmware_vswitch |
| 17 |
short_description: Manage a VMware Standard Switch to an ESXi host. |
| 18 |
description: |
| 19 |
- This module can be used to add, remove and update a VMware Standard Switch to an ESXi host. |
| 20 |
author: |
| 21 |
- Joseph Callen (@jcpowermac) |
| 22 |
- Russell Teague (@mtnbikenc) |
| 23 |
- Abhijeet Kasurde (@Akasurde) <[email protected]> |
| 24 |
options: |
| 25 |
switch: |
| 26 |
description: |
| 27 |
- vSwitch name to add. |
| 28 |
required: true |
| 29 |
aliases: [ switch_name ] |
| 30 |
type: str |
| 31 |
nics: |
| 32 |
description: |
| 33 |
- A list of vmnic names or vmnic name to attach to vSwitch. |
| 34 |
aliases: [ nic_name ] |
| 35 |
default: [] |
| 36 |
type: list |
| 37 |
elements: str |
| 38 |
number_of_ports: |
| 39 |
description: |
| 40 |
- Number of port to configure on vSwitch. |
| 41 |
default: 128 |
| 42 |
type: int |
| 43 |
mtu: |
| 44 |
description: |
| 45 |
- MTU to configure on vSwitch. |
| 46 |
default: 1500 |
| 47 |
type: int |
| 48 |
state: |
| 49 |
description: |
| 50 |
- Add or remove the switch. |
| 51 |
default: present |
| 52 |
choices: [ absent, present ] |
| 53 |
type: str |
| 54 |
esxi_hostname: |
| 55 |
description: |
| 56 |
- Manage the vSwitch using this ESXi host system. |
| 57 |
aliases: [ 'host' ] |
| 58 |
type: str |
| 59 |
security: |
| 60 |
description: |
| 61 |
- Network policy specifies layer 2 security settings for a |
| 62 |
portgroup such as promiscuous mode, where guest adapter listens |
| 63 |
to all the packets, MAC address changes and forged transmits. |
| 64 |
- Dict which configures the different security values for portgroup. |
| 65 |
suboptions: |
| 66 |
promiscuous_mode: |
| 67 |
type: bool |
| 68 |
description: Indicates whether promiscuous mode is allowed. |
| 69 |
forged_transmits: |
| 70 |
type: bool |
| 71 |
description: Indicates whether forged transmits are allowed. |
| 72 |
mac_changes: |
| 73 |
type: bool |
| 74 |
description: Indicates whether mac changes are allowed. |
| 75 |
required: false |
| 76 |
aliases: [ 'security_policy', 'network_policy' ] |
| 77 |
type: dict |
| 78 |
teaming: |
| 79 |
description: |
| 80 |
- Dictionary which configures the different teaming values for portgroup. |
| 81 |
suboptions: |
| 82 |
load_balancing: |
| 83 |
type: str |
| 84 |
description: |
| 85 |
- Network adapter teaming policy. |
| 86 |
choices: [ loadbalance_ip, loadbalance_srcmac, loadbalance_srcid, failover_explicit ] |
| 87 |
aliases: [ 'load_balance_policy' ] |
| 88 |
network_failure_detection: |
| 89 |
type: str |
| 90 |
description: Network failure detection. |
| 91 |
choices: [ link_status_only, beacon_probing ] |
| 92 |
notify_switches: |
| 93 |
type: bool |
| 94 |
description: Indicate whether or not to notify the physical switch if a link fails. |
| 95 |
failback: |
| 96 |
type: bool |
| 97 |
description: Indicate whether or not to use a failback when restoring links. |
| 98 |
active_adapters: |
| 99 |
type: list |
| 100 |
description: |
| 101 |
- List of active adapters used for load balancing. |
| 102 |
- All vmnics are used as active adapters if O(teaming.active_adapters) and O(teaming.standby_adapters) are not defined. |
| 103 |
elements: str |
| 104 |
standby_adapters: |
| 105 |
type: list |
| 106 |
description: |
| 107 |
- List of standby adapters used for failover. |
| 108 |
- All vmnics are used as active adapters if O(teaming.active_adapters) and O(teaming.standby_adapters) are not defined. |
| 109 |
elements: str |
| 110 |
required: false |
| 111 |
aliases: [ 'teaming_policy' ] |
| 112 |
type: dict |
| 113 |
traffic_shaping: |
| 114 |
description: |
| 115 |
- Dictionary which configures traffic shaping for the switch. |
| 116 |
suboptions: |
| 117 |
enabled: |
| 118 |
type: bool |
| 119 |
description: Status of Traffic Shaping Policy. |
| 120 |
average_bandwidth: |
| 121 |
type: int |
| 122 |
description: Average bandwidth (kbit/s). |
| 123 |
peak_bandwidth: |
| 124 |
type: int |
| 125 |
description: Peak bandwidth (kbit/s). |
| 126 |
burst_size: |
| 127 |
type: int |
| 128 |
description: Burst size (KB). |
| 129 |
required: false |
| 130 |
type: dict |
| 131 |
extends_documentation_fragment: |
| 132 |
- vmware.vmware.base_options |
| 133 |
|
| 134 |
''' |
| 135 |
|
| 136 |
EXAMPLES = r''' |
| 137 |
- name: Add a VMware vSwitch |
| 138 |
community.vmware.vmware_vswitch: |
| 139 |
hostname: '{{ esxi_hostname }}' |
| 140 |
username: '{{ esxi_username }}' |
| 141 |
password: '{{ esxi_password }}' |
| 142 |
switch: vswitch_name |
| 143 |
nics: vmnic_name |
| 144 |
mtu: 9000 |
| 145 |
delegate_to: localhost |
| 146 |
|
| 147 |
- name: Add a VMware vSwitch without any physical NIC attached |
| 148 |
community.vmware.vmware_vswitch: |
| 149 |
hostname: '{{ esxi_hostname }}' |
| 150 |
username: '{{ esxi_username }}' |
| 151 |
password: '{{ esxi_password }}' |
| 152 |
switch: vswitch_0001 |
| 153 |
mtu: 9000 |
| 154 |
delegate_to: localhost |
| 155 |
|
| 156 |
- name: Add a VMware vSwitch with multiple NICs |
| 157 |
community.vmware.vmware_vswitch: |
| 158 |
hostname: '{{ esxi_hostname }}' |
| 159 |
username: '{{ esxi_username }}' |
| 160 |
password: '{{ esxi_password }}' |
| 161 |
switch: vmware_vswitch_0004 |
| 162 |
nics: |
| 163 |
- vmnic1 |
| 164 |
- vmnic2 |
| 165 |
mtu: 9000 |
| 166 |
delegate_to: localhost |
| 167 |
|
| 168 |
- name: Add a VMware vSwitch to a specific host system |
| 169 |
community.vmware.vmware_vswitch: |
| 170 |
hostname: '{{ esxi_hostname }}' |
| 171 |
username: '{{ esxi_username }}' |
| 172 |
password: '{{ esxi_password }}' |
| 173 |
esxi_hostname: DC0_H0 |
| 174 |
switch_name: vswitch_001 |
| 175 |
nic_name: vmnic0 |
| 176 |
mtu: 9000 |
| 177 |
delegate_to: localhost |
| 178 |
|
| 179 |
- name: Add a VMware vSwitch to a specific host system with Promiscuous Mode Enabled |
| 180 |
community.vmware.vmware_vswitch: |
| 181 |
hostname: '{{ esxi_hostname }}' |
| 182 |
username: '{{ esxi_username }}' |
| 183 |
password: '{{ esxi_password }}' |
| 184 |
esxi_hostname: DC0_H0 |
| 185 |
switch_name: vswitch_001 |
| 186 |
nic_name: vmnic0 |
| 187 |
mtu: 9000 |
| 188 |
security: |
| 189 |
promiscuous_mode: true |
| 190 |
delegate_to: localhost |
| 191 |
|
| 192 |
- name: Add a VMware vSwitch to a specific host system with active/standby teaming |
| 193 |
community.vmware.vmware_vswitch: |
| 194 |
hostname: '{{ esxi_hostname }}' |
| 195 |
username: '{{ esxi_username }}' |
| 196 |
password: '{{ esxi_password }}' |
| 197 |
esxi_hostname: DC0_H0 |
| 198 |
switch_name: vswitch_001 |
| 199 |
nic_name: |
| 200 |
- vmnic0 |
| 201 |
- vmnic1 |
| 202 |
teaming: |
| 203 |
active_adapters: |
| 204 |
- vmnic0 |
| 205 |
standby_adapters: |
| 206 |
- vmnic1 |
| 207 |
delegate_to: localhost |
| 208 |
|
| 209 |
- name: Add a VMware vSwitch to a specific host system with traffic shaping |
| 210 |
community.vmware.vmware_vswitch: |
| 211 |
hostname: '{{ esxi_hostname }}' |
| 212 |
username: '{{ esxi_username }}' |
| 213 |
password: '{{ esxi_password }}' |
| 214 |
esxi_hostname: DC0_H0 |
| 215 |
switch_name: vswitch_001 |
| 216 |
nic_name: |
| 217 |
- vmnic0 |
| 218 |
- vmnic1 |
| 219 |
traffic_shaping: |
| 220 |
enabled: true |
| 221 |
average_bandwidth: 100000 |
| 222 |
peak_bandwidth: 100000 |
| 223 |
burst_size: 102400 |
| 224 |
delegate_to: localhost |
| 225 |
|
| 226 |
- name: Delete a VMware vSwitch in a specific host system |
| 227 |
community.vmware.vmware_vswitch: |
| 228 |
hostname: '{{ esxi_hostname }}' |
| 229 |
username: '{{ esxi_username }}' |
| 230 |
password: '{{ esxi_password }}' |
| 231 |
esxi_hostname: DC0_H0 |
| 232 |
switch_name: vswitch_001 |
| 233 |
state: absent |
| 234 |
delegate_to: localhost |
| 235 |
''' |
| 236 |
|
| 237 |
RETURN = r''' |
| 238 |
result: |
| 239 |
description: information about performed operation |
| 240 |
returned: always |
| 241 |
type: str |
| 242 |
sample: "vSwitch 'vSwitch_1002' is created successfully" |
| 243 |
''' |
| 244 |
|
| 245 |
try: |
| 246 |
from pyVmomi import vim, vmodl |
| 247 |
except ImportError: |
| 248 |
pass |
| 249 |
|
| 250 |
from ansible.module_utils.basic import AnsibleModule |
| 251 |
from ansible_collections.community.vmware.plugins.module_utils.vmware import PyVmomi |
| 252 |
from ansible_collections.vmware.vmware.plugins.module_utils.argument_spec import base_argument_spec |
| 253 |
from ansible.module_utils.common.text.converters import to_native |
| 254 |
|
| 255 |
|
| 256 |
class VMwareHostVirtualSwitch(PyVmomi): |
| 257 |
def __init__(self, module): |
| 258 |
super(VMwareHostVirtualSwitch, self).__init__(module) |
| 259 |
self.host_system = None |
| 260 |
self.vss = None |
| 261 |
self.switch = module.params['switch'] |
| 262 |
self.number_of_ports = module.params['number_of_ports'] |
| 263 |
self.nics = module.params['nics'] |
| 264 |
self.mtu = module.params['mtu'] |
| 265 |
self.state = module.params['state'] |
| 266 |
esxi_hostname = module.params['esxi_hostname'] |
| 267 |
|
| 268 |
hosts = self.get_all_host_objs(esxi_host_name=esxi_hostname) |
| 269 |
if hosts: |
| 270 |
self.host_system = hosts[0] |
| 271 |
else: |
| 272 |
self.module.fail_json(msg="Failed to get details of ESXi server." |
| 273 |
" Please specify esxi_hostname.") |
| 274 |
|
| 275 |
self.network_mgr = self.host_system.configManager.networkSystem |
| 276 |
if not self.network_mgr: |
| 277 |
self.module.fail_json(msg="Failed to find network manager for ESXi system.") |
| 278 |
|
| 279 |
if self.params.get('state') == 'present': |
| 280 |
# Gather information about all vSwitches and Physical NICs |
| 281 |
available_pnic = [pnic.device for pnic in self.network_mgr.networkInfo.pnic] |
| 282 |
self.available_vswitches = dict() |
| 283 |
for available_vswitch in self.network_mgr.networkInfo.vswitch: |
| 284 |
used_pnic = [] |
| 285 |
for pnic in available_vswitch.pnic: |
| 286 |
# vSwitch contains all PNICs as string in format of 'key-vim.host.PhysicalNic-vmnic0' |
| 287 |
m_pnic = pnic.split("-", 3)[-1] |
| 288 |
used_pnic.append(m_pnic) |
| 289 |
self.available_vswitches[available_vswitch.name] = dict(pnic=used_pnic, |
| 290 |
mtu=available_vswitch.mtu, |
| 291 |
num_ports=available_vswitch.spec.numPorts, |
| 292 |
) |
| 293 |
for desired_pnic in self.nics: |
| 294 |
if desired_pnic not in available_pnic: |
| 295 |
# Check if pnic does not exists |
| 296 |
self.module.fail_json(msg="Specified Physical NIC '%s' does not" |
| 297 |
" exists on given ESXi '%s'." % (desired_pnic, |
| 298 |
self.host_system.name)) |
| 299 |
for vswitch in self.available_vswitches: |
| 300 |
if desired_pnic in self.available_vswitches[vswitch]['pnic'] and vswitch != self.switch: |
| 301 |
# Check if pnic is already part of some other vSwitch |
| 302 |
self.module.fail_json(msg="Specified Physical NIC '%s' is already used" |
| 303 |
" by vSwitch '%s'." % (desired_pnic, vswitch)) |
| 304 |
|
| 305 |
def process_state(self): |
| 306 |
""" |
| 307 |
Manage internal state of vSwitch |
| 308 |
""" |
| 309 |
vswitch_states = { |
| 310 |
'absent': { |
| 311 |
'present': self.state_destroy_vswitch, |
| 312 |
'absent': self.state_exit_unchanged, |
| 313 |
}, |
| 314 |
'present': { |
| 315 |
'present': self.state_update_vswitch, |
| 316 |
'absent': self.state_create_vswitch, |
| 317 |
} |
| 318 |
} |
| 319 |
|
| 320 |
try: |
| 321 |
vswitch_states[self.state][self.check_vswitch_configuration()]() |
| 322 |
except vmodl.RuntimeFault as runtime_fault: |
| 323 |
self.module.fail_json(msg=to_native(runtime_fault.msg)) |
| 324 |
except vmodl.MethodFault as method_fault: |
| 325 |
self.module.fail_json(msg=to_native(method_fault.msg)) |
| 326 |
except Exception as e: |
| 327 |
self.module.fail_json(msg=to_native(e)) |
| 328 |
|
| 329 |
def state_create_vswitch(self): |
| 330 |
""" |
| 331 |
Create a virtual switch |
| 332 |
|
| 333 |
Source from |
| 334 |
https://github.com/rreubenur/pyvmomi-community-samples/blob/patch-1/samples/create_vswitch.py |
| 335 |
|
| 336 |
""" |
| 337 |
|
| 338 |
results = dict(changed=False, result="") |
| 339 |
vss_spec = vim.host.VirtualSwitch.Specification() |
| 340 |
vss_spec.numPorts = self.number_of_ports |
| 341 |
vss_spec.mtu = self.mtu |
| 342 |
if self.nics: |
| 343 |
vss_spec.bridge = vim.host.VirtualSwitch.BondBridge(nicDevice=self.nics) |
| 344 |
|
| 345 |
if self.module.check_mode: |
| 346 |
results['msg'] = "vSwitch '%s' would be created" % self.switch |
| 347 |
else: |
| 348 |
try: |
| 349 |
self.network_mgr.AddVirtualSwitch(vswitchName=self.switch, |
| 350 |
spec=vss_spec) |
| 351 |
|
| 352 |
changed = False |
| 353 |
spec = self.find_vswitch_by_name(self.host_system, self.switch).spec |
| 354 |
|
| 355 |
# Check Security Policy |
| 356 |
if self.update_security_policy(spec, results): |
| 357 |
changed = True |
| 358 |
|
| 359 |
# Check Teaming Policy |
| 360 |
if self.update_teaming_policy(spec, results): |
| 361 |
changed = True |
| 362 |
|
| 363 |
# Check Traffic Shaping Policy |
| 364 |
if self.update_traffic_shaping_policy(spec, results): |
| 365 |
changed = True |
| 366 |
|
| 367 |
if changed: |
| 368 |
self.network_mgr.UpdateVirtualSwitch(vswitchName=self.switch, |
| 369 |
spec=spec) |
| 370 |
|
| 371 |
results['result'] = "vSwitch '%s' is created successfully" % self.switch |
| 372 |
except vim.fault.AlreadyExists as already_exists: |
| 373 |
results['result'] = "vSwitch with name %s already exists: %s" % (self.switch, |
| 374 |
to_native(already_exists.msg)) |
| 375 |
except vim.fault.ResourceInUse as resource_used: |
| 376 |
self.module.fail_json(msg="Failed to add vSwitch '%s' as physical network adapter" |
| 377 |
" being bridged is already in use: %s" % (self.switch, |
| 378 |
to_native(resource_used.msg))) |
| 379 |
except vim.fault.HostConfigFault as host_config_fault: |
| 380 |
self.module.fail_json(msg="Failed to add vSwitch '%s' due to host" |
| 381 |
" configuration fault : %s" % (self.switch, |
| 382 |
to_native(host_config_fault.msg))) |
| 383 |
except vmodl.fault.InvalidArgument as invalid_argument: |
| 384 |
self.module.fail_json(msg="Failed to add vSwitch '%s', this can be due to either of following :" |
| 385 |
" 1. vSwitch Name exceeds the maximum allowed length," |
| 386 |
" 2. Number of ports specified falls out of valid range," |
| 387 |
" 3. Network policy is invalid," |
| 388 |
" 4. Beacon configuration is invalid : %s" % (self.switch, |
| 389 |
to_native(invalid_argument.msg))) |
| 390 |
except vmodl.fault.SystemError as system_error: |
| 391 |
self.module.fail_json(msg="Failed to add vSwitch '%s' due to : %s" % (self.switch, |
| 392 |
to_native(system_error.msg))) |
| 393 |
except Exception as generic_exc: |
| 394 |
self.module.fail_json(msg="Failed to add vSwitch '%s' due to" |
| 395 |
" generic exception : %s" % (self.switch, |
| 396 |
to_native(generic_exc))) |
| 397 |
|
| 398 |
results['changed'] = True |
| 399 |
|
| 400 |
self.module.exit_json(**results) |
| 401 |
|
| 402 |
def state_exit_unchanged(self): |
| 403 |
""" |
| 404 |
Declare exit without unchanged |
| 405 |
""" |
| 406 |
self.module.exit_json(changed=False) |
| 407 |
|
| 408 |
def state_destroy_vswitch(self): |
| 409 |
""" |
| 410 |
Remove vSwitch from configuration |
| 411 |
|
| 412 |
""" |
| 413 |
results = dict(changed=False, result="") |
| 414 |
|
| 415 |
if self.module.check_mode: |
| 416 |
results['msg'] = "vSwitch '%s' would be removed" % self.vss.name |
| 417 |
else: |
| 418 |
try: |
| 419 |
self.host_system.configManager.networkSystem.RemoveVirtualSwitch(self.vss.name) |
| 420 |
results['result'] = "vSwitch '%s' removed successfully." % self.vss.name |
| 421 |
except vim.fault.NotFound as vswitch_not_found: |
| 422 |
results['result'] = "vSwitch '%s' not available. %s" % (self.switch, |
| 423 |
to_native(vswitch_not_found.msg)) |
| 424 |
except vim.fault.ResourceInUse as vswitch_in_use: |
| 425 |
self.module.fail_json(msg="Failed to remove vSwitch '%s' as vSwitch" |
| 426 |
" is used by several virtual" |
| 427 |
" network adapters: %s" % (self.switch, |
| 428 |
to_native(vswitch_in_use.msg))) |
| 429 |
except vim.fault.HostConfigFault as host_config_fault: |
| 430 |
self.module.fail_json(msg="Failed to remove vSwitch '%s' due to host" |
| 431 |
" configuration fault : %s" % (self.switch, |
| 432 |
to_native(host_config_fault.msg))) |
| 433 |
except Exception as generic_exc: |
| 434 |
self.module.fail_json(msg="Failed to remove vSwitch '%s' due to generic" |
| 435 |
" exception : %s" % (self.switch, |
| 436 |
to_native(generic_exc))) |
| 437 |
|
| 438 |
results['changed'] = True |
| 439 |
|
| 440 |
self.module.exit_json(**results) |
| 441 |
|
| 442 |
def state_update_vswitch(self): |
| 443 |
""" |
| 444 |
Update vSwitch |
| 445 |
|
| 446 |
""" |
| 447 |
changed = False |
| 448 |
results = dict(changed=False, result="No change in vSwitch '%s'" % self.switch) |
| 449 |
spec = self.vss.spec |
| 450 |
|
| 451 |
# Check MTU |
| 452 |
if self.vss.mtu != self.mtu: |
| 453 |
spec.mtu = self.mtu |
| 454 |
changed = True |
| 455 |
|
| 456 |
# Check Number of Ports |
| 457 |
if spec.numPorts != self.number_of_ports: |
| 458 |
spec.numPorts = self.number_of_ports |
| 459 |
changed = True |
| 460 |
|
| 461 |
# Check nics |
| 462 |
nics_current = set(map(lambda n: n.rsplit('-', 1)[1], self.vss.pnic)) |
| 463 |
if nics_current != set(self.nics): |
| 464 |
if self.nics: |
| 465 |
spec.bridge = vim.host.VirtualSwitch.BondBridge(nicDevice=self.nics) |
| 466 |
else: |
| 467 |
spec.bridge = None |
| 468 |
changed = True |
| 469 |
|
| 470 |
# Update teaming if not configured specifigaly |
| 471 |
if not self.params['teaming']: |
| 472 |
nicOrder = spec.policy.nicTeaming.nicOrder |
| 473 |
# Remove missing nics from policy |
| 474 |
if nicOrder.activeNic != [i for i in nicOrder.activeNic if i in self.nics]: |
| 475 |
nicOrder.activeNic = [i for i in nicOrder.activeNic if i in self.nics] |
| 476 |
if nicOrder.standbyNic != [i for i in nicOrder.standbyNic if i in self.nics]: |
| 477 |
nicOrder.standbyNic = [i for i in nicOrder.standbyNic if i in self.nics] |
| 478 |
# Set new nics as active |
| 479 |
if set(self.nics) - nics_current: |
| 480 |
nicOrder.activeNic += set(self.nics) - nics_current |
| 481 |
|
| 482 |
# Check Security Policy |
| 483 |
if self.update_security_policy(spec, results): |
| 484 |
changed = True |
| 485 |
|
| 486 |
# Check Teaming Policy |
| 487 |
if self.update_teaming_policy(spec, results): |
| 488 |
changed = True |
| 489 |
|
| 490 |
# Check Traffic Shaping Policy |
| 491 |
if self.update_traffic_shaping_policy(spec, results): |
| 492 |
changed = True |
| 493 |
|
| 494 |
if changed: |
| 495 |
if self.module.check_mode: |
| 496 |
results['msg'] = "vSwitch '%s' would be updated" % self.switch |
| 497 |
else: |
| 498 |
try: |
| 499 |
self.network_mgr.UpdateVirtualSwitch(vswitchName=self.switch, |
| 500 |
spec=spec) |
| 501 |
results['result'] = "vSwitch '%s' is updated successfully" % self.switch |
| 502 |
except vim.fault.ResourceInUse as resource_used: |
| 503 |
self.module.fail_json(msg="Failed to update vSwitch '%s' as physical network adapter" |
| 504 |
" being bridged is already in use: %s" % (self.switch, |
| 505 |
to_native(resource_used.msg))) |
| 506 |
except vim.fault.NotFound as not_found: |
| 507 |
self.module.fail_json(msg="Failed to update vSwitch with name '%s'" |
| 508 |
" as it does not exists: %s" % (self.switch, |
| 509 |
to_native(not_found.msg))) |
| 510 |
|
| 511 |
except vim.fault.HostConfigFault as host_config_fault: |
| 512 |
self.module.fail_json(msg="Failed to update vSwitch '%s' due to host" |
| 513 |
" configuration fault : %s" % (self.switch, |
| 514 |
to_native(host_config_fault.msg))) |
| 515 |
except vmodl.fault.InvalidArgument as invalid_argument: |
| 516 |
self.module.fail_json(msg="Failed to update vSwitch '%s', this can be due to either of following :" |
| 517 |
" 1. vSwitch Name exceeds the maximum allowed length," |
| 518 |
" 2. Number of ports specified falls out of valid range," |
| 519 |
" 3. Network policy is invalid," |
| 520 |
" 4. Beacon configuration is invalid : %s" % (self.switch, |
| 521 |
to_native(invalid_argument.msg))) |
| 522 |
except vmodl.fault.SystemError as system_error: |
| 523 |
self.module.fail_json(msg="Failed to update vSwitch '%s' due to : %s" % (self.switch, |
| 524 |
to_native(system_error.msg))) |
| 525 |
except vmodl.fault.NotSupported as not_supported: |
| 526 |
self.module.fail_json(msg="Failed to update vSwitch '%s' as network adapter teaming policy" |
| 527 |
" is set but is not supported : %s" % (self.switch, |
| 528 |
to_native(not_supported.msg))) |
| 529 |
except Exception as generic_exc: |
| 530 |
self.module.fail_json(msg="Failed to update vSwitch '%s' due to" |
| 531 |
" generic exception : %s" % (self.switch, |
| 532 |
to_native(generic_exc))) |
| 533 |
|
| 534 |
results['changed'] = True |
| 535 |
|
| 536 |
self.module.exit_json(**results) |
| 537 |
|
| 538 |
def check_vswitch_configuration(self): |
| 539 |
""" |
| 540 |
Check if vSwitch exists |
| 541 |
Returns: 'present' if vSwitch exists or 'absent' if not |
| 542 |
|
| 543 |
""" |
| 544 |
self.vss = self.find_vswitch_by_name(self.host_system, self.switch) |
| 545 |
if self.vss is None: |
| 546 |
return 'absent' |
| 547 |
else: |
| 548 |
return 'present' |
| 549 |
|
| 550 |
@staticmethod |
| 551 |
def find_vswitch_by_name(host, vswitch_name): |
| 552 |
""" |
| 553 |
Find and return vSwitch managed object |
| 554 |
Args: |
| 555 |
host: Host system managed object |
| 556 |
vswitch_name: Name of vSwitch to find |
| 557 |
|
| 558 |
Returns: vSwitch managed object if found, else None |
| 559 |
|
| 560 |
""" |
| 561 |
for vss in host.configManager.networkSystem.networkInfo.vswitch: |
| 562 |
if vss.name == vswitch_name: |
| 563 |
return vss |
| 564 |
return None |
| 565 |
|
| 566 |
def update_security_policy(self, spec, results): |
| 567 |
""" |
| 568 |
Update the security policy according to the parameters |
| 569 |
Args: |
| 570 |
spec: The vSwitch spec |
| 571 |
results: The results dict |
| 572 |
|
| 573 |
Returns: True if changes have been made, else false |
| 574 |
""" |
| 575 |
if not self.params['security'] or not spec.policy.security: |
| 576 |
return False |
| 577 |
|
| 578 |
security_policy = spec.policy.security |
| 579 |
changed = False |
| 580 |
sec_promiscuous_mode = self.params['security'].get('promiscuous_mode') |
| 581 |
sec_forged_transmits = self.params['security'].get('forged_transmits') |
| 582 |
sec_mac_changes = self.params['security'].get('mac_changes') |
| 583 |
|
| 584 |
if sec_promiscuous_mode is not None: |
| 585 |
results['sec_promiscuous_mode'] = sec_promiscuous_mode |
| 586 |
if security_policy.allowPromiscuous is not sec_promiscuous_mode: |
| 587 |
results['sec_promiscuous_mode_previous'] = security_policy.allowPromiscuous |
| 588 |
security_policy.allowPromiscuous = sec_promiscuous_mode |
| 589 |
changed = True |
| 590 |
|
| 591 |
if sec_mac_changes is not None: |
| 592 |
results['sec_mac_changes'] = sec_mac_changes |
| 593 |
if security_policy.macChanges is not sec_mac_changes: |
| 594 |
results['sec_mac_changes_previous'] = security_policy.macChanges |
| 595 |
security_policy.macChanges = sec_mac_changes |
| 596 |
changed = True |
| 597 |
|
| 598 |
if sec_forged_transmits is not None: |
| 599 |
results['sec_forged_transmits'] = sec_forged_transmits |
| 600 |
if security_policy.forgedTransmits is not sec_forged_transmits: |
| 601 |
results['sec_forged_transmits_previous'] = security_policy.forgedTransmits |
| 602 |
security_policy.forgedTransmits = sec_forged_transmits |
| 603 |
changed = True |
| 604 |
|
| 605 |
return changed |
| 606 |
|
| 607 |
def update_teaming_policy(self, spec, results): |
| 608 |
""" |
| 609 |
Update the teaming policy according to the parameters |
| 610 |
Args: |
| 611 |
spec: The vSwitch spec |
| 612 |
results: The results dict |
| 613 |
|
| 614 |
Returns: True if changes have been made, else false |
| 615 |
""" |
| 616 |
if not self.params['teaming'] or not spec.policy.nicTeaming: |
| 617 |
return False |
| 618 |
|
| 619 |
teaming_policy = spec.policy.nicTeaming |
| 620 |
changed = False |
| 621 |
teaming_load_balancing = self.params['teaming'].get('load_balancing') |
| 622 |
teaming_failure_detection = self.params['teaming'].get('network_failure_detection') |
| 623 |
teaming_notify_switches = self.params['teaming'].get('notify_switches') |
| 624 |
teaming_failback = self.params['teaming'].get('failback') |
| 625 |
teaming_failover_order_active = self.params['teaming'].get('active_adapters') |
| 626 |
teaming_failover_order_standby = self.params['teaming'].get('standby_adapters') |
| 627 |
|
| 628 |
# Check teaming policy |
| 629 |
if teaming_load_balancing is not None: |
| 630 |
results['load_balancing'] = teaming_load_balancing |
| 631 |
if teaming_policy.policy != teaming_load_balancing: |
| 632 |
results['load_balancing_previous'] = teaming_policy.policy |
| 633 |
teaming_policy.policy = teaming_load_balancing |
| 634 |
changed = True |
| 635 |
|
| 636 |
# Check teaming notify switches |
| 637 |
if teaming_notify_switches is not None: |
| 638 |
results['notify_switches'] = teaming_notify_switches |
| 639 |
if teaming_policy.notifySwitches is not teaming_notify_switches: |
| 640 |
results['notify_switches_previous'] = teaming_policy.notifySwitches |
| 641 |
teaming_policy.notifySwitches = teaming_notify_switches |
| 642 |
changed = True |
| 643 |
|
| 644 |
# Check failback |
| 645 |
if teaming_failback is not None: |
| 646 |
results['failback'] = teaming_failback |
| 647 |
current_failback = not teaming_policy.rollingOrder |
| 648 |
if current_failback != teaming_failback: |
| 649 |
results['failback_previous'] = current_failback |
| 650 |
teaming_policy.rollingOrder = not teaming_failback |
| 651 |
changed = True |
| 652 |
|
| 653 |
# Check teaming failover order |
| 654 |
if teaming_failover_order_active is not None: |
| 655 |
results['failover_active'] = teaming_failover_order_active |
| 656 |
if teaming_policy.nicOrder.activeNic != teaming_failover_order_active: |
| 657 |
results['failover_active_previous'] = teaming_policy.nicOrder.activeNic |
| 658 |
teaming_policy.nicOrder.activeNic = teaming_failover_order_active |
| 659 |
changed = True |
| 660 |
if teaming_failover_order_standby is not None: |
| 661 |
results['failover_standby'] = teaming_failover_order_standby |
| 662 |
if teaming_policy.nicOrder.standbyNic != teaming_failover_order_standby: |
| 663 |
results['failover_standby_previous'] = teaming_policy.nicOrder.standbyNic |
| 664 |
teaming_policy.nicOrder.standbyNic = teaming_failover_order_standby |
| 665 |
changed = True |
| 666 |
|
| 667 |
# Check teaming failure detection |
| 668 |
if teaming_failure_detection is not None: |
| 669 |
results['failure_detection'] = teaming_failure_detection |
| 670 |
if teaming_failure_detection == "link_status_only": |
| 671 |
if teaming_policy.failureCriteria.checkBeacon is True: |
| 672 |
results['failure_detection_previous'] = "beacon_probing" |
| 673 |
teaming_policy.failureCriteria.checkBeacon = False |
| 674 |
changed = True |
| 675 |
elif teaming_failure_detection == "beacon_probing": |
| 676 |
if teaming_policy.failureCriteria.checkBeacon is False: |
| 677 |
results['failure_detection_previous'] = "link_status_only" |
| 678 |
teaming_policy.failureCriteria.checkBeacon = True |
| 679 |
changed = True |
| 680 |
|
| 681 |
return changed |
| 682 |
|
| 683 |
def update_traffic_shaping_policy(self, spec, results): |
| 684 |
""" |
| 685 |
Update the traffic shaping policy according to the parameters |
| 686 |
Args: |
| 687 |
spec: The vSwitch spec |
| 688 |
results: The results dict |
| 689 |
|
| 690 |
Returns: True if changes have been made, else false |
| 691 |
""" |
| 692 |
if not self.params['traffic_shaping'] or not spec.policy.nicTeaming: |
| 693 |
return False |
| 694 |
|
| 695 |
ts_policy = spec.policy.shapingPolicy |
| 696 |
changed = False |
| 697 |
ts_enabled = self.params['traffic_shaping'].get('enabled') |
| 698 |
|
| 699 |
# Check if traffic shaping needs to be disabled |
| 700 |
if not ts_enabled: |
| 701 |
if ts_policy.enabled: |
| 702 |
ts_policy.enabled = False |
| 703 |
changed = True |
| 704 |
return changed |
| 705 |
|
| 706 |
for value in ['average_bandwidth', 'peak_bandwidth', 'burst_size']: |
| 707 |
if not self.params['traffic_shaping'].get(value): |
| 708 |
self.module.fail_json(msg="traffic_shaping.%s is a required parameter if traffic_shaping is enabled." % value) |
| 709 |
ts_average_bandwidth = self.params['traffic_shaping'].get('average_bandwidth') * 1000 |
| 710 |
ts_peak_bandwidth = self.params['traffic_shaping'].get('peak_bandwidth') * 1000 |
| 711 |
ts_burst_size = self.params['traffic_shaping'].get('burst_size') * 1024 |
| 712 |
|
| 713 |
if not ts_policy.enabled: |
| 714 |
ts_policy.enabled = True |
| 715 |
changed = True |
| 716 |
|
| 717 |
if ts_policy.averageBandwidth != ts_average_bandwidth: |
| 718 |
results['traffic_shaping_avg_bandw'] = ts_average_bandwidth |
| 719 |
results['traffic_shaping_avg_bandw_previous'] = ts_policy.averageBandwidth |
| 720 |
ts_policy.averageBandwidth = ts_average_bandwidth |
| 721 |
changed = True |
| 722 |
|
| 723 |
if ts_policy.peakBandwidth != ts_peak_bandwidth: |
| 724 |
results['traffic_shaping_peak_bandw'] = ts_peak_bandwidth |
| 725 |
results['traffic_shaping_peak_bandw_previous'] = ts_policy.peakBandwidth |
| 726 |
ts_policy.peakBandwidth = ts_peak_bandwidth |
| 727 |
changed = True |
| 728 |
|
| 729 |
if ts_policy.burstSize != ts_burst_size: |
| 730 |
results['traffic_shaping_burst'] = ts_burst_size |
| 731 |
results['traffic_shaping_burst_previous'] = ts_policy.burstSize |
| 732 |
ts_policy.burstSize = ts_burst_size |
| 733 |
changed = True |
| 734 |
|
| 735 |
return changed |
| 736 |
|
| 737 |
|
| 738 |
def main(): |
| 739 |
argument_spec = base_argument_spec() |
| 740 |
argument_spec.update(dict( |
| 741 |
switch=dict(type='str', required=True, aliases=['switch_name']), |
| 742 |
nics=dict(type='list', aliases=['nic_name'], default=[], elements='str'), |
| 743 |
number_of_ports=dict(type='int', default=128), |
| 744 |
mtu=dict(type='int', default=1500), |
| 745 |
state=dict(type='str', default='present', choices=['absent', 'present']), |
| 746 |
esxi_hostname=dict(type='str', aliases=['host']), |
| 747 |
security=dict( |
| 748 |
type='dict', |
| 749 |
options=dict( |
| 750 |
promiscuous_mode=dict(type='bool'), |
| 751 |
forged_transmits=dict(type='bool'), |
| 752 |
mac_changes=dict(type='bool'), |
| 753 |
), |
| 754 |
aliases=['security_policy', 'network_policy'] |
| 755 |
), |
| 756 |
teaming=dict( |
| 757 |
type='dict', |
| 758 |
options=dict( |
| 759 |
load_balancing=dict( |
| 760 |
type='str', |
| 761 |
choices=[ |
| 762 |
'loadbalance_ip', |
| 763 |
'loadbalance_srcmac', |
| 764 |
'loadbalance_srcid', |
| 765 |
'failover_explicit', |
| 766 |
], |
| 767 |
aliases=['load_balance_policy'], |
| 768 |
), |
| 769 |
network_failure_detection=dict( |
| 770 |
type='str', |
| 771 |
choices=['link_status_only', 'beacon_probing'] |
| 772 |
), |
| 773 |
notify_switches=dict(type='bool'), |
| 774 |
failback=dict(type='bool'), |
| 775 |
active_adapters=dict(type='list', elements='str'), |
| 776 |
standby_adapters=dict(type='list', elements='str'), |
| 777 |
), |
| 778 |
aliases=['teaming_policy'] |
| 779 |
), |
| 780 |
traffic_shaping=dict( |
| 781 |
type='dict', |
| 782 |
options=dict( |
| 783 |
enabled=dict(type='bool'), |
| 784 |
average_bandwidth=dict(type='int'), |
| 785 |
peak_bandwidth=dict(type='int'), |
| 786 |
burst_size=dict(type='int'), |
| 787 |
), |
| 788 |
), |
| 789 |
)) |
| 790 |
|
| 791 |
module = AnsibleModule(argument_spec=argument_spec, |
| 792 |
supports_check_mode=True) |
| 793 |
|
| 794 |
host_virtual_switch = VMwareHostVirtualSwitch(module) |
| 795 |
host_virtual_switch.process_state() |
| 796 |
|
| 797 |
|
| 798 |
if __name__ == '__main__': |
| 799 |
main() |