| 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 |
# Copyright: (c) 2017, Abhijeet Kasurde <[email protected]> |
| 7 |
# Copyright: (c) 2018, Christian Kotte <[email protected]> |
| 8 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 9 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 10 |
|
| 11 |
from __future__ import absolute_import, division, print_function |
| 12 |
__metaclass__ = type |
| 13 |
|
| 14 |
|
| 15 |
DOCUMENTATION = r''' |
| 16 |
--- |
| 17 |
module: vmware_portgroup |
| 18 |
short_description: Create a VMware portgroup |
| 19 |
description: |
| 20 |
- Create a VMware Port Group on a VMware Standard Switch (vSS) for given ESXi host(s) or hosts of given cluster. |
| 21 |
author: |
| 22 |
- Joseph Callen (@jcpowermac) |
| 23 |
- Russell Teague (@mtnbikenc) |
| 24 |
- Abhijeet Kasurde (@Akasurde) |
| 25 |
- Christian Kotte (@ckotte) |
| 26 |
options: |
| 27 |
switch: |
| 28 |
description: |
| 29 |
- vSwitch to modify. |
| 30 |
required: true |
| 31 |
aliases: [ 'switch_name', 'vswitch' ] |
| 32 |
type: str |
| 33 |
portgroup: |
| 34 |
description: |
| 35 |
- Portgroup name to add. |
| 36 |
required: true |
| 37 |
aliases: [ 'portgroup_name' ] |
| 38 |
type: str |
| 39 |
vlan_id: |
| 40 |
description: |
| 41 |
- VLAN ID to assign to portgroup. |
| 42 |
- Set to 0 (no VLAN tagging) by default. |
| 43 |
required: false |
| 44 |
default: 0 |
| 45 |
aliases: [ 'vlan' ] |
| 46 |
type: int |
| 47 |
security: |
| 48 |
description: |
| 49 |
- Network policy specifies layer 2 security settings for a |
| 50 |
portgroup such as promiscuous mode, where guest adapter listens |
| 51 |
to all the packets, MAC address changes and forged transmits. |
| 52 |
- Dict which configures the different security values for portgroup. |
| 53 |
suboptions: |
| 54 |
promiscuous_mode: |
| 55 |
type: bool |
| 56 |
description: Indicates whether promiscuous mode is allowed. |
| 57 |
forged_transmits: |
| 58 |
type: bool |
| 59 |
description: Indicates whether forged transmits are allowed. |
| 60 |
mac_changes: |
| 61 |
type: bool |
| 62 |
description: Indicates whether mac changes are allowed. |
| 63 |
required: false |
| 64 |
aliases: [ 'security_policy', 'network_policy' ] |
| 65 |
type: dict |
| 66 |
teaming: |
| 67 |
description: |
| 68 |
- Dictionary which configures the different teaming values for portgroup. |
| 69 |
suboptions: |
| 70 |
load_balancing: |
| 71 |
type: str |
| 72 |
description: |
| 73 |
- Network adapter teaming policy. |
| 74 |
choices: [ loadbalance_ip, loadbalance_srcmac, loadbalance_srcid, failover_explicit ] |
| 75 |
aliases: [ 'load_balance_policy' ] |
| 76 |
network_failure_detection: |
| 77 |
type: str |
| 78 |
description: Network failure detection. |
| 79 |
choices: [ link_status_only, beacon_probing ] |
| 80 |
notify_switches: |
| 81 |
type: bool |
| 82 |
description: Indicate whether or not to notify the physical switch if a link fails. |
| 83 |
failback: |
| 84 |
type: bool |
| 85 |
description: Indicate whether or not to use a failback when restoring links. |
| 86 |
active_adapters: |
| 87 |
type: list |
| 88 |
description: |
| 89 |
- List of active adapters used for load balancing. |
| 90 |
- All vmnics are used as active adapters if O(teaming.active_adapters) and O(teaming.standby_adapters) are not defined. |
| 91 |
elements: str |
| 92 |
standby_adapters: |
| 93 |
type: list |
| 94 |
description: |
| 95 |
- List of standby adapters used for failover. |
| 96 |
- All vmnics are used as active adapters if O(teaming.active_adapters) and O(teaming.standby_adapters) are not defined. |
| 97 |
elements: str |
| 98 |
required: false |
| 99 |
aliases: [ 'teaming_policy' ] |
| 100 |
type: dict |
| 101 |
traffic_shaping: |
| 102 |
description: |
| 103 |
- Dictionary which configures traffic shaping for the switch. |
| 104 |
suboptions: |
| 105 |
enabled: |
| 106 |
type: bool |
| 107 |
description: Status of Traffic Shaping Policy. |
| 108 |
average_bandwidth: |
| 109 |
type: int |
| 110 |
description: Average bandwidth (kbit/s). |
| 111 |
peak_bandwidth: |
| 112 |
type: int |
| 113 |
description: Peak bandwidth (kbit/s). |
| 114 |
burst_size: |
| 115 |
type: int |
| 116 |
description: Burst size (KB). |
| 117 |
required: false |
| 118 |
type: dict |
| 119 |
cluster_name: |
| 120 |
description: |
| 121 |
- Name of cluster name for host membership. |
| 122 |
- Portgroup will be created on all hosts of the given cluster. |
| 123 |
- This option is required if O(hosts) is not specified. |
| 124 |
aliases: [ 'cluster' ] |
| 125 |
type: str |
| 126 |
hosts: |
| 127 |
description: |
| 128 |
- List of name of host or hosts on which portgroup needs to be added. |
| 129 |
- This option is required if O(cluster_name) is not specified. |
| 130 |
aliases: [ esxi_hostname ] |
| 131 |
type: list |
| 132 |
elements: str |
| 133 |
state: |
| 134 |
description: |
| 135 |
- Determines if the portgroup should be present or not. |
| 136 |
choices: |
| 137 |
- 'present' |
| 138 |
- 'absent' |
| 139 |
default: present |
| 140 |
type: str |
| 141 |
extends_documentation_fragment: |
| 142 |
- vmware.vmware.base_options |
| 143 |
|
| 144 |
''' |
| 145 |
|
| 146 |
EXAMPLES = r''' |
| 147 |
- name: Add Management Network VM Portgroup |
| 148 |
community.vmware.vmware_portgroup: |
| 149 |
hostname: "{{ esxi_hostname }}" |
| 150 |
username: "{{ esxi_username }}" |
| 151 |
password: "{{ esxi_password }}" |
| 152 |
switch: "{{ vswitch_name }}" |
| 153 |
portgroup: "{{ portgroup_name }}" |
| 154 |
vlan_id: "{{ vlan_id }}" |
| 155 |
delegate_to: localhost |
| 156 |
|
| 157 |
- name: Add Portgroup with Promiscuous Mode Enabled |
| 158 |
community.vmware.vmware_portgroup: |
| 159 |
hostname: "{{ esxi_hostname }}" |
| 160 |
username: "{{ esxi_username }}" |
| 161 |
password: "{{ esxi_password }}" |
| 162 |
switch: "{{ vswitch_name }}" |
| 163 |
portgroup: "{{ portgroup_name }}" |
| 164 |
security: |
| 165 |
promiscuous_mode: true |
| 166 |
delegate_to: localhost |
| 167 |
|
| 168 |
- name: Add Management Network VM Portgroup to specific hosts |
| 169 |
community.vmware.vmware_portgroup: |
| 170 |
hostname: "{{ vcenter_hostname }}" |
| 171 |
username: "{{ vcenter_username }}" |
| 172 |
password: "{{ vcenter_password }}" |
| 173 |
hosts: [esxi_hostname_one] |
| 174 |
switch: "{{ vswitch_name }}" |
| 175 |
portgroup: "{{ portgroup_name }}" |
| 176 |
vlan_id: "{{ vlan_id }}" |
| 177 |
delegate_to: localhost |
| 178 |
|
| 179 |
- name: Add Management Network VM Portgroup to all hosts in a cluster |
| 180 |
community.vmware.vmware_portgroup: |
| 181 |
hostname: "{{ vcenter_hostname }}" |
| 182 |
username: "{{ vcenter_username }}" |
| 183 |
password: "{{ vcenter_password }}" |
| 184 |
cluster_name: "{{ cluster_name }}" |
| 185 |
switch: "{{ vswitch_name }}" |
| 186 |
portgroup: "{{ portgroup_name }}" |
| 187 |
vlan_id: "{{ vlan_id }}" |
| 188 |
delegate_to: localhost |
| 189 |
|
| 190 |
- name: Remove Management Network VM Portgroup to all hosts in a cluster |
| 191 |
community.vmware.vmware_portgroup: |
| 192 |
hostname: "{{ vcenter_hostname }}" |
| 193 |
username: "{{ vcenter_username }}" |
| 194 |
password: "{{ vcenter_password }}" |
| 195 |
cluster_name: "{{ cluster_name }}" |
| 196 |
switch: "{{ vswitch_name }}" |
| 197 |
portgroup: "{{ portgroup_name }}" |
| 198 |
vlan_id: "{{ vlan_id }}" |
| 199 |
state: absent |
| 200 |
delegate_to: localhost |
| 201 |
|
| 202 |
- name: Add Portgroup with all settings defined |
| 203 |
community.vmware.vmware_portgroup: |
| 204 |
hostname: "{{ vcenter_hostname }}" |
| 205 |
username: "{{ vcenter_username }}" |
| 206 |
password: "{{ vcenter_password }}" |
| 207 |
esxi_hostname: "{{ inventory_hostname }}" |
| 208 |
switch: "{{ vswitch_name }}" |
| 209 |
portgroup: "{{ portgroup_name }}" |
| 210 |
vlan_id: 10 |
| 211 |
security: |
| 212 |
promiscuous_mode: false |
| 213 |
mac_changes: false |
| 214 |
forged_transmits: false |
| 215 |
traffic_shaping: |
| 216 |
enabled: true |
| 217 |
average_bandwidth: 100000 |
| 218 |
peak_bandwidth: 100000 |
| 219 |
burst_size: 102400 |
| 220 |
teaming: |
| 221 |
load_balancing: failover_explicit |
| 222 |
network_failure_detection: link_status_only |
| 223 |
notify_switches: true |
| 224 |
failback: true |
| 225 |
active_adapters: |
| 226 |
- vmnic0 |
| 227 |
standby_adapters: |
| 228 |
- vmnic1 |
| 229 |
delegate_to: localhost |
| 230 |
register: teaming_result |
| 231 |
''' |
| 232 |
|
| 233 |
RETURN = r''' |
| 234 |
result: |
| 235 |
description: metadata about the portgroup |
| 236 |
returned: always |
| 237 |
type: dict |
| 238 |
sample: { |
| 239 |
"esxi01.example.com": { |
| 240 |
"changed": true, |
| 241 |
"failback": "No override", |
| 242 |
"failover_active": "No override", |
| 243 |
"failover_standby": "No override", |
| 244 |
"failure_detection": "No override", |
| 245 |
"load_balancing": "No override", |
| 246 |
"msg": "Port Group added", |
| 247 |
"notify_switches": "No override", |
| 248 |
"portgroup": "vMotion", |
| 249 |
"sec_forged_transmits": false, |
| 250 |
"sec_mac_changes": false, |
| 251 |
"sec_promiscuous_mode": false, |
| 252 |
"traffic_shaping": "No override", |
| 253 |
"vlan_id": 33, |
| 254 |
"vswitch": "vSwitch1" |
| 255 |
} |
| 256 |
} |
| 257 |
''' |
| 258 |
|
| 259 |
|
| 260 |
try: |
| 261 |
from pyVmomi import vim, vmodl |
| 262 |
except ImportError: |
| 263 |
pass |
| 264 |
|
| 265 |
from ansible.module_utils.basic import AnsibleModule |
| 266 |
from ansible_collections.community.vmware.plugins.module_utils.vmware import PyVmomi |
| 267 |
from ansible_collections.vmware.vmware.plugins.module_utils.argument_spec import base_argument_spec |
| 268 |
from ansible.module_utils.common.text.converters import to_native |
| 269 |
|
| 270 |
|
| 271 |
class VMwareHostPortGroup(PyVmomi): |
| 272 |
"""Manage portgroup""" |
| 273 |
|
| 274 |
def __init__(self, module): |
| 275 |
super(VMwareHostPortGroup, self).__init__(module) |
| 276 |
self.switch_object = None |
| 277 |
self.portgroup_object = None |
| 278 |
hosts = self.params['hosts'] |
| 279 |
cluster = self.params['cluster_name'] |
| 280 |
self.portgroup = self.params['portgroup'] |
| 281 |
self.switch = self.params['switch'] |
| 282 |
self.vlan_id = self.params['vlan_id'] |
| 283 |
if self.params['security']: |
| 284 |
self.sec_promiscuous_mode = self.params['security'].get('promiscuous_mode') |
| 285 |
self.sec_forged_transmits = self.params['security'].get('forged_transmits') |
| 286 |
self.sec_mac_changes = self.params['security'].get('mac_changes') |
| 287 |
else: |
| 288 |
self.sec_promiscuous_mode = None |
| 289 |
self.sec_forged_transmits = None |
| 290 |
self.sec_mac_changes = None |
| 291 |
if self.params['traffic_shaping']: |
| 292 |
self.ts_enabled = self.params['traffic_shaping'].get('enabled') |
| 293 |
if self.ts_enabled is True: |
| 294 |
for value in ['average_bandwidth', 'peak_bandwidth', 'burst_size']: |
| 295 |
if not self.params['traffic_shaping'].get(value): |
| 296 |
self.module.fail_json(msg="traffic_shaping.%s is a required parameter if traffic_shaping is enabled." % value) |
| 297 |
self.ts_average_bandwidth = self.params['traffic_shaping'].get('average_bandwidth') |
| 298 |
self.ts_peak_bandwidth = self.params['traffic_shaping'].get('peak_bandwidth') |
| 299 |
self.ts_burst_size = self.params['traffic_shaping'].get('burst_size') |
| 300 |
else: |
| 301 |
self.ts_enabled = None |
| 302 |
self.ts_average_bandwidth = None |
| 303 |
self.ts_peak_bandwidth = None |
| 304 |
self.ts_burst_size = None |
| 305 |
if self.params['teaming']: |
| 306 |
self.teaming_load_balancing = self.params['teaming'].get('load_balancing') |
| 307 |
self.teaming_failure_detection = self.params['teaming'].get('network_failure_detection') |
| 308 |
self.teaming_notify_switches = self.params['teaming'].get('notify_switches') |
| 309 |
self.teaming_failback = self.params['teaming'].get('failback') |
| 310 |
self.teaming_failover_order_active = self.params['teaming'].get('active_adapters') |
| 311 |
self.teaming_failover_order_standby = self.params['teaming'].get('standby_adapters') |
| 312 |
if self.teaming_failover_order_active is None: |
| 313 |
self.teaming_failover_order_active = [] |
| 314 |
if self.teaming_failover_order_standby is None: |
| 315 |
self.teaming_failover_order_standby = [] |
| 316 |
else: |
| 317 |
self.teaming_load_balancing = None |
| 318 |
self.teaming_failure_detection = None |
| 319 |
self.teaming_notify_switches = None |
| 320 |
self.teaming_failback = None |
| 321 |
self.teaming_failover_order_active = None |
| 322 |
self.teaming_failover_order_standby = None |
| 323 |
self.state = self.params['state'] |
| 324 |
|
| 325 |
self.hosts = self.get_all_host_objs(cluster_name=cluster, esxi_host_name=hosts) |
| 326 |
if not self.hosts: |
| 327 |
self.module.fail_json(msg="Failed to find host system with given configuration.") |
| 328 |
|
| 329 |
def process_state(self): |
| 330 |
"""Manage internal state of the portgroup""" |
| 331 |
results = dict(changed=False, result=dict()) |
| 332 |
host_change_list = [] |
| 333 |
for host in self.hosts: |
| 334 |
changed = False |
| 335 |
results['result'][host.name] = dict() |
| 336 |
switch_state = self.check_if_vswitch_exists(host_system=host) |
| 337 |
if switch_state == 'absent': |
| 338 |
self.module.fail_json(msg="The vSwitch '%s' doesn't exist on host '%s'" % (self.switch, host.name)) |
| 339 |
portgroup_state = self.check_if_portgroup_exists(host_system=host) |
| 340 |
if self.state == 'present' and portgroup_state == 'present': |
| 341 |
changed, host_results = self.update_host_port_group( |
| 342 |
host_system=host, |
| 343 |
portgroup_object=self.portgroup_object |
| 344 |
) |
| 345 |
elif self.state == 'present' and portgroup_state == 'absent': |
| 346 |
changed, host_results = self.create_host_port_group(host_system=host) |
| 347 |
elif self.state == 'absent' and portgroup_state == 'present': |
| 348 |
changed, host_results = self.remove_host_port_group(host_system=host) |
| 349 |
else: |
| 350 |
host_results = dict() |
| 351 |
host_results['changed'] = False |
| 352 |
host_results['msg'] = "Port Group already deleted" |
| 353 |
host_results['portgroup'] = self.portgroup |
| 354 |
results['result'][host.name] = host_results |
| 355 |
|
| 356 |
host_change_list.append(changed) |
| 357 |
|
| 358 |
if any(host_change_list): |
| 359 |
results['changed'] = True |
| 360 |
self.module.exit_json(**results) |
| 361 |
|
| 362 |
def check_if_portgroup_exists(self, host_system): |
| 363 |
""" |
| 364 |
Check if portgroup exists |
| 365 |
Returns: 'present' if portgroup exists or 'absent' if not |
| 366 |
""" |
| 367 |
self.portgroup_object = self.find_portgroup_by_name( |
| 368 |
host_system=host_system, |
| 369 |
portgroup_name=self.portgroup, |
| 370 |
vswitch_name=self.switch |
| 371 |
) |
| 372 |
if self.portgroup_object is None: |
| 373 |
return 'absent' |
| 374 |
return 'present' |
| 375 |
|
| 376 |
def find_portgroup_by_name(self, host_system, portgroup_name, vswitch_name): |
| 377 |
""" |
| 378 |
Find and return port group managed object |
| 379 |
Args: |
| 380 |
host_system: Name of Host System |
| 381 |
portgroup_name: Name of the Port Group |
| 382 |
vswitch_name: Name of the vSwitch |
| 383 |
|
| 384 |
Returns: Port Group managed object if found, else None |
| 385 |
""" |
| 386 |
portgroups = self.get_all_port_groups_by_host(host_system=host_system) |
| 387 |
for portgroup in portgroups: |
| 388 |
if portgroup.spec.name == portgroup_name and portgroup.spec.vswitchName != vswitch_name: |
| 389 |
# portgroup names are unique; there can be only one portgroup with the same name per host |
| 390 |
self.module.fail_json(msg="The portgroup already exists on vSwitch '%s'" % portgroup.spec.vswitchName) |
| 391 |
if portgroup.spec.name == portgroup_name and portgroup.spec.vswitchName == vswitch_name: |
| 392 |
return portgroup |
| 393 |
return None |
| 394 |
|
| 395 |
def check_if_vswitch_exists(self, host_system): |
| 396 |
""" |
| 397 |
Check if vSwitch exists |
| 398 |
Returns: 'present' if vSwitch exists or 'absent' if not |
| 399 |
""" |
| 400 |
self.switch_object = self.find_vswitch_by_name( |
| 401 |
host_system=host_system, |
| 402 |
vswitch_name=self.switch |
| 403 |
) |
| 404 |
if self.switch_object is None: |
| 405 |
return 'absent' |
| 406 |
return 'present' |
| 407 |
|
| 408 |
@staticmethod |
| 409 |
def find_vswitch_by_name(host_system, vswitch_name): |
| 410 |
""" |
| 411 |
Find and return vSwitch managed object |
| 412 |
Args: |
| 413 |
host: Host system managed object |
| 414 |
vswitch_name: Name of vSwitch to find |
| 415 |
|
| 416 |
Returns: vSwitch managed object if found, else None |
| 417 |
""" |
| 418 |
for vss in host_system.configManager.networkSystem.networkInfo.vswitch: |
| 419 |
if vss.name == vswitch_name: |
| 420 |
return vss |
| 421 |
return None |
| 422 |
|
| 423 |
def remove_host_port_group(self, host_system): |
| 424 |
""" |
| 425 |
Remove a Port Group from a given host |
| 426 |
Args: |
| 427 |
host_system: Name of Host System |
| 428 |
""" |
| 429 |
host_results = dict(changed=False, msg="") |
| 430 |
|
| 431 |
if self.module.check_mode: |
| 432 |
host_results['msg'] = "Port Group would be removed" |
| 433 |
else: |
| 434 |
try: |
| 435 |
host_system.configManager.networkSystem.RemovePortGroup(pgName=self.portgroup) |
| 436 |
host_results['msg'] = "Port Group removed" |
| 437 |
except vim.fault.NotFound as not_found: |
| 438 |
self.module.fail_json( |
| 439 |
msg="Failed to remove Portgroup as it was not found: %s" % to_native(not_found.msg) |
| 440 |
) |
| 441 |
except vim.fault.ResourceInUse as resource_in_use: |
| 442 |
self.module.fail_json( |
| 443 |
msg="Failed to remove Portgroup as it is in use: %s" % to_native(resource_in_use.msg) |
| 444 |
) |
| 445 |
except vim.fault.HostConfigFault as host_config_fault: |
| 446 |
self.module.fail_json( |
| 447 |
msg="Failed to remove Portgroup due to configuration failures: %s" % to_native(host_config_fault.msg) |
| 448 |
) |
| 449 |
host_results['changed'] = True |
| 450 |
host_results['portgroup'] = self.portgroup |
| 451 |
host_results['vswitch'] = self.switch |
| 452 |
|
| 453 |
return True, host_results |
| 454 |
|
| 455 |
def create_host_port_group(self, host_system): |
| 456 |
"""Create Port Group on a given host |
| 457 |
Args: |
| 458 |
host_system: Name of Host System |
| 459 |
""" |
| 460 |
host_results = dict(changed=False, msg="") |
| 461 |
|
| 462 |
if self.module.check_mode: |
| 463 |
host_results['msg'] = "Port Group would be added" |
| 464 |
else: |
| 465 |
port_group = vim.host.PortGroup.Config() |
| 466 |
port_group.spec = vim.host.PortGroup.Specification() |
| 467 |
port_group.spec.vswitchName = self.switch |
| 468 |
port_group.spec.name = self.portgroup |
| 469 |
port_group.spec.vlanId = self.vlan_id |
| 470 |
port_group.spec.policy = self.create_network_policy() |
| 471 |
|
| 472 |
try: |
| 473 |
host_system.configManager.networkSystem.AddPortGroup(portgrp=port_group.spec) |
| 474 |
host_results['changed'] = True |
| 475 |
host_results['msg'] = "Port Group added" |
| 476 |
except vim.fault.AlreadyExists as already_exists: |
| 477 |
self.module.fail_json( |
| 478 |
msg="Failed to add Portgroup as it already exists: %s" % to_native(already_exists.msg) |
| 479 |
) |
| 480 |
except vim.fault.NotFound as not_found: |
| 481 |
self.module.fail_json( |
| 482 |
msg="Failed to add Portgroup as vSwitch was not found: %s" % to_native(not_found.msg) |
| 483 |
) |
| 484 |
except vim.fault.HostConfigFault as host_config_fault: |
| 485 |
self.module.fail_json( |
| 486 |
msg="Failed to add Portgroup due to host system configuration failure : %s" % |
| 487 |
to_native(host_config_fault.msg) |
| 488 |
) |
| 489 |
except vmodl.fault.InvalidArgument as invalid_argument: |
| 490 |
self.module.fail_json( |
| 491 |
msg="Failed to add Portgroup as VLAN id was not correct as per specifications: %s" % |
| 492 |
to_native(invalid_argument.msg) |
| 493 |
) |
| 494 |
host_results['changed'] = True |
| 495 |
host_results['portgroup'] = self.portgroup |
| 496 |
host_results['vswitch'] = self.switch |
| 497 |
host_results['vlan_id'] = self.vlan_id |
| 498 |
if self.sec_promiscuous_mode is None: |
| 499 |
host_results['sec_promiscuous_mode'] = "No override" |
| 500 |
else: |
| 501 |
host_results['sec_promiscuous_mode'] = self.sec_promiscuous_mode |
| 502 |
if self.sec_mac_changes is None: |
| 503 |
host_results['sec_mac_changes'] = "No override" |
| 504 |
else: |
| 505 |
host_results['sec_mac_changes'] = self.sec_mac_changes |
| 506 |
if self.sec_forged_transmits is None: |
| 507 |
host_results['sec_forged_transmits'] = "No override" |
| 508 |
else: |
| 509 |
host_results['sec_forged_transmits'] = self.sec_forged_transmits |
| 510 |
host_results['traffic_shaping'] = "No override" if self.ts_enabled is None else self.ts_enabled |
| 511 |
host_results['load_balancing'] = "No override" if self.teaming_load_balancing is None \ |
| 512 |
else self.teaming_load_balancing |
| 513 |
host_results['notify_switches'] = "No override" if self.teaming_notify_switches is None \ |
| 514 |
else self.teaming_notify_switches |
| 515 |
host_results['failback'] = "No override" if self.teaming_failback is None else self.teaming_failback |
| 516 |
host_results['failover_active'] = "No override" if self.teaming_failover_order_active is None \ |
| 517 |
else self.teaming_failover_order_active |
| 518 |
host_results['failover_standby'] = "No override" if self.teaming_failover_order_standby is None \ |
| 519 |
else self.teaming_failover_order_standby |
| 520 |
host_results['failure_detection'] = "No override" if self.teaming_failure_detection is None \ |
| 521 |
else self.teaming_failure_detection |
| 522 |
|
| 523 |
return True, host_results |
| 524 |
|
| 525 |
def update_host_port_group(self, host_system, portgroup_object): |
| 526 |
"""Update a Port Group on a given host |
| 527 |
Args: |
| 528 |
host_system: Name of Host System |
| 529 |
""" |
| 530 |
changed = changed_security = False |
| 531 |
changed_list = [] |
| 532 |
host_results = dict(changed=False, msg="") |
| 533 |
spec = portgroup_object.spec |
| 534 |
message = '' |
| 535 |
|
| 536 |
# Check VLAN ID |
| 537 |
host_results['vlan_id'] = self.vlan_id |
| 538 |
if spec.vlanId != self.vlan_id: |
| 539 |
changed = True |
| 540 |
changed_list.append("VLAN ID") |
| 541 |
host_results['vlan_id_previous'] = spec.vlanId |
| 542 |
spec.vlanId = self.vlan_id |
| 543 |
|
| 544 |
# Check security settings |
| 545 |
if self.sec_promiscuous_mode is None: |
| 546 |
host_results['sec_promiscuous_mode'] = "No override" |
| 547 |
else: |
| 548 |
host_results['sec_promiscuous_mode'] = self.sec_promiscuous_mode |
| 549 |
if self.sec_mac_changes is None: |
| 550 |
host_results['sec_mac_changes'] = "No override" |
| 551 |
else: |
| 552 |
host_results['sec_mac_changes'] = self.sec_mac_changes |
| 553 |
if self.sec_forged_transmits is None: |
| 554 |
host_results['sec_forged_transmits'] = "No override" |
| 555 |
else: |
| 556 |
host_results['sec_forged_transmits'] = self.sec_forged_transmits |
| 557 |
if spec.policy.security: |
| 558 |
promiscuous_mode_previous = spec.policy.security.allowPromiscuous |
| 559 |
mac_changes_previous = spec.policy.security.macChanges |
| 560 |
forged_transmits_previous = spec.policy.security.forgedTransmits |
| 561 |
if promiscuous_mode_previous is not self.sec_promiscuous_mode: |
| 562 |
spec.policy.security.allowPromiscuous = self.sec_promiscuous_mode |
| 563 |
changed = changed_security = True |
| 564 |
changed_list.append("Promiscuous mode") |
| 565 |
if mac_changes_previous is not self.sec_mac_changes: |
| 566 |
spec.policy.security.macChanges = self.sec_mac_changes |
| 567 |
changed = changed_security = True |
| 568 |
changed_list.append("MAC address changes") |
| 569 |
if forged_transmits_previous is not self.sec_forged_transmits: |
| 570 |
spec.policy.security.forgedTransmits = self.sec_forged_transmits |
| 571 |
changed = changed_security = True |
| 572 |
changed_list.append("Forged transmits") |
| 573 |
if changed_security: |
| 574 |
if self.sec_promiscuous_mode is None: |
| 575 |
host_results['sec_promiscuous_mode_previous'] = "No override" |
| 576 |
else: |
| 577 |
host_results['sec_promiscuous_mode_previous'] = promiscuous_mode_previous |
| 578 |
if self.sec_mac_changes is None: |
| 579 |
host_results['sec_mac_changes_previous'] = "No override" |
| 580 |
else: |
| 581 |
host_results['sec_mac_changes'] = mac_changes_previous |
| 582 |
if self.sec_forged_transmits is None: |
| 583 |
host_results['sec_forged_transmits_previous'] = "No override" |
| 584 |
else: |
| 585 |
host_results['sec_forged_transmits_previous'] = forged_transmits_previous |
| 586 |
else: |
| 587 |
spec.policy.security = self.create_security_policy() |
| 588 |
changed = True |
| 589 |
changed_list.append("Security") |
| 590 |
host_results['sec_promiscuous_mode_previous'] = "No override" |
| 591 |
host_results['sec_mac_changes_previous'] = "No override" |
| 592 |
host_results['sec_forged_transmits_previous'] = "No override" |
| 593 |
|
| 594 |
# Check traffic shaping |
| 595 |
if self.ts_enabled is None: |
| 596 |
host_results['traffic_shaping'] = "No override" |
| 597 |
else: |
| 598 |
host_results['traffic_shaping'] = self.ts_enabled |
| 599 |
if self.ts_enabled: |
| 600 |
ts_average_bandwidth = self.ts_average_bandwidth * 1000 |
| 601 |
ts_peak_bandwidth = self.ts_peak_bandwidth * 1000 |
| 602 |
ts_burst_size = self.ts_burst_size * 1024 |
| 603 |
host_results['traffic_shaping_avg_bandw'] = ts_average_bandwidth |
| 604 |
host_results['traffic_shaping_peak_bandw'] = ts_peak_bandwidth |
| 605 |
host_results['traffic_shaping_burst'] = ts_burst_size |
| 606 |
if spec.policy.shapingPolicy and spec.policy.shapingPolicy.enabled is not None: |
| 607 |
if spec.policy.shapingPolicy.enabled: |
| 608 |
if self.ts_enabled: |
| 609 |
if spec.policy.shapingPolicy.averageBandwidth != ts_average_bandwidth: |
| 610 |
changed = True |
| 611 |
changed_list.append("Average bandwidth") |
| 612 |
host_results['traffic_shaping_avg_bandw_previous'] = spec.policy.shapingPolicy.averageBandwidth |
| 613 |
spec.policy.shapingPolicy.averageBandwidth = ts_average_bandwidth |
| 614 |
if spec.policy.shapingPolicy.peakBandwidth != ts_peak_bandwidth: |
| 615 |
changed = True |
| 616 |
changed_list.append("Peak bandwidth") |
| 617 |
host_results['traffic_shaping_peak_bandw_previous'] = spec.policy.shapingPolicy.peakBandwidth |
| 618 |
spec.policy.shapingPolicy.peakBandwidth = ts_peak_bandwidth |
| 619 |
if spec.policy.shapingPolicy.burstSize != ts_burst_size: |
| 620 |
changed = True |
| 621 |
changed_list.append("Burst size") |
| 622 |
host_results['traffic_shaping_burst_previous'] = spec.policy.shapingPolicy.burstSize |
| 623 |
spec.policy.shapingPolicy.burstSize = ts_burst_size |
| 624 |
elif self.ts_enabled is False: |
| 625 |
changed = True |
| 626 |
changed_list.append("Traffic shaping") |
| 627 |
host_results['traffic_shaping_previous'] = True |
| 628 |
spec.policy.shapingPolicy.enabled = False |
| 629 |
elif self.ts_enabled is None: |
| 630 |
spec.policy.shapingPolicy = None |
| 631 |
changed = True |
| 632 |
changed_list.append("Traffic shaping") |
| 633 |
host_results['traffic_shaping_previous'] = True |
| 634 |
else: |
| 635 |
if self.ts_enabled: |
| 636 |
spec.policy.shapingPolicy = self.create_shaping_policy() |
| 637 |
changed = True |
| 638 |
changed_list.append("Traffic shaping") |
| 639 |
host_results['traffic_shaping_previous'] = False |
| 640 |
elif self.ts_enabled is None: |
| 641 |
spec.policy.shapingPolicy = None |
| 642 |
changed = True |
| 643 |
changed_list.append("Traffic shaping") |
| 644 |
host_results['traffic_shaping_previous'] = True |
| 645 |
else: |
| 646 |
if self.ts_enabled: |
| 647 |
spec.policy.shapingPolicy = self.create_shaping_policy() |
| 648 |
changed = True |
| 649 |
changed_list.append("Traffic shaping") |
| 650 |
host_results['traffic_shaping_previous'] = "No override" |
| 651 |
elif self.ts_enabled is False: |
| 652 |
changed = True |
| 653 |
changed_list.append("Traffic shaping") |
| 654 |
host_results['traffic_shaping_previous'] = "No override" |
| 655 |
spec.policy.shapingPolicy.enabled = False |
| 656 |
|
| 657 |
# Check teaming |
| 658 |
if spec.policy.nicTeaming: |
| 659 |
# Check teaming policy |
| 660 |
if self.teaming_load_balancing is None: |
| 661 |
host_results['load_balancing'] = "No override" |
| 662 |
else: |
| 663 |
host_results['load_balancing'] = self.teaming_load_balancing |
| 664 |
if spec.policy.nicTeaming.policy: |
| 665 |
if spec.policy.nicTeaming.policy != self.teaming_load_balancing: |
| 666 |
changed = True |
| 667 |
changed_list.append("Load balancing") |
| 668 |
host_results['load_balancing_previous'] = spec.policy.nicTeaming.policy |
| 669 |
spec.policy.nicTeaming.policy = self.teaming_load_balancing |
| 670 |
else: |
| 671 |
if self.teaming_load_balancing: |
| 672 |
changed = True |
| 673 |
changed_list.append("Load balancing") |
| 674 |
host_results['load_balancing_previous'] = "No override" |
| 675 |
spec.policy.nicTeaming.policy = self.teaming_load_balancing |
| 676 |
# Check teaming notify switches |
| 677 |
if spec.policy.nicTeaming.notifySwitches is None: |
| 678 |
host_results['notify_switches'] = "No override" |
| 679 |
else: |
| 680 |
host_results['notify_switches'] = self.teaming_notify_switches |
| 681 |
if spec.policy.nicTeaming.notifySwitches is not None: |
| 682 |
if self.teaming_notify_switches is not None: |
| 683 |
if spec.policy.nicTeaming.notifySwitches is not self.teaming_notify_switches: |
| 684 |
changed = True |
| 685 |
changed_list.append("Notify switches") |
| 686 |
host_results['notify_switches_previous'] = spec.policy.nicTeaming.notifySwitches |
| 687 |
spec.policy.nicTeaming.notifySwitches = self.teaming_notify_switches |
| 688 |
else: |
| 689 |
changed = True |
| 690 |
changed_list.append("Notify switches") |
| 691 |
host_results['notify_switches_previous'] = spec.policy.nicTeaming.notifySwitches |
| 692 |
spec.policy.nicTeaming.notifySwitches = None |
| 693 |
else: |
| 694 |
if self.teaming_notify_switches is not None: |
| 695 |
changed = True |
| 696 |
changed_list.append("Notify switches") |
| 697 |
host_results['notify_switches_previous'] = "No override" |
| 698 |
spec.policy.nicTeaming.notifySwitches = self.teaming_notify_switches |
| 699 |
# Check failback |
| 700 |
if spec.policy.nicTeaming.rollingOrder is None: |
| 701 |
host_results['failback'] = "No override" |
| 702 |
else: |
| 703 |
host_results['failback'] = self.teaming_failback |
| 704 |
if spec.policy.nicTeaming.rollingOrder is not None: |
| 705 |
if self.teaming_failback is not None: |
| 706 |
# this option is called 'failback' in the vSphere Client |
| 707 |
# rollingOrder also uses the opposite value displayed in the client |
| 708 |
if spec.policy.nicTeaming.rollingOrder is self.teaming_failback: |
| 709 |
changed = True |
| 710 |
changed_list.append("Failback") |
| 711 |
host_results['failback_previous'] = not spec.policy.nicTeaming.rollingOrder |
| 712 |
spec.policy.nicTeaming.rollingOrder = not self.teaming_failback |
| 713 |
else: |
| 714 |
changed = True |
| 715 |
changed_list.append("Failback") |
| 716 |
host_results['failback_previous'] = spec.policy.nicTeaming.rollingOrder |
| 717 |
spec.policy.nicTeaming.rollingOrder = None |
| 718 |
else: |
| 719 |
if self.teaming_failback is not None: |
| 720 |
changed = True |
| 721 |
changed_list.append("Failback") |
| 722 |
host_results['failback_previous'] = "No override" |
| 723 |
spec.policy.nicTeaming.rollingOrder = not self.teaming_failback |
| 724 |
# Check teaming failover order |
| 725 |
if self.teaming_failover_order_active is None and self.teaming_failover_order_standby is None: |
| 726 |
host_results['failover_active'] = "No override" |
| 727 |
host_results['failover_standby'] = "No override" |
| 728 |
else: |
| 729 |
host_results['failover_active'] = self.teaming_failover_order_active |
| 730 |
host_results['failover_standby'] = self.teaming_failover_order_standby |
| 731 |
if spec.policy.nicTeaming.nicOrder: |
| 732 |
if self.teaming_failover_order_active or self.teaming_failover_order_standby: |
| 733 |
if spec.policy.nicTeaming.nicOrder.activeNic != self.teaming_failover_order_active: |
| 734 |
changed = True |
| 735 |
changed_list.append("Failover order active") |
| 736 |
host_results['failover_active_previous'] = spec.policy.nicTeaming.nicOrder.activeNic |
| 737 |
spec.policy.nicTeaming.nicOrder.activeNic = self.teaming_failover_order_active |
| 738 |
if spec.policy.nicTeaming.nicOrder.standbyNic != self.teaming_failover_order_standby: |
| 739 |
changed = True |
| 740 |
changed_list.append("Failover order standby") |
| 741 |
host_results['failover_standby_previous'] = spec.policy.nicTeaming.nicOrder.standbyNic |
| 742 |
spec.policy.nicTeaming.nicOrder.standbyNic = self.teaming_failover_order_standby |
| 743 |
else: |
| 744 |
spec.policy.nicTeaming.nicOrder = None |
| 745 |
changed = True |
| 746 |
changed_list.append("Failover order") |
| 747 |
if hasattr(spec.policy.nicTeaming.nicOrder, 'activeNic'): |
| 748 |
host_results['failover_active_previous'] = spec.policy.nicTeaming.nicOrder.activeNic |
| 749 |
else: |
| 750 |
host_results['failover_active_previous'] = [] |
| 751 |
if hasattr(spec.policy.nicTeaming.nicOrder, 'standbyNic'): |
| 752 |
host_results['failover_standby_previous'] = spec.policy.nicTeaming.nicOrder.standbyNic |
| 753 |
else: |
| 754 |
host_results['failover_standby_previous'] = [] |
| 755 |
else: |
| 756 |
if self.teaming_failover_order_active or self.teaming_failover_order_standby: |
| 757 |
changed = True |
| 758 |
changed_list.append("Failover order") |
| 759 |
host_results['failover_active_previous'] = "No override" |
| 760 |
host_results['failover_standby_previous'] = "No override" |
| 761 |
spec.policy.nicTeaming.nicOrder = self.create_nic_order_policy() |
| 762 |
# Check teaming failure detection |
| 763 |
if self.teaming_failure_detection is None: |
| 764 |
host_results['failure_detection'] = "No override" |
| 765 |
else: |
| 766 |
host_results['failure_detection'] = self.teaming_failure_detection |
| 767 |
if spec.policy.nicTeaming.failureCriteria and spec.policy.nicTeaming.failureCriteria.checkBeacon is not None: |
| 768 |
if self.teaming_failure_detection == "link_status_only": |
| 769 |
if spec.policy.nicTeaming.failureCriteria.checkBeacon is True: |
| 770 |
changed = True |
| 771 |
changed_list.append("Network failure detection") |
| 772 |
host_results['failure_detection_previous'] = "beacon_probing" |
| 773 |
spec.policy.nicTeaming.failureCriteria.checkBeacon = False |
| 774 |
elif self.teaming_failure_detection == "beacon_probing": |
| 775 |
if spec.policy.nicTeaming.failureCriteria.checkBeacon is False: |
| 776 |
changed = True |
| 777 |
changed_list.append("Network failure detection") |
| 778 |
host_results['failure_detection_previous'] = "link_status_only" |
| 779 |
spec.policy.nicTeaming.failureCriteria.checkBeacon = True |
| 780 |
elif spec.policy.nicTeaming.failureCriteria.checkBeacon is not None: |
| 781 |
changed = True |
| 782 |
changed_list.append("Network failure detection") |
| 783 |
host_results['failure_detection_previous'] = spec.policy.nicTeaming.failureCriteria.checkBeacon |
| 784 |
spec.policy.nicTeaming.failureCriteria = None |
| 785 |
else: |
| 786 |
if self.teaming_failure_detection: |
| 787 |
spec.policy.nicTeaming.failureCriteria = self.create_nic_failure_policy() |
| 788 |
changed = True |
| 789 |
changed_list.append("Network failure detection") |
| 790 |
host_results['failure_detection_previous'] = "No override" |
| 791 |
else: |
| 792 |
spec.policy.nicTeaming = self.create_teaming_policy() |
| 793 |
if spec.policy.nicTeaming: |
| 794 |
changed = True |
| 795 |
changed_list.append("Teaming and failover") |
| 796 |
host_results['load_balancing_previous'] = "No override" |
| 797 |
host_results['notify_switches_previous'] = "No override" |
| 798 |
host_results['failback_previous'] = "No override" |
| 799 |
host_results['failover_active_previous'] = "No override" |
| 800 |
host_results['failover_standby_previous'] = "No override" |
| 801 |
host_results['failure_detection_previous'] = "No override" |
| 802 |
|
| 803 |
if changed: |
| 804 |
if self.module.check_mode: |
| 805 |
changed_suffix = ' would be changed' |
| 806 |
else: |
| 807 |
changed_suffix = ' changed' |
| 808 |
if len(changed_list) > 2: |
| 809 |
message = ', '.join(changed_list[:-1]) + ', and ' + str(changed_list[-1]) |
| 810 |
elif len(changed_list) == 2: |
| 811 |
message = ' and '.join(changed_list) |
| 812 |
elif len(changed_list) == 1: |
| 813 |
message = changed_list[0] |
| 814 |
message += changed_suffix |
| 815 |
if not self.module.check_mode: |
| 816 |
try: |
| 817 |
host_system.configManager.networkSystem.UpdatePortGroup( |
| 818 |
pgName=self.portgroup, |
| 819 |
portgrp=spec |
| 820 |
) |
| 821 |
except vim.fault.AlreadyExists as already_exists: |
| 822 |
self.module.fail_json( |
| 823 |
msg="Failed to update Portgroup as it would conflict with an existing port group: %s" % |
| 824 |
to_native(already_exists.msg) |
| 825 |
) |
| 826 |
except vim.fault.NotFound as not_found: |
| 827 |
self.module.fail_json( |
| 828 |
msg="Failed to update Portgroup as vSwitch was not found: %s" % |
| 829 |
to_native(not_found.msg) |
| 830 |
) |
| 831 |
except vim.fault.HostConfigFault as host_config_fault: |
| 832 |
self.module.fail_json( |
| 833 |
msg="Failed to update Portgroup due to host system configuration failure : %s" % |
| 834 |
to_native(host_config_fault.msg) |
| 835 |
) |
| 836 |
except vmodl.fault.InvalidArgument as invalid_argument: |
| 837 |
self.module.fail_json( |
| 838 |
msg="Failed to update Port Group '%s', this can be due to either of following :" |
| 839 |
" 1. VLAN id was not correct as per specifications, 2. Network policy is invalid : %s" % |
| 840 |
(self.portgroup, to_native(invalid_argument.msg)) |
| 841 |
) |
| 842 |
else: |
| 843 |
message = "Port Group already configured properly" |
| 844 |
host_results['changed'] = changed |
| 845 |
host_results['msg'] = message |
| 846 |
host_results['portgroup'] = self.portgroup |
| 847 |
host_results['vswitch'] = self.switch |
| 848 |
|
| 849 |
return changed, host_results |
| 850 |
|
| 851 |
def create_network_policy(self): |
| 852 |
""" |
| 853 |
Create a Network Policy |
| 854 |
Returns: Network Policy object |
| 855 |
""" |
| 856 |
security_policy = None |
| 857 |
shaping_policy = None |
| 858 |
teaming_policy = None |
| 859 |
|
| 860 |
# Only configure security policy if an option is defined |
| 861 |
if not all(option is None for option in [self.sec_promiscuous_mode, |
| 862 |
self.sec_mac_changes, |
| 863 |
self.sec_forged_transmits]): |
| 864 |
security_policy = self.create_security_policy() |
| 865 |
if self.ts_enabled: |
| 866 |
shaping_policy = self.create_shaping_policy() |
| 867 |
teaming_policy = self.create_teaming_policy() |
| 868 |
|
| 869 |
network_policy = vim.host.NetworkPolicy( |
| 870 |
security=security_policy, |
| 871 |
nicTeaming=teaming_policy, |
| 872 |
shapingPolicy=shaping_policy |
| 873 |
) |
| 874 |
|
| 875 |
return network_policy |
| 876 |
|
| 877 |
def create_security_policy(self): |
| 878 |
""" |
| 879 |
Create a Security Policy |
| 880 |
Returns: Security Policy object |
| 881 |
""" |
| 882 |
security_policy = vim.host.NetworkPolicy.SecurityPolicy() |
| 883 |
security_policy.allowPromiscuous = self.sec_promiscuous_mode |
| 884 |
security_policy.macChanges = self.sec_mac_changes |
| 885 |
security_policy.forgedTransmits = self.sec_forged_transmits |
| 886 |
return security_policy |
| 887 |
|
| 888 |
def create_shaping_policy(self): |
| 889 |
""" |
| 890 |
Create a Traffic Shaping Policy |
| 891 |
Returns: Traffic Shaping Policy object |
| 892 |
""" |
| 893 |
shaping_policy = vim.host.NetworkPolicy.TrafficShapingPolicy() |
| 894 |
shaping_policy.enabled = self.ts_enabled |
| 895 |
shaping_policy.averageBandwidth = self.ts_average_bandwidth * 1000 |
| 896 |
shaping_policy.peakBandwidth = self.ts_peak_bandwidth * 1000 |
| 897 |
shaping_policy.burstSize = self.ts_burst_size * 1024 |
| 898 |
return shaping_policy |
| 899 |
|
| 900 |
def create_teaming_policy(self): |
| 901 |
""" |
| 902 |
Create a NIC Teaming Policy |
| 903 |
Returns: NIC Teaming Policy object |
| 904 |
""" |
| 905 |
# Only configure teaming policy if an option is defined |
| 906 |
if not all(option is None for option in [self.teaming_load_balancing, |
| 907 |
self.teaming_failure_detection, |
| 908 |
self.teaming_notify_switches, |
| 909 |
self.teaming_failback, |
| 910 |
self.teaming_failover_order_active, |
| 911 |
self.teaming_failover_order_standby]): |
| 912 |
teaming_policy = vim.host.NetworkPolicy.NicTeamingPolicy() |
| 913 |
teaming_policy.policy = self.teaming_load_balancing |
| 914 |
teaming_policy.reversePolicy = True |
| 915 |
teaming_policy.notifySwitches = self.teaming_notify_switches |
| 916 |
if self.teaming_failback is None: |
| 917 |
teaming_policy.rollingOrder = None |
| 918 |
else: |
| 919 |
teaming_policy.rollingOrder = not self.teaming_failback |
| 920 |
if self.teaming_failover_order_active is None and self.teaming_failover_order_standby is None: |
| 921 |
teaming_policy.nicOrder = None |
| 922 |
else: |
| 923 |
teaming_policy.nicOrder = self.create_nic_order_policy() |
| 924 |
if self.teaming_failure_detection is None: |
| 925 |
teaming_policy.failureCriteria = None |
| 926 |
else: |
| 927 |
teaming_policy.failureCriteria = self.create_nic_failure_policy() |
| 928 |
return teaming_policy |
| 929 |
return None |
| 930 |
|
| 931 |
def create_nic_order_policy(self): |
| 932 |
""" |
| 933 |
Create a NIC order Policy |
| 934 |
Returns: NIC order Policy object |
| 935 |
""" |
| 936 |
for active_nic in self.teaming_failover_order_active: |
| 937 |
if active_nic not in self.switch_object.spec.bridge.nicDevice: |
| 938 |
self.module.fail_json( |
| 939 |
msg="NIC '%s' (active) is not configured on vSwitch '%s'" % (active_nic, self.switch) |
| 940 |
) |
| 941 |
for standby_nic in self.teaming_failover_order_standby: |
| 942 |
if standby_nic not in self.switch_object.spec.bridge.nicDevice: |
| 943 |
self.module.fail_json( |
| 944 |
msg="NIC '%s' (standby) is not configured on vSwitch '%s'" % (standby_nic, self.switch) |
| 945 |
) |
| 946 |
nic_order = vim.host.NetworkPolicy.NicOrderPolicy() |
| 947 |
nic_order.activeNic = self.teaming_failover_order_active |
| 948 |
nic_order.standbyNic = self.teaming_failover_order_standby |
| 949 |
return nic_order |
| 950 |
|
| 951 |
def create_nic_failure_policy(self): |
| 952 |
""" |
| 953 |
Create a NIC Failure Criteria Policy |
| 954 |
Returns: NIC Failure Criteria Policy object |
| 955 |
""" |
| 956 |
failure_criteria = vim.host.NetworkPolicy.NicFailureCriteria() |
| 957 |
if self.teaming_failure_detection == "link_status_only": |
| 958 |
failure_criteria.checkBeacon = False |
| 959 |
elif self.teaming_failure_detection == "beacon_probing": |
| 960 |
failure_criteria.checkBeacon = True |
| 961 |
elif self.teaming_failure_detection is None: |
| 962 |
failure_criteria = None |
| 963 |
# The following properties are deprecated since VI API 5.1. Default values are used |
| 964 |
failure_criteria.fullDuplex = False |
| 965 |
failure_criteria.percentage = 0 |
| 966 |
failure_criteria.checkErrorPercent = False |
| 967 |
failure_criteria.checkDuplex = False |
| 968 |
failure_criteria.speed = 10 |
| 969 |
failure_criteria.checkSpeed = 'minimum' |
| 970 |
return failure_criteria |
| 971 |
|
| 972 |
|
| 973 |
def main(): |
| 974 |
"""Main""" |
| 975 |
argument_spec = base_argument_spec() |
| 976 |
argument_spec.update(dict( |
| 977 |
portgroup=dict(type='str', required=True, aliases=['portgroup_name']), |
| 978 |
switch=dict(type='str', required=True, aliases=['switch_name', 'vswitch']), |
| 979 |
vlan_id=dict(type='int', required=False, default=0, aliases=['vlan']), |
| 980 |
hosts=dict(type='list', aliases=['esxi_hostname'], elements='str'), |
| 981 |
cluster_name=dict(type='str', aliases=['cluster']), |
| 982 |
state=dict(type='str', choices=['present', 'absent'], default='present'), |
| 983 |
security=dict( |
| 984 |
type='dict', |
| 985 |
options=dict( |
| 986 |
promiscuous_mode=dict(type='bool'), |
| 987 |
forged_transmits=dict(type='bool'), |
| 988 |
mac_changes=dict(type='bool'), |
| 989 |
), |
| 990 |
aliases=['security_policy', 'network_policy'] |
| 991 |
), |
| 992 |
traffic_shaping=dict( |
| 993 |
type='dict', |
| 994 |
options=dict( |
| 995 |
enabled=dict(type='bool'), |
| 996 |
average_bandwidth=dict(type='int'), |
| 997 |
peak_bandwidth=dict(type='int'), |
| 998 |
burst_size=dict(type='int'), |
| 999 |
), |
| 1000 |
), |
| 1001 |
teaming=dict( |
| 1002 |
type='dict', |
| 1003 |
options=dict( |
| 1004 |
load_balancing=dict( |
| 1005 |
type='str', |
| 1006 |
choices=[ |
| 1007 |
'loadbalance_ip', |
| 1008 |
'loadbalance_srcmac', |
| 1009 |
'loadbalance_srcid', |
| 1010 |
'failover_explicit', |
| 1011 |
], |
| 1012 |
aliases=['load_balance_policy'], |
| 1013 |
), |
| 1014 |
network_failure_detection=dict( |
| 1015 |
type='str', |
| 1016 |
choices=['link_status_only', 'beacon_probing'] |
| 1017 |
), |
| 1018 |
notify_switches=dict(type='bool'), |
| 1019 |
failback=dict(type='bool'), |
| 1020 |
active_adapters=dict(type='list', elements='str'), |
| 1021 |
standby_adapters=dict(type='list', elements='str'), |
| 1022 |
), |
| 1023 |
aliases=['teaming_policy'] |
| 1024 |
), |
| 1025 |
)) |
| 1026 |
|
| 1027 |
module = AnsibleModule( |
| 1028 |
argument_spec=argument_spec, |
| 1029 |
required_one_of=[ |
| 1030 |
['cluster_name', 'hosts'], |
| 1031 |
], |
| 1032 |
supports_check_mode=True |
| 1033 |
) |
| 1034 |
|
| 1035 |
try: |
| 1036 |
host_portgroup = VMwareHostPortGroup(module) |
| 1037 |
host_portgroup.process_state() |
| 1038 |
except vmodl.RuntimeFault as runtime_fault: |
| 1039 |
module.fail_json(msg=to_native(runtime_fault.msg)) |
| 1040 |
except vmodl.MethodFault as method_fault: |
| 1041 |
module.fail_json(msg=to_native(method_fault.msg)) |
| 1042 |
except Exception as e: |
| 1043 |
module.fail_json(msg=to_native(e)) |
| 1044 |
|
| 1045 |
|
| 1046 |
if __name__ == '__main__': |
| 1047 |
main() |