| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2015, Joseph Callen <jcallen () csc.com> |
| 5 |
# Copyright: (c) 2017, 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_dvs_portgroup |
| 16 |
short_description: Create or remove a Distributed vSwitch portgroup. |
| 17 |
description: |
| 18 |
- Create or remove a Distributed vSwitch portgroup. |
| 19 |
author: |
| 20 |
- Joseph Callen (@jcpowermac) |
| 21 |
- Philippe Dellaert (@pdellaert) <[email protected]> |
| 22 |
options: |
| 23 |
portgroup_name: |
| 24 |
description: |
| 25 |
- The name of the portgroup that is to be created or deleted. |
| 26 |
required: true |
| 27 |
type: str |
| 28 |
portgroup_description: |
| 29 |
description: |
| 30 |
- Description of the portgroup |
| 31 |
required: false |
| 32 |
type: str |
| 33 |
aliases: ['description'] |
| 34 |
switch_name: |
| 35 |
description: |
| 36 |
- The name of the distributed vSwitch the port group should be created on. |
| 37 |
required: true |
| 38 |
type: str |
| 39 |
vlan_id: |
| 40 |
description: |
| 41 |
- The VLAN ID that should be configured with the portgroup, use 0 for no VLAN. |
| 42 |
- 'If O(vlan_trunk=true), this can be a combination of multiple ranges and numbers, example: 1-200, 205, 400-4094.' |
| 43 |
- The valid range is from 0 to 4094. Overlapping ranges are allowed. |
| 44 |
- 'If O(vlan_private=true), the corresponding private VLAN should already be configured in the distributed vSwitch.' |
| 45 |
required: true |
| 46 |
type: str |
| 47 |
num_ports: |
| 48 |
description: |
| 49 |
- The number of ports the portgroup should contain. |
| 50 |
type: int |
| 51 |
port_binding: |
| 52 |
description: |
| 53 |
- The type of port binding determines when ports in a port group are assigned to virtual machines. |
| 54 |
- See VMware KB 1022312 U(https://kb.vmware.com/s/article/1022312) for more details. |
| 55 |
required: true |
| 56 |
type: str |
| 57 |
choices: |
| 58 |
- 'static' |
| 59 |
- 'ephemeral' |
| 60 |
port_allocation: |
| 61 |
description: |
| 62 |
- Elastic port groups automatically increase or decrease the number of ports as needed. |
| 63 |
- Only valid if O(port_binding=static). |
| 64 |
- Will be V(elastic) if not specified and O(port_binding=static). |
| 65 |
- Will be V(fixed) if not specified and O(port_binding=ephemeral). |
| 66 |
type: str |
| 67 |
choices: |
| 68 |
- 'elastic' |
| 69 |
- 'fixed' |
| 70 |
state: |
| 71 |
description: |
| 72 |
- Determines if the portgroup should be present or not. |
| 73 |
required: false |
| 74 |
type: str |
| 75 |
choices: |
| 76 |
- 'present' |
| 77 |
- 'absent' |
| 78 |
default: 'present' |
| 79 |
vlan_trunk: |
| 80 |
description: |
| 81 |
- Indicates whether this is a VLAN trunk or not. |
| 82 |
- Mutually exclusive with O(vlan_private) parameter. |
| 83 |
required: false |
| 84 |
default: false |
| 85 |
type: bool |
| 86 |
vlan_private: |
| 87 |
description: |
| 88 |
- Indicates whether this is for a private VLAN or not. |
| 89 |
- Mutually exclusive with O(vlan_trunk) parameter. |
| 90 |
required: false |
| 91 |
default: false |
| 92 |
type: bool |
| 93 |
mac_learning: |
| 94 |
description: |
| 95 |
- This option is deprecated in favour of O(network_policy.mac_learning) and will be removed in 7.0.0. |
| 96 |
- Dictionary which configures MAC learning for portgroup. |
| 97 |
- Ignored if O(network_policy.inherited=true). |
| 98 |
- Ignored if O(network_policy.mac_learning) is defined. |
| 99 |
- Beware that setting this to enabled might disable the inheritance of promiscuous mode and so on! |
| 100 |
suboptions: |
| 101 |
allow_unicast_flooding: |
| 102 |
type: bool |
| 103 |
description: The flag to allow flooding of unlearned MAC for ingress traffic. |
| 104 |
required: false |
| 105 |
enabled: |
| 106 |
type: bool |
| 107 |
description: The flag to indicate if source MAC address learning is allowed. |
| 108 |
required: false |
| 109 |
limit: |
| 110 |
type: int |
| 111 |
description: The maximum number of MAC addresses that can be learned. |
| 112 |
required: false |
| 113 |
limit_policy: |
| 114 |
type: str |
| 115 |
description: The default switching policy after MAC limit is exceeded. |
| 116 |
required: false |
| 117 |
choices: |
| 118 |
- 'allow' |
| 119 |
- 'drop' |
| 120 |
type: dict |
| 121 |
network_policy: |
| 122 |
description: |
| 123 |
- Dictionary which configures the different security values for portgroup. |
| 124 |
suboptions: |
| 125 |
inherited: |
| 126 |
type: bool |
| 127 |
description: |
| 128 |
- Inherit the settings from the switch or not. |
| 129 |
- Beware that setting this to enabled will also inherit MAC learning configuration (O(mac_learning)) from the switch! |
| 130 |
required: true |
| 131 |
promiscuous: |
| 132 |
type: bool |
| 133 |
description: Indicates whether promiscuous mode is allowed. Ignored if O(network_policy.inherited=true). |
| 134 |
forged_transmits: |
| 135 |
type: bool |
| 136 |
description: Indicates whether forged transmits are allowed. Ignored if O(network_policy.inherited=true). |
| 137 |
mac_changes: |
| 138 |
type: bool |
| 139 |
description: Indicates whether mac changes are allowed. Ignored if O(network_policy.inherited=true). |
| 140 |
mac_learning: |
| 141 |
description: |
| 142 |
- Configure MAC learning for portgroup. |
| 143 |
- Ignored if O(network_policy.inherited=true). |
| 144 |
type: dict |
| 145 |
version_added: 5.6.0 |
| 146 |
suboptions: |
| 147 |
allow_unicast_flooding: |
| 148 |
type: bool |
| 149 |
description: The flag to allow flooding of unlearned MAC for ingress traffic. |
| 150 |
required: false |
| 151 |
enabled: |
| 152 |
type: bool |
| 153 |
description: The flag to indicate if source MAC address learning is allowed. |
| 154 |
required: false |
| 155 |
limit: |
| 156 |
type: int |
| 157 |
description: The maximum number of MAC addresses that can be learned. |
| 158 |
required: false |
| 159 |
limit_policy: |
| 160 |
type: str |
| 161 |
description: The default switching policy after MAC limit is exceeded. |
| 162 |
required: false |
| 163 |
choices: |
| 164 |
- 'allow' |
| 165 |
- 'drop' |
| 166 |
required: false |
| 167 |
type: dict |
| 168 |
teaming_policy: |
| 169 |
description: |
| 170 |
- Dictionary which configures the different teaming values for portgroup. |
| 171 |
suboptions: |
| 172 |
load_balance_policy: |
| 173 |
description: |
| 174 |
- Network adapter teaming policy. |
| 175 |
default: 'loadbalance_srcid' |
| 176 |
type: str |
| 177 |
choices: |
| 178 |
- loadbalance_ip |
| 179 |
- loadbalance_srcmac |
| 180 |
- loadbalance_srcid |
| 181 |
- loadbalance_loadbased |
| 182 |
- failover_explicit |
| 183 |
notify_switches: |
| 184 |
description: |
| 185 |
- Indicate whether or not to notify the physical switch if a link fails. |
| 186 |
default: true |
| 187 |
type: bool |
| 188 |
inbound_policy: |
| 189 |
description: |
| 190 |
- Indicate whether or not the teaming policy is applied to inbound frames as well. |
| 191 |
type: bool |
| 192 |
rolling_order: |
| 193 |
description: |
| 194 |
- Indicate whether or not to use a rolling policy when restoring links. |
| 195 |
default: false |
| 196 |
type: bool |
| 197 |
active_uplinks: |
| 198 |
description: |
| 199 |
- List of active uplinks used for load balancing. |
| 200 |
type: list |
| 201 |
elements: str |
| 202 |
standby_uplinks: |
| 203 |
description: |
| 204 |
- List of standby uplinks used for failover. |
| 205 |
type: list |
| 206 |
elements: str |
| 207 |
default: { |
| 208 |
'notify_switches': true, |
| 209 |
'load_balance_policy': 'loadbalance_srcid', |
| 210 |
'rolling_order': false |
| 211 |
} |
| 212 |
type: dict |
| 213 |
port_policy: |
| 214 |
description: |
| 215 |
- Dictionary which configures the advanced policy settings for the portgroup. |
| 216 |
suboptions: |
| 217 |
block_override: |
| 218 |
description: |
| 219 |
- Indicates if the block policy can be changed per port. |
| 220 |
default: true |
| 221 |
type: bool |
| 222 |
port_config_reset_at_disconnect: |
| 223 |
description: |
| 224 |
- Indicates if the configuration of a port is reset automatically after disconnect. |
| 225 |
default: true |
| 226 |
type: bool |
| 227 |
required: false |
| 228 |
ipfix_override: |
| 229 |
description: |
| 230 |
- Indicates if the ipfix policy can be changed per port. |
| 231 |
default: false |
| 232 |
type: bool |
| 233 |
live_port_move: |
| 234 |
description: |
| 235 |
- Indicates if a live port can be moved in or out of the portgroup. |
| 236 |
default: false |
| 237 |
type: bool |
| 238 |
network_rp_override: |
| 239 |
description: |
| 240 |
- Indicates if the network resource pool can be changed per port. |
| 241 |
default: false |
| 242 |
type: bool |
| 243 |
mac_management_override: |
| 244 |
description: |
| 245 |
- Indicates if the security policy can be changed per port. |
| 246 |
default: false |
| 247 |
type: bool |
| 248 |
shaping_override: |
| 249 |
description: |
| 250 |
- Indicates if the shaping policy can be changed per port. |
| 251 |
default: false |
| 252 |
type: bool |
| 253 |
traffic_filter_override: |
| 254 |
description: |
| 255 |
- Indicates if the traffic filter can be changed per port. |
| 256 |
default: false |
| 257 |
type: bool |
| 258 |
uplink_teaming_override: |
| 259 |
description: |
| 260 |
- Indicates if the uplink teaming policy can be changed per port. |
| 261 |
default: false |
| 262 |
type: bool |
| 263 |
vendor_config_override: |
| 264 |
description: |
| 265 |
- Indicates if the vendor config can be changed per port. |
| 266 |
type: bool |
| 267 |
default: false |
| 268 |
vlan_override: |
| 269 |
description: |
| 270 |
- Indicates if the vlan can be changed per port. |
| 271 |
type: bool |
| 272 |
default: false |
| 273 |
default: { |
| 274 |
'traffic_filter_override': false, |
| 275 |
'network_rp_override': false, |
| 276 |
'live_port_move': false, |
| 277 |
'mac_management_override': false, |
| 278 |
'vendor_config_override': false, |
| 279 |
'port_config_reset_at_disconnect': true, |
| 280 |
'uplink_teaming_override': false, |
| 281 |
'block_override': true, |
| 282 |
'shaping_override': false, |
| 283 |
'vlan_override': false, |
| 284 |
'ipfix_override': false |
| 285 |
} |
| 286 |
type: dict |
| 287 |
net_flow: |
| 288 |
description: |
| 289 |
- Indicate whether or not the virtual machine IP traffic that flows through a vds gets analyzed by sending reports to a NetFlow collector. |
| 290 |
required: false |
| 291 |
type: 'str' |
| 292 |
choices: |
| 293 |
- 'true' |
| 294 |
- 'on' |
| 295 |
- 'yes' |
| 296 |
- 'false' |
| 297 |
- 'off' |
| 298 |
- 'no' |
| 299 |
- 'inherited' |
| 300 |
in_traffic_shaping: |
| 301 |
description: |
| 302 |
- Dictionary which configures the ingress traffic shaping settings for the portgroup. |
| 303 |
suboptions: |
| 304 |
inherited: |
| 305 |
type: bool |
| 306 |
description: Inherit the settings from the switch or not. |
| 307 |
required: true |
| 308 |
enabled: |
| 309 |
type: bool |
| 310 |
description: |
| 311 |
- Indicates whether ingress traffic shaping is activated or not. |
| 312 |
- Ignored if O(in_traffic_shaping.inherited=true). |
| 313 |
average_bandwidth: |
| 314 |
type: int |
| 315 |
description: |
| 316 |
- Establishes the number of bits per second to allow across a port, averaged over time, that is, the allowed average load. |
| 317 |
- Ignored if O(in_traffic_shaping.inherited=true). |
| 318 |
burst_size: |
| 319 |
type: int |
| 320 |
description: |
| 321 |
- The maximum number of bits per second to allow across a port when it is sending/sending or receiving a burst of traffic. |
| 322 |
- Ignored if O(in_traffic_shaping.inherited=true). |
| 323 |
peak_bandwidth: |
| 324 |
type: int |
| 325 |
description: |
| 326 |
- The maximum number of bytes to allow in a burst. |
| 327 |
- Ignored if O(in_traffic_shaping.inherited=true). |
| 328 |
required: false |
| 329 |
type: dict |
| 330 |
out_traffic_shaping: |
| 331 |
description: |
| 332 |
- Dictionary which configures the egress traffic shaping settings for the portgroup. |
| 333 |
suboptions: |
| 334 |
inherited: |
| 335 |
type: bool |
| 336 |
description: |
| 337 |
- Inherit the settings from the switch or not. |
| 338 |
required: true |
| 339 |
enabled: |
| 340 |
type: bool |
| 341 |
description: |
| 342 |
- Indicates whether egress traffic shaping is activated or not. |
| 343 |
- Ignored if O(out_traffic_shaping.inherited=true). |
| 344 |
average_bandwidth: |
| 345 |
type: int |
| 346 |
description: |
| 347 |
- Establishes the number of bits per second to allow across a port, averaged over time, that is, the allowed average load. |
| 348 |
- Ignored if O(out_traffic_shaping.inherited=true). |
| 349 |
burst_size: |
| 350 |
type: int |
| 351 |
description: |
| 352 |
- The maximum number of bits per second to allow across a port when it is sending/sending or receiving a burst of traffic. |
| 353 |
- Ignored if O(out_traffic_shaping.inherited=true). |
| 354 |
peak_bandwidth: |
| 355 |
type: int |
| 356 |
description: |
| 357 |
- The maximum number of bytes to allow in a burst. |
| 358 |
- Ignored if O(out_traffic_shaping.inherited=true). |
| 359 |
required: false |
| 360 |
type: dict |
| 361 |
extends_documentation_fragment: |
| 362 |
- vmware.vmware.base_options |
| 363 |
|
| 364 |
""" |
| 365 |
|
| 366 |
EXAMPLES = r''' |
| 367 |
- name: Create vlan portgroup |
| 368 |
community.vmware.vmware_dvs_portgroup: |
| 369 |
hostname: '{{ vcenter_hostname }}' |
| 370 |
username: '{{ vcenter_username }}' |
| 371 |
password: '{{ vcenter_password }}' |
| 372 |
portgroup_name: vlan-123-portrgoup |
| 373 |
switch_name: dvSwitch |
| 374 |
vlan_id: 123 |
| 375 |
num_ports: 120 |
| 376 |
port_binding: static |
| 377 |
state: present |
| 378 |
delegate_to: localhost |
| 379 |
|
| 380 |
- name: Create vlan trunk portgroup |
| 381 |
community.vmware.vmware_dvs_portgroup: |
| 382 |
hostname: '{{ vcenter_hostname }}' |
| 383 |
username: '{{ vcenter_username }}' |
| 384 |
password: '{{ vcenter_password }}' |
| 385 |
portgroup_name: vlan-trunk-portrgoup |
| 386 |
portgroup_description: 'Trunk 1-1000, 1005, 1100-1200' |
| 387 |
switch_name: dvSwitch |
| 388 |
vlan_id: 1-1000, 1005, 1100-1200 |
| 389 |
vlan_trunk: true |
| 390 |
num_ports: 120 |
| 391 |
port_binding: static |
| 392 |
state: present |
| 393 |
delegate_to: localhost |
| 394 |
|
| 395 |
- name: Create private vlan portgroup |
| 396 |
vmware_dvs_portgroup: |
| 397 |
hostname: '{{ vcenter_hostname }}' |
| 398 |
username: '{{ vcenter_username }}' |
| 399 |
password: '{{ vcenter_password }}' |
| 400 |
portgroup_name: private-vlan-portrgoup |
| 401 |
switch_name: dvSwitch |
| 402 |
vlan_id: 1001 |
| 403 |
vlan_private: true |
| 404 |
num_ports: 120 |
| 405 |
port_binding: static |
| 406 |
state: present |
| 407 |
delegate_to: localhost |
| 408 |
|
| 409 |
- name: Create no-vlan portgroup |
| 410 |
community.vmware.vmware_dvs_portgroup: |
| 411 |
hostname: '{{ vcenter_hostname }}' |
| 412 |
username: '{{ vcenter_username }}' |
| 413 |
password: '{{ vcenter_password }}' |
| 414 |
portgroup_name: no-vlan-portrgoup |
| 415 |
switch_name: dvSwitch |
| 416 |
vlan_id: 0 |
| 417 |
num_ports: 120 |
| 418 |
port_binding: static |
| 419 |
state: present |
| 420 |
delegate_to: localhost |
| 421 |
|
| 422 |
- name: Create vlan portgroup with all security and port policies |
| 423 |
community.vmware.vmware_dvs_portgroup: |
| 424 |
hostname: '{{ vcenter_hostname }}' |
| 425 |
username: '{{ vcenter_username }}' |
| 426 |
password: '{{ vcenter_password }}' |
| 427 |
portgroup_name: vlan-123-portrgoup |
| 428 |
switch_name: dvSwitch |
| 429 |
vlan_id: 123 |
| 430 |
num_ports: 120 |
| 431 |
port_binding: static |
| 432 |
state: present |
| 433 |
network_policy: |
| 434 |
inherited: false |
| 435 |
promiscuous: true |
| 436 |
forged_transmits: true |
| 437 |
mac_changes: true |
| 438 |
port_policy: |
| 439 |
block_override: true |
| 440 |
ipfix_override: true |
| 441 |
live_port_move: true |
| 442 |
network_rp_override: true |
| 443 |
port_config_reset_at_disconnect: true |
| 444 |
mac_management_override: true |
| 445 |
shaping_override: true |
| 446 |
traffic_filter_override: true |
| 447 |
uplink_teaming_override: true |
| 448 |
vendor_config_override: true |
| 449 |
vlan_override: true |
| 450 |
delegate_to: localhost |
| 451 |
''' |
| 452 |
|
| 453 |
try: |
| 454 |
from pyVmomi import vim, vmodl |
| 455 |
except ImportError: |
| 456 |
pass |
| 457 |
|
| 458 |
from ansible.module_utils.basic import AnsibleModule |
| 459 |
from ansible_collections.community.vmware.plugins.module_utils.vmware import ( |
| 460 |
PyVmomi, |
| 461 |
find_dvs_by_name, |
| 462 |
find_dvspg_by_name, |
| 463 |
is_boolean, |
| 464 |
is_truthy, |
| 465 |
wait_for_task) |
| 466 |
from ansible_collections.vmware.vmware.plugins.module_utils.argument_spec import base_argument_spec |
| 467 |
|
| 468 |
|
| 469 |
class VMwareDvsPortgroup(PyVmomi): |
| 470 |
def __init__(self, module): |
| 471 |
super(VMwareDvsPortgroup, self).__init__(module) |
| 472 |
self.dvs_portgroup = None |
| 473 |
self.dv_switch = None |
| 474 |
|
| 475 |
self.networkPolicy = self.module.params['network_policy'] |
| 476 |
|
| 477 |
self.port_allocation = self.module.params['port_allocation'] |
| 478 |
if self.port_allocation is None: |
| 479 |
if self.module.params['port_binding'] == 'ephemeral': |
| 480 |
self.port_allocation = 'fixed' |
| 481 |
else: |
| 482 |
self.port_allocation = 'elastic' |
| 483 |
|
| 484 |
# Some sanity checks |
| 485 |
if self.port_allocation == 'elastic' and self.module.params['port_binding'] == 'ephemeral': |
| 486 |
self.module.fail_json( |
| 487 |
msg="'elastic' port allocation is not supported on an 'ephemeral' portgroup." |
| 488 |
) |
| 489 |
|
| 490 |
def create_vlan_list(self): |
| 491 |
vlan_id_list = [] |
| 492 |
for vlan_id_splitted in self.module.params['vlan_id'].split(','): |
| 493 |
vlans = vlan_id_splitted.split('-') |
| 494 |
if len(vlans) > 2: |
| 495 |
self.module.fail_json(msg="Invalid VLAN range %s." % vlan_id_splitted) |
| 496 |
if len(vlans) == 2: |
| 497 |
vlan_id_start = vlans[0].strip() |
| 498 |
vlan_id_end = vlans[1].strip() |
| 499 |
if not vlan_id_start.isdigit(): |
| 500 |
self.module.fail_json(msg="Invalid VLAN %s." % vlan_id_start) |
| 501 |
if not vlan_id_end.isdigit(): |
| 502 |
self.module.fail_json(msg="Invalid VLAN %s." % vlan_id_end) |
| 503 |
vlan_id_start = int(vlan_id_start) |
| 504 |
vlan_id_end = int(vlan_id_end) |
| 505 |
if vlan_id_start not in range(0, 4095) or vlan_id_end not in range(0, 4095): |
| 506 |
self.module.fail_json(msg="vlan_id range %s specified is incorrect. The valid vlan_id range is from 0 to 4094." % vlan_id_splitted) |
| 507 |
vlan_id_list.append((vlan_id_start, vlan_id_end)) |
| 508 |
else: |
| 509 |
vlan_id = vlans[0].strip() |
| 510 |
if not vlan_id.isdigit(): |
| 511 |
self.module.fail_json(msg="Invalid VLAN %s." % vlan_id) |
| 512 |
vlan_id = int(vlan_id) |
| 513 |
vlan_id_list.append((vlan_id, vlan_id)) |
| 514 |
|
| 515 |
vlan_id_list.sort() |
| 516 |
|
| 517 |
return vlan_id_list |
| 518 |
|
| 519 |
def build_config(self): |
| 520 |
config = vim.dvs.DistributedVirtualPortgroup.ConfigSpec() |
| 521 |
|
| 522 |
# Basic config |
| 523 |
config.name = self.module.params['portgroup_name'] |
| 524 |
config.description = self.module.params['portgroup_description'] |
| 525 |
|
| 526 |
if self.module.params['port_allocation'] != 'elastic' and self.module.params['port_binding'] != 'ephemeral': |
| 527 |
config.numPorts = self.module.params['num_ports'] |
| 528 |
|
| 529 |
# Default port config |
| 530 |
config.defaultPortConfig = vim.dvs.VmwareDistributedVirtualSwitch.VmwarePortConfigPolicy() |
| 531 |
if self.module.params['vlan_trunk']: |
| 532 |
config.defaultPortConfig.vlan = vim.dvs.VmwareDistributedVirtualSwitch.TrunkVlanSpec() |
| 533 |
config.defaultPortConfig.vlan.vlanId = list(map(lambda x: vim.NumericRange(start=x[0], end=x[1]), self.create_vlan_list())) |
| 534 |
elif self.module.params['vlan_private']: |
| 535 |
# Check that private VLAN exists in dvs |
| 536 |
if self.module.params['vlan_private']: |
| 537 |
pvlan_exists = self.check_dvs_pvlan() |
| 538 |
if not pvlan_exists: |
| 539 |
self.module.fail_json(msg="No private vlan with id %s in distributed vSwitch %s" |
| 540 |
% (self.module.params['vlan_id'], self.module.params['switch_name'])) |
| 541 |
|
| 542 |
config.defaultPortConfig.vlan = vim.dvs.VmwareDistributedVirtualSwitch.PvlanSpec() |
| 543 |
config.defaultPortConfig.vlan.pvlanId = int(self.module.params['vlan_id']) |
| 544 |
else: |
| 545 |
config.defaultPortConfig.vlan = vim.dvs.VmwareDistributedVirtualSwitch.VlanIdSpec() |
| 546 |
config.defaultPortConfig.vlan.vlanId = int(self.module.params['vlan_id']) |
| 547 |
|
| 548 |
config.defaultPortConfig.vlan.inherited = False |
| 549 |
|
| 550 |
if self.networkPolicy is not None: |
| 551 |
config.defaultPortConfig.macManagementPolicy = vim.dvs.VmwareDistributedVirtualSwitch.MacManagementPolicy() |
| 552 |
config.defaultPortConfig.macManagementPolicy.inherited = self.networkPolicy['inherited'] |
| 553 |
if not self.networkPolicy['inherited']: |
| 554 |
config.defaultPortConfig.macManagementPolicy.allowPromiscuous = self.networkPolicy['promiscuous'] |
| 555 |
config.defaultPortConfig.macManagementPolicy.forgedTransmits = self.networkPolicy['forged_transmits'] |
| 556 |
config.defaultPortConfig.macManagementPolicy.macChanges = self.networkPolicy['mac_changes'] |
| 557 |
macLearning = self.networkPolicy['mac_learning'] |
| 558 |
if macLearning: |
| 559 |
macLearningPolicy = vim.dvs.VmwareDistributedVirtualSwitch.MacLearningPolicy() |
| 560 |
macLearningPolicy.allowUnicastFlooding = macLearning['allow_unicast_flooding'] |
| 561 |
macLearningPolicy.enabled = macLearning['enabled'] |
| 562 |
macLearningPolicy.limit = macLearning['limit'] |
| 563 |
macLearningPolicy.limitPolicy = macLearning['limit_policy'] |
| 564 |
config.defaultPortConfig.macManagementPolicy.macLearningPolicy = macLearningPolicy |
| 565 |
|
| 566 |
if not self.networkPolicy or (not self.networkPolicy['inherited'] and not self.networkPolicy['mac_learning']): |
| 567 |
macLearning = self.module.params['mac_learning'] |
| 568 |
if macLearning: |
| 569 |
if config.defaultPortConfig.macManagementPolicy is None: |
| 570 |
config.defaultPortConfig.macManagementPolicy = vim.dvs.VmwareDistributedVirtualSwitch.MacManagementPolicy() |
| 571 |
macLearningPolicy = vim.dvs.VmwareDistributedVirtualSwitch.MacLearningPolicy() |
| 572 |
if macLearning['allow_unicast_flooding'] is not None: |
| 573 |
macLearningPolicy.allowUnicastFlooding = macLearning['allow_unicast_flooding'] |
| 574 |
if macLearning['enabled'] is not None: |
| 575 |
macLearningPolicy.enabled = macLearning['enabled'] |
| 576 |
if macLearning['limit'] is not None: |
| 577 |
macLearningPolicy.limit = macLearning['limit'] |
| 578 |
if macLearning['limit_policy']: |
| 579 |
macLearningPolicy.limitPolicy = macLearning['limit_policy'] |
| 580 |
config.defaultPortConfig.macManagementPolicy.macLearningPolicy = macLearningPolicy |
| 581 |
|
| 582 |
# Teaming Policy |
| 583 |
teamingPolicy = vim.dvs.VmwareDistributedVirtualSwitch.UplinkPortTeamingPolicy() |
| 584 |
teamingPolicy.policy = vim.StringPolicy(value=self.module.params['teaming_policy']['load_balance_policy']) |
| 585 |
if self.module.params['teaming_policy']['inbound_policy'] is not None: |
| 586 |
teamingPolicy.reversePolicy = vim.BoolPolicy(value=self.module.params['teaming_policy']['inbound_policy']) |
| 587 |
teamingPolicy.notifySwitches = vim.BoolPolicy(value=self.module.params['teaming_policy']['notify_switches']) |
| 588 |
teamingPolicy.rollingOrder = vim.BoolPolicy(value=self.module.params['teaming_policy']['rolling_order']) |
| 589 |
|
| 590 |
if self.module.params['teaming_policy']['active_uplinks'] or self.module.params['teaming_policy']['standby_uplinks']: |
| 591 |
teamingPolicy.uplinkPortOrder = vim.dvs.VmwareDistributedVirtualSwitch.UplinkPortOrderPolicy() |
| 592 |
if self.module.params['teaming_policy']['active_uplinks']: |
| 593 |
teamingPolicy.uplinkPortOrder.activeUplinkPort = self.module.params['teaming_policy']['active_uplinks'] |
| 594 |
if self.module.params['teaming_policy']['standby_uplinks']: |
| 595 |
teamingPolicy.uplinkPortOrder.standbyUplinkPort = self.module.params['teaming_policy']['standby_uplinks'] |
| 596 |
|
| 597 |
config.defaultPortConfig.uplinkTeamingPolicy = teamingPolicy |
| 598 |
|
| 599 |
# PG policy (advanced_policy) |
| 600 |
config.policy = vim.dvs.VmwareDistributedVirtualSwitch.VMwarePortgroupPolicy() |
| 601 |
config.policy.blockOverrideAllowed = self.module.params['port_policy']['block_override'] |
| 602 |
config.policy.ipfixOverrideAllowed = self.module.params['port_policy']['ipfix_override'] |
| 603 |
config.policy.livePortMovingAllowed = self.module.params['port_policy']['live_port_move'] |
| 604 |
config.policy.macManagementOverrideAllowed = self.module.params['port_policy']['mac_management_override'] |
| 605 |
config.policy.networkResourcePoolOverrideAllowed = self.module.params['port_policy']['network_rp_override'] |
| 606 |
config.policy.portConfigResetAtDisconnect = self.module.params['port_policy']['port_config_reset_at_disconnect'] |
| 607 |
config.policy.shapingOverrideAllowed = self.module.params['port_policy']['shaping_override'] |
| 608 |
config.policy.trafficFilterOverrideAllowed = self.module.params['port_policy']['traffic_filter_override'] |
| 609 |
config.policy.uplinkTeamingOverrideAllowed = self.module.params['port_policy']['uplink_teaming_override'] |
| 610 |
config.policy.vendorConfigOverrideAllowed = self.module.params['port_policy']['vendor_config_override'] |
| 611 |
config.policy.vlanOverrideAllowed = self.module.params['port_policy']['vlan_override'] |
| 612 |
|
| 613 |
# NetFlow |
| 614 |
net_flow = self.module.params['net_flow'] |
| 615 |
if net_flow is not None: |
| 616 |
config.defaultPortConfig.ipfixEnabled = vim.BoolPolicy() |
| 617 |
if is_boolean(net_flow): |
| 618 |
config.defaultPortConfig.ipfixEnabled.inherited = False |
| 619 |
config.defaultPortConfig.ipfixEnabled.value = is_truthy(net_flow) |
| 620 |
else: |
| 621 |
config.defaultPortConfig.ipfixEnabled.inherited = True |
| 622 |
|
| 623 |
# Ingress traffic shaping |
| 624 |
config.defaultPortConfig.inShapingPolicy = vim.dvs.DistributedVirtualPort.TrafficShapingPolicy() |
| 625 |
config.defaultPortConfig.inShapingPolicy.averageBandwidth = vim.LongPolicy() |
| 626 |
config.defaultPortConfig.inShapingPolicy.burstSize = vim.LongPolicy() |
| 627 |
config.defaultPortConfig.inShapingPolicy.peakBandwidth = vim.LongPolicy() |
| 628 |
config.defaultPortConfig.inShapingPolicy.enabled = vim.BoolPolicy() |
| 629 |
|
| 630 |
in_traffic_shaping = self.module.params['in_traffic_shaping'] |
| 631 |
if in_traffic_shaping is not None: |
| 632 |
if in_traffic_shaping['inherited'] is False: |
| 633 |
config.defaultPortConfig.inShapingPolicy.inherited = False |
| 634 |
|
| 635 |
# enabled |
| 636 |
config.defaultPortConfig.inShapingPolicy.enabled.inherited = False |
| 637 |
config.defaultPortConfig.inShapingPolicy.enabled.value = in_traffic_shaping['enabled'] |
| 638 |
|
| 639 |
# adverage bandwidth |
| 640 |
config.defaultPortConfig.inShapingPolicy.averageBandwidth.inherited = False |
| 641 |
config.defaultPortConfig.inShapingPolicy.averageBandwidth.value = in_traffic_shaping['average_bandwidth'] * 1000 |
| 642 |
|
| 643 |
# burst size |
| 644 |
config.defaultPortConfig.inShapingPolicy.burstSize.inherited = False |
| 645 |
config.defaultPortConfig.inShapingPolicy.burstSize.value = in_traffic_shaping['burst_size'] * 1024 |
| 646 |
|
| 647 |
# peak bandwidth |
| 648 |
config.defaultPortConfig.inShapingPolicy.peakBandwidth.inherited = False |
| 649 |
config.defaultPortConfig.inShapingPolicy.peakBandwidth.value = in_traffic_shaping['peak_bandwidth'] * 1000 |
| 650 |
else: |
| 651 |
config.defaultPortConfig.inShapingPolicy.inherited = True |
| 652 |
config.defaultPortConfig.inShapingPolicy.enabled.inherited = True |
| 653 |
config.defaultPortConfig.inShapingPolicy.averageBandwidth.inherited = True |
| 654 |
config.defaultPortConfig.inShapingPolicy.burstSize.inherited = True |
| 655 |
config.defaultPortConfig.inShapingPolicy.peakBandwidth.inherited = True |
| 656 |
|
| 657 |
# Egress traffic shaping |
| 658 |
config.defaultPortConfig.outShapingPolicy = vim.dvs.DistributedVirtualPort.TrafficShapingPolicy() |
| 659 |
config.defaultPortConfig.outShapingPolicy.averageBandwidth = vim.LongPolicy() |
| 660 |
config.defaultPortConfig.outShapingPolicy.burstSize = vim.LongPolicy() |
| 661 |
config.defaultPortConfig.outShapingPolicy.peakBandwidth = vim.LongPolicy() |
| 662 |
config.defaultPortConfig.outShapingPolicy.enabled = vim.BoolPolicy() |
| 663 |
|
| 664 |
out_traffic_shaping = self.module.params['out_traffic_shaping'] |
| 665 |
if out_traffic_shaping is not None: |
| 666 |
if out_traffic_shaping['inherited'] is False: |
| 667 |
config.defaultPortConfig.outShapingPolicy.inherited = False |
| 668 |
|
| 669 |
# enabled |
| 670 |
config.defaultPortConfig.outShapingPolicy.enabled.inherited = False |
| 671 |
config.defaultPortConfig.outShapingPolicy.enabled.value = out_traffic_shaping['enabled'] |
| 672 |
|
| 673 |
# adverage bandwidth |
| 674 |
config.defaultPortConfig.outShapingPolicy.averageBandwidth.inherited = False |
| 675 |
config.defaultPortConfig.outShapingPolicy.averageBandwidth.value = out_traffic_shaping['average_bandwidth'] * 1000 |
| 676 |
|
| 677 |
# burst size |
| 678 |
config.defaultPortConfig.outShapingPolicy.burstSize.inherited = False |
| 679 |
config.defaultPortConfig.outShapingPolicy.burstSize.value = out_traffic_shaping['burst_size'] * 1024 |
| 680 |
|
| 681 |
# peak bandwidth |
| 682 |
config.defaultPortConfig.outShapingPolicy.peakBandwidth.inherited = False |
| 683 |
config.defaultPortConfig.outShapingPolicy.peakBandwidth.value = out_traffic_shaping['peak_bandwidth'] * 1000 |
| 684 |
else: |
| 685 |
config.defaultPortConfig.outShapingPolicy.inherited = True |
| 686 |
config.defaultPortConfig.outShapingPolicy.enabled.inherited = True |
| 687 |
config.defaultPortConfig.outShapingPolicy.averageBandwidth.inherited = True |
| 688 |
config.defaultPortConfig.outShapingPolicy.burstSize.inherited = True |
| 689 |
config.defaultPortConfig.outShapingPolicy.peakBandwidth.inherited = True |
| 690 |
|
| 691 |
# PG Type |
| 692 |
if self.module.params['port_binding'] == 'ephemeral': |
| 693 |
config.type = 'ephemeral' |
| 694 |
else: |
| 695 |
config.type = 'earlyBinding' |
| 696 |
|
| 697 |
if self.port_allocation == 'elastic': |
| 698 |
config.autoExpand = True |
| 699 |
else: |
| 700 |
config.autoExpand = False |
| 701 |
|
| 702 |
return config |
| 703 |
|
| 704 |
def process_state(self): |
| 705 |
dvspg_states = { |
| 706 |
'absent': { |
| 707 |
'update': self.state_destroy_dvspg, |
| 708 |
'present': self.state_destroy_dvspg, |
| 709 |
'absent': self.state_exit_unchanged, |
| 710 |
}, |
| 711 |
'present': { |
| 712 |
'update': self.state_update_dvspg, |
| 713 |
'present': self.state_exit_unchanged, |
| 714 |
'absent': self.state_create_dvspg, |
| 715 |
} |
| 716 |
} |
| 717 |
try: |
| 718 |
dvspg_states[self.module.params['state']][self.check_dvspg_state()]() |
| 719 |
except vmodl.RuntimeFault as runtime_fault: |
| 720 |
self.module.fail_json(msg=runtime_fault.msg) |
| 721 |
except vmodl.MethodFault as method_fault: |
| 722 |
self.module.fail_json(msg=method_fault.msg) |
| 723 |
except Exception as e: |
| 724 |
self.module.fail_json(msg=str(e)) |
| 725 |
|
| 726 |
def update_port_group(self): |
| 727 |
config = self.build_config() |
| 728 |
config.configVersion = self.dvs_portgroup.config.configVersion |
| 729 |
task = self.dvs_portgroup.ReconfigureDVPortgroup_Task(config) |
| 730 |
changed, result = wait_for_task(task) |
| 731 |
return changed, result |
| 732 |
|
| 733 |
def create_port_group(self): |
| 734 |
config = self.build_config() |
| 735 |
task = self.dv_switch.AddDVPortgroup_Task([config]) |
| 736 |
changed, result = wait_for_task(task) |
| 737 |
return changed, result |
| 738 |
|
| 739 |
def state_destroy_dvspg(self): |
| 740 |
changed = True |
| 741 |
result = None |
| 742 |
|
| 743 |
if not self.module.check_mode: |
| 744 |
task = self.dvs_portgroup.Destroy_Task() |
| 745 |
changed, result = wait_for_task(task) |
| 746 |
self.module.exit_json(changed=changed, result=str(result)) |
| 747 |
|
| 748 |
def state_exit_unchanged(self): |
| 749 |
self.module.exit_json(changed=False) |
| 750 |
|
| 751 |
def state_update_dvspg(self): |
| 752 |
changed = True |
| 753 |
result = None |
| 754 |
|
| 755 |
if not self.module.check_mode: |
| 756 |
changed, result = self.update_port_group() |
| 757 |
self.module.exit_json(changed=changed, result=str(result)) |
| 758 |
|
| 759 |
def state_create_dvspg(self): |
| 760 |
changed = True |
| 761 |
result = None |
| 762 |
|
| 763 |
if not self.module.check_mode: |
| 764 |
changed, result = self.create_port_group() |
| 765 |
self.module.exit_json(changed=changed, result=str(result)) |
| 766 |
|
| 767 |
def check_dvs_pvlan(self): |
| 768 |
for pvlan in self.dv_switch.config.pvlanConfig: |
| 769 |
if pvlan.primaryVlanId == int(self.module.params['vlan_id']): |
| 770 |
return True |
| 771 |
if pvlan.secondaryVlanId == int(self.module.params['vlan_id']): |
| 772 |
return True |
| 773 |
return False |
| 774 |
|
| 775 |
def check_dvspg_state(self): |
| 776 |
self.dv_switch = find_dvs_by_name(self.content, self.module.params['switch_name']) |
| 777 |
|
| 778 |
if self.dv_switch is None: |
| 779 |
self.module.fail_json(msg="A distributed virtual switch with name %s does not exist" % self.module.params['switch_name']) |
| 780 |
self.dvs_portgroup = find_dvspg_by_name(self.dv_switch, self.module.params['portgroup_name']) |
| 781 |
|
| 782 |
if self.dvs_portgroup is None: |
| 783 |
return 'absent' |
| 784 |
|
| 785 |
# description property in DVPortgroupConfigInfo stored in config property |
| 786 |
if self.module.params['portgroup_description'] is not None and self.dvs_portgroup.config.description != self.module.params['portgroup_description']: |
| 787 |
return 'update' |
| 788 |
|
| 789 |
# Check config |
| 790 |
if self.module.params['port_allocation'] != 'elastic' and self.module.params['port_binding'] != 'ephemeral': |
| 791 |
if self.module.params['num_ports'] is not None and self.dvs_portgroup.config.numPorts != self.module.params['num_ports']: |
| 792 |
return 'update' |
| 793 |
|
| 794 |
# Default port config |
| 795 |
defaultPortConfig = self.dvs_portgroup.config.defaultPortConfig |
| 796 |
if self.module.params['vlan_trunk']: |
| 797 |
if not isinstance(defaultPortConfig.vlan, vim.dvs.VmwareDistributedVirtualSwitch.TrunkVlanSpec): |
| 798 |
return 'update' |
| 799 |
if list(map(lambda x: (x.start, x.end), defaultPortConfig.vlan.vlanId)) != self.create_vlan_list(): |
| 800 |
return 'update' |
| 801 |
elif self.module.params['vlan_private']: |
| 802 |
if not isinstance(defaultPortConfig.vlan, vim.dvs.VmwareDistributedVirtualSwitch.PvlanSpec): |
| 803 |
return 'update' |
| 804 |
if defaultPortConfig.vlan.pvlanId != int(self.module.params['vlan_id']): |
| 805 |
return 'update' |
| 806 |
else: |
| 807 |
if not isinstance(defaultPortConfig.vlan, vim.dvs.VmwareDistributedVirtualSwitch.VlanIdSpec): |
| 808 |
return 'update' |
| 809 |
if defaultPortConfig.vlan.vlanId != int(self.module.params['vlan_id']): |
| 810 |
return 'update' |
| 811 |
|
| 812 |
if self.networkPolicy is not None: |
| 813 |
if defaultPortConfig.macManagementPolicy.inherited != self.networkPolicy['inherited']: |
| 814 |
return 'update' |
| 815 |
if not self.networkPolicy['inherited']: |
| 816 |
if defaultPortConfig.macManagementPolicy.allowPromiscuous != self.networkPolicy['promiscuous'] or \ |
| 817 |
defaultPortConfig.macManagementPolicy.forgedTransmits != self.networkPolicy['forged_transmits'] or \ |
| 818 |
defaultPortConfig.macManagementPolicy.macChanges != self.networkPolicy['mac_changes']: |
| 819 |
return 'update' |
| 820 |
macLearning = self.networkPolicy['mac_learning'] |
| 821 |
if macLearning: |
| 822 |
macLearningPolicy = defaultPortConfig.macManagementPolicy.macLearningPolicy |
| 823 |
if macLearning['allow_unicast_flooding'] is not None and macLearningPolicy.allowUnicastFlooding != macLearning['allow_unicast_flooding']: |
| 824 |
return 'update' |
| 825 |
if macLearning['enabled'] is not None and macLearningPolicy.enabled != macLearning['enabled']: |
| 826 |
return 'update' |
| 827 |
if macLearning['limit'] is not None and macLearningPolicy.limit != macLearning['limit']: |
| 828 |
return 'update' |
| 829 |
if macLearning['limit_policy'] and macLearningPolicy.limitPolicy != macLearning['limit_policy']: |
| 830 |
return 'update' |
| 831 |
|
| 832 |
if not self.networkPolicy or (not self.networkPolicy['inherited'] and not self.networkPolicy['mac_learning']): |
| 833 |
macLearning = self.module.params['mac_learning'] |
| 834 |
if macLearning: |
| 835 |
macLearningPolicy = defaultPortConfig.macManagementPolicy.macLearningPolicy |
| 836 |
if macLearning['allow_unicast_flooding'] is not None and macLearningPolicy.allowUnicastFlooding != macLearning['allow_unicast_flooding']: |
| 837 |
return 'update' |
| 838 |
if macLearning['enabled'] is not None and macLearningPolicy.enabled != macLearning['enabled']: |
| 839 |
return 'update' |
| 840 |
if macLearning['limit'] is not None and macLearningPolicy.limit != macLearning['limit']: |
| 841 |
return 'update' |
| 842 |
if macLearning['limit_policy'] and macLearningPolicy.limitPolicy != macLearning['limit_policy']: |
| 843 |
return 'update' |
| 844 |
|
| 845 |
# Teaming Policy |
| 846 |
teamingPolicy = self.dvs_portgroup.config.defaultPortConfig.uplinkTeamingPolicy |
| 847 |
|
| 848 |
if self.module.params['teaming_policy']['inbound_policy'] is not None and \ |
| 849 |
teamingPolicy.reversePolicy.value != self.module.params['teaming_policy']['inbound_policy']: |
| 850 |
return 'update' |
| 851 |
|
| 852 |
if teamingPolicy.policy.value != self.module.params['teaming_policy']['load_balance_policy'] or \ |
| 853 |
teamingPolicy.notifySwitches.value != self.module.params['teaming_policy']['notify_switches'] or \ |
| 854 |
teamingPolicy.rollingOrder.value != self.module.params['teaming_policy']['rolling_order']: |
| 855 |
return 'update' |
| 856 |
|
| 857 |
if self.module.params['teaming_policy']['active_uplinks'] and \ |
| 858 |
teamingPolicy.uplinkPortOrder.activeUplinkPort != self.module.params['teaming_policy']['active_uplinks']: |
| 859 |
return 'update' |
| 860 |
|
| 861 |
if self.module.params['teaming_policy']['standby_uplinks'] and \ |
| 862 |
teamingPolicy.uplinkPortOrder.standbyUplinkPort != self.module.params['teaming_policy']['standby_uplinks']: |
| 863 |
return 'update' |
| 864 |
|
| 865 |
# NetFlow |
| 866 |
net_flow = self.module.params['net_flow'] |
| 867 |
if net_flow is not None: |
| 868 |
if is_boolean(net_flow) and \ |
| 869 |
(self.dvs_portgroup.config.defaultPortConfig.ipfixEnabled.inherited is not False |
| 870 |
or self.dvs_portgroup.config.defaultPortConfig.ipfixEnabled.value != is_truthy(net_flow)): |
| 871 |
return 'update' |
| 872 |
elif self.dvs_portgroup.config.defaultPortConfig.ipfixEnabled.inherited is not True: |
| 873 |
return 'update' |
| 874 |
|
| 875 |
# Ingress traffic shaping |
| 876 |
in_traffic_shaping = self.module.params['in_traffic_shaping'] |
| 877 |
if in_traffic_shaping is not None: |
| 878 |
if in_traffic_shaping['inherited'] is False and \ |
| 879 |
self.dvs_portgroup.config.defaultPortConfig.inShapingPolicy.inherited is not False and \ |
| 880 |
self.dvs_portgroup.config.defaultPortConfig.inShapingPolicy.enabled.inherited is not False and \ |
| 881 |
self.dvs_portgroup.config.defaultPortConfig.inShapingPolicy.enabled.value != in_traffic_shaping['enabled'] and \ |
| 882 |
self.dvs_portgroup.config.defaultPortConfig.inShapingPolicy.averageBandwidth.inherited is not False and \ |
| 883 |
self.dvs_portgroup.config.defaultPortConfig.inShapingPolicy.averageBandwidth.value != (in_traffic_shaping['average_bandwidth'] * 1000) and \ |
| 884 |
self.dvs_portgroup.config.defaultPortConfig.inShapingPolicy.burstSize.inherited is not False and \ |
| 885 |
self.dvs_portgroup.config.defaultPortConfig.inShapingPolicy.burstSize.value != (in_traffic_shaping['burst_size'] * 1024) and \ |
| 886 |
self.dvs_portgroup.config.defaultPortConfig.inShapingPolicy.peakBandwidth.inherited is not False and \ |
| 887 |
self.dvs_portgroup.config.defaultPortConfig.inShapingPolicy.peakBandwidth.value != (in_traffic_shaping['peak_bandwidth'] * 1000): |
| 888 |
return 'update' |
| 889 |
elif in_traffic_shaping['inherited'] is True and \ |
| 890 |
self.dvs_portgroup.config.defaultPortConfig.inShapingPolicy.inherited is not True and \ |
| 891 |
self.dvs_portgroup.config.defaultPortConfig.inShapingPolicy.enabled.inherited is not True and \ |
| 892 |
self.dvs_portgroup.config.defaultPortConfig.inShapingPolicy.averageBandwidth.inherited is not True and \ |
| 893 |
self.dvs_portgroup.config.defaultPortConfig.inShapingPolicy.burstSize.inherited is not True and \ |
| 894 |
self.dvs_portgroup.config.defaultPortConfig.inShapingPolicy.peakBandwidth.inherited is not True: |
| 895 |
return 'update' |
| 896 |
|
| 897 |
# Egress traffic shaping |
| 898 |
out_traffic_shaping = self.module.params['out_traffic_shaping'] |
| 899 |
if out_traffic_shaping is not None: |
| 900 |
if out_traffic_shaping['inherited'] is False and \ |
| 901 |
self.dvs_portgroup.config.defaultPortConfig.outShapingPolicy.inherited is not False and \ |
| 902 |
self.dvs_portgroup.config.defaultPortConfig.outShapingPolicy.enabled.inherited is not False and \ |
| 903 |
self.dvs_portgroup.config.defaultPortConfig.outShapingPolicy.enabled.value != out_traffic_shaping['enabled'] and \ |
| 904 |
self.dvs_portgroup.config.defaultPortConfig.outShapingPolicy.averageBandwidth.inherited is not False and \ |
| 905 |
self.dvs_portgroup.config.defaultPortConfig.outShapingPolicy.averageBandwidth.value != \ |
| 906 |
(out_traffic_shaping['average_bandwidth'] * 1000) and \ |
| 907 |
self.dvs_portgroup.config.defaultPortConfig.outShapingPolicy.burstSize.inherited is not False and \ |
| 908 |
self.dvs_portgroup.config.defaultPortConfig.outShapingPolicy.burstSize.value != (out_traffic_shaping['burst_size'] * 1024) and \ |
| 909 |
self.dvs_portgroup.config.defaultPortConfig.outShapingPolicy.peakBandwidth.inherited is not False and \ |
| 910 |
self.dvs_portgroup.config.defaultPortConfig.outShapingPolicy.peakBandwidth.value != (out_traffic_shaping['peak_bandwidth'] * 1000): |
| 911 |
return 'update' |
| 912 |
elif self.module.params['out_traffic_shaping'] is None and \ |
| 913 |
self.dvs_portgroup.config.defaultPortConfig.outShapingPolicy.inherited is not True and \ |
| 914 |
self.dvs_portgroup.config.defaultPortConfig.outShapingPolicy.enabled.inherited is not True and \ |
| 915 |
self.dvs_portgroup.config.defaultPortConfig.outShapingPolicy.averageBandwidth.inherited is not True and \ |
| 916 |
self.dvs_portgroup.config.defaultPortConfig.outShapingPolicy.burstSize.inherited is not True and \ |
| 917 |
self.dvs_portgroup.config.defaultPortConfig.outShapingPolicy.peakBandwidth.inherited is not True: |
| 918 |
return 'update' |
| 919 |
|
| 920 |
# PG policy (advanced_policy) |
| 921 |
policy = self.dvs_portgroup.config.policy |
| 922 |
if policy.blockOverrideAllowed != self.module.params['port_policy']['block_override'] or \ |
| 923 |
policy.ipfixOverrideAllowed != self.module.params['port_policy']['ipfix_override'] or \ |
| 924 |
policy.livePortMovingAllowed != self.module.params['port_policy']['live_port_move'] or \ |
| 925 |
policy.macManagementOverrideAllowed != self.module.params['port_policy']['mac_management_override'] or \ |
| 926 |
policy.networkResourcePoolOverrideAllowed != self.module.params['port_policy']['network_rp_override'] or \ |
| 927 |
policy.portConfigResetAtDisconnect != self.module.params['port_policy']['port_config_reset_at_disconnect'] or \ |
| 928 |
policy.shapingOverrideAllowed != self.module.params['port_policy']['shaping_override'] or \ |
| 929 |
policy.trafficFilterOverrideAllowed != self.module.params['port_policy']['traffic_filter_override'] or \ |
| 930 |
policy.uplinkTeamingOverrideAllowed != self.module.params['port_policy']['uplink_teaming_override'] or \ |
| 931 |
policy.vendorConfigOverrideAllowed != self.module.params['port_policy']['vendor_config_override'] or \ |
| 932 |
policy.vlanOverrideAllowed != self.module.params['port_policy']['vlan_override']: |
| 933 |
return 'update' |
| 934 |
|
| 935 |
# PG Type |
| 936 |
if self.module.params['port_binding'] == 'ephemeral': |
| 937 |
if self.dvs_portgroup.config.type != 'ephemeral': |
| 938 |
return 'update' |
| 939 |
elif self.port_allocation == 'fixed' and self.dvs_portgroup.config.type != 'earlyBinding': |
| 940 |
return 'update' |
| 941 |
|
| 942 |
# Check port allocation |
| 943 |
if self.port_allocation == 'elastic' and self.dvs_portgroup.config.autoExpand is False: |
| 944 |
return 'update' |
| 945 |
elif self.port_allocation == 'fixed' and self.dvs_portgroup.config.autoExpand is True: |
| 946 |
return 'update' |
| 947 |
|
| 948 |
return 'present' |
| 949 |
|
| 950 |
|
| 951 |
def main(): |
| 952 |
argument_spec = base_argument_spec() |
| 953 |
argument_spec.update( |
| 954 |
dict( |
| 955 |
portgroup_name=dict(required=True, type='str'), |
| 956 |
portgroup_description=dict(required=False, type='str', aliases=['description']), |
| 957 |
switch_name=dict(required=True, type='str'), |
| 958 |
vlan_id=dict(required=True, type='str'), |
| 959 |
num_ports=dict(type='int'), |
| 960 |
port_binding=dict(required=True, type='str', choices=['static', 'ephemeral']), |
| 961 |
port_allocation=dict(type='str', choices=['fixed', 'elastic']), |
| 962 |
state=dict(type='str', choices=['present', 'absent'], default='present'), |
| 963 |
vlan_trunk=dict(type='bool', default=False), |
| 964 |
vlan_private=dict(type='bool', default=False), |
| 965 |
network_policy=dict( |
| 966 |
type='dict', |
| 967 |
options=dict( |
| 968 |
inherited=dict(type='bool', required=True), |
| 969 |
promiscuous=dict(type='bool'), |
| 970 |
forged_transmits=dict(type='bool'), |
| 971 |
mac_changes=dict(type='bool'), |
| 972 |
mac_learning=dict( |
| 973 |
type='dict', |
| 974 |
options=dict( |
| 975 |
allow_unicast_flooding=dict(type='bool'), |
| 976 |
enabled=dict(type='bool'), |
| 977 |
limit=dict(type='int'), |
| 978 |
limit_policy=dict(type='str', choices=['allow', 'drop']), |
| 979 |
), |
| 980 |
) |
| 981 |
), |
| 982 |
required_if=[ |
| 983 |
('inherited', False, ('promiscuous', 'forged_transmits', 'mac_changes')) |
| 984 |
], |
| 985 |
), |
| 986 |
in_traffic_shaping=dict( |
| 987 |
type='dict', |
| 988 |
options=dict( |
| 989 |
inherited=dict(type='bool', required=True), |
| 990 |
enabled=dict(type='bool'), |
| 991 |
average_bandwidth=dict(type='int'), |
| 992 |
peak_bandwidth=dict(type='int'), |
| 993 |
burst_size=dict(type='int'), |
| 994 |
), |
| 995 |
required_if=[ |
| 996 |
('inherited', False, ('average_bandwidth', 'peak_bandwidth', 'burst_size')) |
| 997 |
], |
| 998 |
), |
| 999 |
out_traffic_shaping=dict( |
| 1000 |
type='dict', |
| 1001 |
options=dict( |
| 1002 |
inherited=dict(type='bool', required=True), |
| 1003 |
enabled=dict(type='bool'), |
| 1004 |
average_bandwidth=dict(type='int'), |
| 1005 |
peak_bandwidth=dict(type='int'), |
| 1006 |
burst_size=dict(type='int'), |
| 1007 |
), |
| 1008 |
required_if=[ |
| 1009 |
('inherited', False, ('average_bandwidth', 'peak_bandwidth', 'burst_size')) |
| 1010 |
], |
| 1011 |
), |
| 1012 |
net_flow=dict( |
| 1013 |
type='str', |
| 1014 |
choices=[ |
| 1015 |
'true', |
| 1016 |
'on', |
| 1017 |
'yes', |
| 1018 |
'false', |
| 1019 |
'off', |
| 1020 |
'no', |
| 1021 |
'inherited', |
| 1022 |
], |
| 1023 |
), |
| 1024 |
teaming_policy=dict( |
| 1025 |
type='dict', |
| 1026 |
options=dict( |
| 1027 |
inbound_policy=dict(type='bool'), |
| 1028 |
notify_switches=dict(type='bool', default=True), |
| 1029 |
rolling_order=dict(type='bool', default=False), |
| 1030 |
load_balance_policy=dict(type='str', |
| 1031 |
default='loadbalance_srcid', |
| 1032 |
choices=[ |
| 1033 |
'loadbalance_ip', |
| 1034 |
'loadbalance_srcmac', |
| 1035 |
'loadbalance_srcid', |
| 1036 |
'loadbalance_loadbased', |
| 1037 |
'failover_explicit', |
| 1038 |
], |
| 1039 |
), |
| 1040 |
active_uplinks=dict(type='list', elements='str'), |
| 1041 |
standby_uplinks=dict(type='list', elements='str'), |
| 1042 |
), |
| 1043 |
default=dict( |
| 1044 |
notify_switches=True, |
| 1045 |
rolling_order=False, |
| 1046 |
load_balance_policy='loadbalance_srcid', |
| 1047 |
), |
| 1048 |
), |
| 1049 |
port_policy=dict( |
| 1050 |
type='dict', |
| 1051 |
options=dict( |
| 1052 |
block_override=dict(type='bool', default=True), |
| 1053 |
ipfix_override=dict(type='bool', default=False), |
| 1054 |
live_port_move=dict(type='bool', default=False), |
| 1055 |
network_rp_override=dict(type='bool', default=False), |
| 1056 |
port_config_reset_at_disconnect=dict(type='bool', default=True), |
| 1057 |
mac_management_override=dict(type='bool', default=False), |
| 1058 |
shaping_override=dict(type='bool', default=False), |
| 1059 |
traffic_filter_override=dict(type='bool', default=False), |
| 1060 |
uplink_teaming_override=dict(type='bool', default=False), |
| 1061 |
vendor_config_override=dict(type='bool', default=False), |
| 1062 |
vlan_override=dict(type='bool', default=False) |
| 1063 |
), |
| 1064 |
default=dict( |
| 1065 |
block_override=True, |
| 1066 |
ipfix_override=False, |
| 1067 |
live_port_move=False, |
| 1068 |
network_rp_override=False, |
| 1069 |
port_config_reset_at_disconnect=True, |
| 1070 |
mac_management_override=False, |
| 1071 |
shaping_override=False, |
| 1072 |
traffic_filter_override=False, |
| 1073 |
uplink_teaming_override=False, |
| 1074 |
vendor_config_override=False, |
| 1075 |
vlan_override=False |
| 1076 |
), |
| 1077 |
), |
| 1078 |
mac_learning=dict( |
| 1079 |
type='dict', |
| 1080 |
removed_in_version='7.0.0', |
| 1081 |
removed_from_collection='community.vmware', |
| 1082 |
options=dict( |
| 1083 |
allow_unicast_flooding=dict(type='bool'), |
| 1084 |
enabled=dict(type='bool'), |
| 1085 |
limit=dict(type='int'), |
| 1086 |
limit_policy=dict(type='str', choices=['allow', 'drop']), |
| 1087 |
), |
| 1088 |
) |
| 1089 |
) |
| 1090 |
) |
| 1091 |
|
| 1092 |
module = AnsibleModule(argument_spec=argument_spec, |
| 1093 |
mutually_exclusive=[ |
| 1094 |
['vlan_trunk', 'vlan_private'], |
| 1095 |
], |
| 1096 |
supports_check_mode=True) |
| 1097 |
|
| 1098 |
vmware_dvs_portgroup = VMwareDvsPortgroup(module) |
| 1099 |
vmware_dvs_portgroup.process_state() |
| 1100 |
|
| 1101 |
|
| 1102 |
if __name__ == '__main__': |
| 1103 |
main() |