| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2015, Joseph Callen <jcallen () csc.com> |
| 5 |
# Copyright: (c) 2017-18, Ansible Project |
| 6 |
# Copyright: (c) 2017-18, 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_vmkernel |
| 18 |
short_description: Manages a VMware VMkernel Adapter of an ESXi host. |
| 19 |
description: |
| 20 |
- This module can be used to manage the VMKernel adapters / VMKernel network interfaces of an ESXi host. |
| 21 |
- The module assumes that the host is already configured with the Port Group in case of a vSphere Standard Switch (vSS). |
| 22 |
- The module assumes that the host is already configured with the Distributed Port Group in case of a vSphere Distributed Switch (vDS). |
| 23 |
- The module automatically migrates the VMKernel adapter from vSS to vDS or vice versa if present. |
| 24 |
author: |
| 25 |
- Joseph Callen (@jcpowermac) |
| 26 |
- Russell Teague (@mtnbikenc) |
| 27 |
- Abhijeet Kasurde (@Akasurde) |
| 28 |
- Christian Kotte (@ckotte) |
| 29 |
- Nina Loser (@Nina2244) |
| 30 |
notes: |
| 31 |
- The option O(device) need to be used with DHCP because otherwise it's not possible to check if a VMkernel device is already present |
| 32 |
- You can only change from DHCP to static, and vSS to vDS, or vice versa, in one step, without creating a new device, with O(device) specified. |
| 33 |
- You can only create the VMKernel adapter on a vDS if authenticated to vCenter and not if authenticated to ESXi. |
| 34 |
options: |
| 35 |
vswitch_name: |
| 36 |
description: |
| 37 |
- The name of the vSwitch where to add the VMKernel interface. |
| 38 |
- Required parameter only if O(state=present). |
| 39 |
- Optional parameter from version 2.5 and onwards. |
| 40 |
type: str |
| 41 |
aliases: ['vswitch'] |
| 42 |
dvswitch_name: |
| 43 |
description: |
| 44 |
- The name of the vSphere Distributed Switch (vDS) where to add the VMKernel interface. |
| 45 |
- Required parameter only if O(state=present). |
| 46 |
- Optional parameter from version 2.8 and onwards. |
| 47 |
type: str |
| 48 |
aliases: ['dvswitch'] |
| 49 |
portgroup_name: |
| 50 |
description: |
| 51 |
- The name of the port group for the VMKernel interface. |
| 52 |
required: true |
| 53 |
aliases: ['portgroup'] |
| 54 |
type: str |
| 55 |
network: |
| 56 |
description: |
| 57 |
- A dictionary of network details. |
| 58 |
suboptions: |
| 59 |
type: |
| 60 |
type: str |
| 61 |
description: |
| 62 |
- Type of IP assignment. |
| 63 |
choices: [ 'static', 'dhcp' ] |
| 64 |
default: 'static' |
| 65 |
ip_address: |
| 66 |
type: str |
| 67 |
description: |
| 68 |
- Static IPv4 address. |
| 69 |
- Required if O(network.type=static) and O(state=present). |
| 70 |
subnet_mask: |
| 71 |
type: str |
| 72 |
description: |
| 73 |
- Static IPv4 netmask. |
| 74 |
- Required if O(network.type=static) and O(state=present). |
| 75 |
default_gateway: |
| 76 |
type: str |
| 77 |
description: Default IPv4 gateway (Override default gateway for this adapter). |
| 78 |
tcpip_stack: |
| 79 |
type: str |
| 80 |
description: |
| 81 |
- The TCP/IP stack for the VMKernel interface. |
| 82 |
choices: [ 'default', 'provisioning', 'vmotion', 'vxlan' ] |
| 83 |
default: 'default' |
| 84 |
type: dict |
| 85 |
default: { |
| 86 |
type: 'static', |
| 87 |
tcpip_stack: 'default', |
| 88 |
} |
| 89 |
mtu: |
| 90 |
description: |
| 91 |
- The MTU for the VMKernel interface. |
| 92 |
- The default value of 1500 is valid from version 2.5 and onwards. |
| 93 |
default: 1500 |
| 94 |
type: int |
| 95 |
device: |
| 96 |
description: |
| 97 |
- Search VMkernel adapter by device name. |
| 98 |
- The parameter is required only in case of O(network.type=dhcp). |
| 99 |
type: str |
| 100 |
enable_vsan: |
| 101 |
description: |
| 102 |
- Enable VSAN traffic on the VMKernel adapter. |
| 103 |
- This option is only allowed if the default TCP/IP stack is used. |
| 104 |
type: bool |
| 105 |
default: false |
| 106 |
enable_vmotion: |
| 107 |
description: |
| 108 |
- Enable vMotion traffic on the VMKernel adapter. |
| 109 |
- This option is only allowed if the default TCP/IP stack is used. |
| 110 |
- You cannot enable vMotion on an additional adapter if you already have an adapter with the vMotion TCP/IP stack configured. |
| 111 |
type: bool |
| 112 |
default: false |
| 113 |
enable_mgmt: |
| 114 |
description: |
| 115 |
- Enable Management traffic on the VMKernel adapter. |
| 116 |
- This option is only allowed if the default TCP/IP stack is used. |
| 117 |
type: bool |
| 118 |
default: false |
| 119 |
enable_ft: |
| 120 |
description: |
| 121 |
- Enable Fault Tolerance traffic on the VMKernel adapter. |
| 122 |
- This option is only allowed if the default TCP/IP stack is used. |
| 123 |
type: bool |
| 124 |
default: false |
| 125 |
enable_provisioning: |
| 126 |
description: |
| 127 |
- Enable Provisioning traffic on the VMKernel adapter. |
| 128 |
- This option is only allowed if the default TCP/IP stack is used. |
| 129 |
type: bool |
| 130 |
default: false |
| 131 |
enable_replication: |
| 132 |
description: |
| 133 |
- Enable vSphere Replication traffic on the VMKernel adapter. |
| 134 |
- This option is only allowed if the default TCP/IP stack is used. |
| 135 |
type: bool |
| 136 |
default: false |
| 137 |
enable_replication_nfc: |
| 138 |
description: |
| 139 |
- Enable vSphere Replication NFC traffic on the VMKernel adapter. |
| 140 |
- This option is only allowed if the default TCP/IP stack is used. |
| 141 |
type: bool |
| 142 |
default: false |
| 143 |
enable_backup_nfc: |
| 144 |
version_added: 4.2.0 |
| 145 |
description: |
| 146 |
- Enable vSphere Backup NFC traffic on the VMKernel adapter. |
| 147 |
- This option is only allowed if the default TCP/IP stack is used. |
| 148 |
type: bool |
| 149 |
default: false |
| 150 |
state: |
| 151 |
description: |
| 152 |
- If set to V(present), the VMKernel adapter will be created with the given specifications. |
| 153 |
- If set to V(absent), the VMKernel adapter will be removed. |
| 154 |
- If set to V(present) and VMKernel adapter exists, the configurations will be updated. |
| 155 |
choices: [ present, absent ] |
| 156 |
default: present |
| 157 |
type: str |
| 158 |
esxi_hostname: |
| 159 |
description: |
| 160 |
- Name of ESXi host to which VMKernel is to be managed. |
| 161 |
required: true |
| 162 |
type: str |
| 163 |
extends_documentation_fragment: |
| 164 |
- vmware.vmware.base_options |
| 165 |
|
| 166 |
''' |
| 167 |
|
| 168 |
EXAMPLES = r''' |
| 169 |
- name: Add Management vmkernel port using static network type |
| 170 |
community.vmware.vmware_vmkernel: |
| 171 |
hostname: '{{ esxi_hostname }}' |
| 172 |
username: '{{ esxi_username }}' |
| 173 |
password: '{{ esxi_password }}' |
| 174 |
esxi_hostname: '{{ esxi_hostname }}' |
| 175 |
vswitch_name: vSwitch0 |
| 176 |
portgroup_name: PG_0001 |
| 177 |
network: |
| 178 |
type: 'static' |
| 179 |
ip_address: 192.168.127.10 |
| 180 |
subnet_mask: 255.255.255.0 |
| 181 |
state: present |
| 182 |
enable_mgmt: true |
| 183 |
delegate_to: localhost |
| 184 |
|
| 185 |
- name: Add Management vmkernel port using DHCP network type |
| 186 |
community.vmware.vmware_vmkernel: |
| 187 |
hostname: '{{ esxi_hostname }}' |
| 188 |
username: '{{ esxi_username }}' |
| 189 |
password: '{{ esxi_password }}' |
| 190 |
esxi_hostname: '{{ esxi_hostname }}' |
| 191 |
vswitch_name: vSwitch0 |
| 192 |
portgroup_name: PG_0002 |
| 193 |
state: present |
| 194 |
network: |
| 195 |
type: 'dhcp' |
| 196 |
enable_mgmt: true |
| 197 |
delegate_to: localhost |
| 198 |
|
| 199 |
- name: Change IP allocation from static to dhcp |
| 200 |
community.vmware.vmware_vmkernel: |
| 201 |
hostname: '{{ esxi_hostname }}' |
| 202 |
username: '{{ esxi_username }}' |
| 203 |
password: '{{ esxi_password }}' |
| 204 |
esxi_hostname: '{{ esxi_hostname }}' |
| 205 |
vswitch_name: vSwitch0 |
| 206 |
portgroup_name: PG_0002 |
| 207 |
state: present |
| 208 |
device: vmk1 |
| 209 |
network: |
| 210 |
type: 'dhcp' |
| 211 |
enable_mgmt: true |
| 212 |
delegate_to: localhost |
| 213 |
|
| 214 |
- name: Delete VMkernel port |
| 215 |
community.vmware.vmware_vmkernel: |
| 216 |
hostname: '{{ esxi_hostname }}' |
| 217 |
username: '{{ esxi_username }}' |
| 218 |
password: '{{ esxi_password }}' |
| 219 |
esxi_hostname: '{{ esxi_hostname }}' |
| 220 |
vswitch_name: vSwitch0 |
| 221 |
portgroup_name: PG_0002 |
| 222 |
state: absent |
| 223 |
delegate_to: localhost |
| 224 |
|
| 225 |
- name: Add Management vmkernel port to Distributed Switch |
| 226 |
community.vmware.vmware_vmkernel: |
| 227 |
hostname: '{{ vcenter_hostname }}' |
| 228 |
username: '{{ vcenter_username }}' |
| 229 |
password: '{{ vcenter_password }}' |
| 230 |
esxi_hostname: '{{ esxi_hostname }}' |
| 231 |
dvswitch_name: dvSwitch1 |
| 232 |
portgroup_name: dvPG_0001 |
| 233 |
network: |
| 234 |
type: 'static' |
| 235 |
ip_address: 192.168.127.10 |
| 236 |
subnet_mask: 255.255.255.0 |
| 237 |
state: present |
| 238 |
enable_mgmt: true |
| 239 |
delegate_to: localhost |
| 240 |
|
| 241 |
- name: Add vMotion vmkernel port with vMotion TCP/IP stack |
| 242 |
community.vmware.vmware_vmkernel: |
| 243 |
hostname: '{{ vcenter_hostname }}' |
| 244 |
username: '{{ vcenter_username }}' |
| 245 |
password: '{{ vcenter_password }}' |
| 246 |
esxi_hostname: '{{ esxi_hostname }}' |
| 247 |
dvswitch_name: dvSwitch1 |
| 248 |
portgroup_name: dvPG_0001 |
| 249 |
network: |
| 250 |
type: 'static' |
| 251 |
ip_address: 192.168.127.10 |
| 252 |
subnet_mask: 255.255.255.0 |
| 253 |
tcpip_stack: vmotion |
| 254 |
state: present |
| 255 |
delegate_to: localhost |
| 256 |
''' |
| 257 |
|
| 258 |
RETURN = r''' |
| 259 |
result: |
| 260 |
description: metadata about VMKernel name |
| 261 |
returned: always |
| 262 |
type: dict |
| 263 |
sample: { |
| 264 |
"changed": false, |
| 265 |
"msg": "VMkernel Adapter already configured properly", |
| 266 |
"device": "vmk1", |
| 267 |
"ipv4": "static", |
| 268 |
"ipv4_gw": "No override", |
| 269 |
"ipv4_ip": "192.168.1.15", |
| 270 |
"ipv4_sm": "255.255.255.0", |
| 271 |
"mtu": 9000, |
| 272 |
"services": "vMotion", |
| 273 |
"switch": "vDS" |
| 274 |
} |
| 275 |
''' |
| 276 |
|
| 277 |
try: |
| 278 |
from pyVmomi import vim, vmodl |
| 279 |
except ImportError: |
| 280 |
pass |
| 281 |
|
| 282 |
from ansible.module_utils.basic import AnsibleModule |
| 283 |
from ansible_collections.community.vmware.plugins.module_utils.vmware import ( |
| 284 |
PyVmomi, TaskError, wait_for_task, find_dvspg_by_name, find_dvs_by_name, get_all_objs |
| 285 |
) |
| 286 |
from ansible_collections.vmware.vmware.plugins.module_utils.argument_spec import base_argument_spec |
| 287 |
from ansible.module_utils.common.text.converters import to_native |
| 288 |
|
| 289 |
|
| 290 |
class PyVmomiHelper(PyVmomi): |
| 291 |
"""Class to manage VMkernel configuration of an ESXi host system""" |
| 292 |
|
| 293 |
def __init__(self, module): |
| 294 |
super(PyVmomiHelper, self).__init__(module) |
| 295 |
if self.params['network']: |
| 296 |
self.network_type = self.params['network'].get('type') |
| 297 |
self.ip_address = self.params['network'].get('ip_address', None) |
| 298 |
self.subnet_mask = self.params['network'].get('subnet_mask', None) |
| 299 |
self.default_gateway = self.params['network'].get('default_gateway', None) |
| 300 |
self.tcpip_stack = self.params['network'].get('tcpip_stack') |
| 301 |
self.device = self.params['device'] |
| 302 |
if self.network_type == 'dhcp' and not self.device: |
| 303 |
module.fail_json(msg="device is a required parameter when network type is set to 'dhcp'") |
| 304 |
self.mtu = self.params['mtu'] |
| 305 |
self.enable_vsan = self.params['enable_vsan'] |
| 306 |
self.enable_vmotion = self.params['enable_vmotion'] |
| 307 |
self.enable_mgmt = self.params['enable_mgmt'] |
| 308 |
self.enable_ft = self.params['enable_ft'] |
| 309 |
self.enable_provisioning = self.params['enable_provisioning'] |
| 310 |
self.enable_replication = self.params['enable_replication'] |
| 311 |
self.enable_replication_nfc = self.params['enable_replication_nfc'] |
| 312 |
self.enable_backup_nfc = self.params['enable_backup_nfc'] |
| 313 |
|
| 314 |
self.vswitch_name = self.params['vswitch_name'] |
| 315 |
self.vds_name = self.params['dvswitch_name'] |
| 316 |
self.port_group_name = self.params['portgroup_name'] |
| 317 |
|
| 318 |
self.esxi_host_name = self.params['esxi_hostname'] |
| 319 |
hosts = self.get_all_host_objs(esxi_host_name=self.esxi_host_name) |
| 320 |
if hosts: |
| 321 |
self.esxi_host_obj = hosts[0] |
| 322 |
else: |
| 323 |
self.module.fail_json( |
| 324 |
msg="Failed to get details of ESXi server. Please specify esxi_hostname." |
| 325 |
) |
| 326 |
|
| 327 |
if self.network_type == 'static': |
| 328 |
if self.module.params['state'] == 'absent': |
| 329 |
pass |
| 330 |
elif not self.ip_address: |
| 331 |
module.fail_json(msg="ip_address is a required parameter when network type is set to 'static'") |
| 332 |
elif not self.subnet_mask: |
| 333 |
module.fail_json(msg="subnet_mask is a required parameter when network type is set to 'static'") |
| 334 |
|
| 335 |
# find Port Group |
| 336 |
if self.vswitch_name: |
| 337 |
self.port_group_obj = self.get_port_group_by_name( |
| 338 |
host_system=self.esxi_host_obj, |
| 339 |
portgroup_name=self.port_group_name, |
| 340 |
vswitch_name=self.vswitch_name |
| 341 |
) |
| 342 |
if not self.port_group_obj: |
| 343 |
module.fail_json(msg="Portgroup '%s' not found on vSS '%s'" % (self.port_group_name, self.vswitch_name)) |
| 344 |
elif self.vds_name: |
| 345 |
self.dv_switch_obj = find_dvs_by_name(self.content, self.vds_name) |
| 346 |
if not self.dv_switch_obj: |
| 347 |
module.fail_json(msg="vDS '%s' not found" % self.vds_name) |
| 348 |
self.port_group_obj = find_dvspg_by_name(self.dv_switch_obj, self.port_group_name) |
| 349 |
if not self.port_group_obj: |
| 350 |
module.fail_json(msg="Portgroup '%s' not found on vDS '%s'" % (self.port_group_name, self.vds_name)) |
| 351 |
|
| 352 |
# find VMkernel Adapter |
| 353 |
if self.device: |
| 354 |
self.vnic = self.get_vmkernel_by_device(device_name=self.device) |
| 355 |
else: |
| 356 |
# config change (e.g. DHCP to static, or vice versa); doesn't work with virtual port change |
| 357 |
self.vnic = self.get_vmkernel_by_portgroup_new(port_group_name=self.port_group_name) |
| 358 |
if not self.vnic and self.network_type == 'static': |
| 359 |
# vDS to vSS or vSS to vSS (static IP) |
| 360 |
self.vnic = self.get_vmkernel_by_ip(ip_address=self.ip_address) |
| 361 |
|
| 362 |
def get_port_group_by_name(self, host_system, portgroup_name, vswitch_name): |
| 363 |
""" |
| 364 |
Get specific port group by given name |
| 365 |
Args: |
| 366 |
host_system: Name of Host System |
| 367 |
portgroup_name: Name of Port Group |
| 368 |
vswitch_name: Name of the vSwitch |
| 369 |
|
| 370 |
Returns: List of port groups by given specifications |
| 371 |
|
| 372 |
""" |
| 373 |
portgroups = self.get_all_port_groups_by_host(host_system=host_system) |
| 374 |
|
| 375 |
for portgroup in portgroups: |
| 376 |
if portgroup.spec.vswitchName == vswitch_name and portgroup.spec.name == portgroup_name: |
| 377 |
return portgroup |
| 378 |
return None |
| 379 |
|
| 380 |
def ensure(self): |
| 381 |
""" |
| 382 |
Manage internal VMKernel management |
| 383 |
Returns: NA |
| 384 |
|
| 385 |
""" |
| 386 |
host_vmk_states = { |
| 387 |
'absent': { |
| 388 |
'present': self.host_vmk_delete, |
| 389 |
'absent': self.host_vmk_unchange, |
| 390 |
}, |
| 391 |
'present': { |
| 392 |
'present': self.host_vmk_update, |
| 393 |
'absent': self.host_vmk_create, |
| 394 |
} |
| 395 |
} |
| 396 |
|
| 397 |
try: |
| 398 |
host_vmk_states[self.module.params['state']][self.check_state()]() |
| 399 |
except vmodl.RuntimeFault as runtime_fault: |
| 400 |
self.module.fail_json(msg=to_native(runtime_fault.msg)) |
| 401 |
except vmodl.MethodFault as method_fault: |
| 402 |
self.module.fail_json(msg=to_native(method_fault.msg)) |
| 403 |
|
| 404 |
def get_vmkernel_by_portgroup_new(self, port_group_name=None): |
| 405 |
""" |
| 406 |
Check if vmkernel available or not |
| 407 |
Args: |
| 408 |
port_group_name: name of port group |
| 409 |
|
| 410 |
Returns: vmkernel managed object if vmkernel found, false if not |
| 411 |
|
| 412 |
""" |
| 413 |
for vnic in self.esxi_host_obj.config.network.vnic: |
| 414 |
# check if it's a vSS Port Group |
| 415 |
if vnic.spec.portgroup == port_group_name: |
| 416 |
return vnic |
| 417 |
# check if it's a vDS Port Group |
| 418 |
try: |
| 419 |
if vnic.spec.distributedVirtualPort.portgroupKey == self.port_group_obj.key: |
| 420 |
return vnic |
| 421 |
except AttributeError: |
| 422 |
pass |
| 423 |
return False |
| 424 |
|
| 425 |
def get_vmkernel_by_ip(self, ip_address): |
| 426 |
""" |
| 427 |
Check if vmkernel available or not |
| 428 |
Args: |
| 429 |
ip_address: IP address of vmkernel device |
| 430 |
|
| 431 |
Returns: vmkernel managed object if vmkernel found, false if not |
| 432 |
|
| 433 |
""" |
| 434 |
for vnic in self.esxi_host_obj.config.network.vnic: |
| 435 |
if vnic.spec.ip.ipAddress == ip_address: |
| 436 |
return vnic |
| 437 |
return None |
| 438 |
|
| 439 |
def get_vmkernel_by_device(self, device_name): |
| 440 |
""" |
| 441 |
Check if vmkernel available or not |
| 442 |
Args: |
| 443 |
device_name: name of vmkernel device |
| 444 |
|
| 445 |
Returns: vmkernel managed object if vmkernel found, false if not |
| 446 |
|
| 447 |
""" |
| 448 |
for vnic in self.esxi_host_obj.config.network.vnic: |
| 449 |
if vnic.device == device_name: |
| 450 |
return vnic |
| 451 |
return None |
| 452 |
|
| 453 |
def check_state(self): |
| 454 |
""" |
| 455 |
Check internal state management |
| 456 |
Returns: Present if found and absent if not found |
| 457 |
|
| 458 |
""" |
| 459 |
return 'present' if self.vnic else 'absent' |
| 460 |
|
| 461 |
def host_vmk_delete(self): |
| 462 |
""" |
| 463 |
Delete VMKernel |
| 464 |
Returns: NA |
| 465 |
|
| 466 |
""" |
| 467 |
results = dict(changed=False, msg='') |
| 468 |
vmk_device = self.vnic.device |
| 469 |
try: |
| 470 |
if self.module.check_mode: |
| 471 |
results['msg'] = "VMkernel Adapter would be deleted" |
| 472 |
else: |
| 473 |
self.esxi_host_obj.configManager.networkSystem.RemoveVirtualNic(vmk_device) |
| 474 |
results['msg'] = "VMkernel Adapter deleted" |
| 475 |
results['changed'] = True |
| 476 |
results['device'] = vmk_device |
| 477 |
except vim.fault.NotFound as not_found: |
| 478 |
self.module.fail_json( |
| 479 |
msg="Failed to find vmk to delete due to %s" % |
| 480 |
to_native(not_found.msg) |
| 481 |
) |
| 482 |
except vim.fault.HostConfigFault as host_config_fault: |
| 483 |
self.module.fail_json( |
| 484 |
msg="Failed to delete vmk due host config issues : %s" % |
| 485 |
to_native(host_config_fault.msg) |
| 486 |
) |
| 487 |
|
| 488 |
self.module.exit_json(**results) |
| 489 |
|
| 490 |
def host_vmk_unchange(self): |
| 491 |
""" |
| 492 |
Denote no change in VMKernel |
| 493 |
Returns: NA |
| 494 |
|
| 495 |
""" |
| 496 |
self.module.exit_json(changed=False) |
| 497 |
|
| 498 |
def host_vmk_update(self): |
| 499 |
""" |
| 500 |
Update VMKernel with given parameters |
| 501 |
Returns: NA |
| 502 |
|
| 503 |
""" |
| 504 |
changed = changed_settings = changed_vds = changed_services = \ |
| 505 |
changed_service_vmotion = changed_service_mgmt = changed_service_ft = \ |
| 506 |
changed_service_vsan = changed_service_prov = changed_service_rep = changed_service_rep_nfc = changed_service_backup_nfc = False |
| 507 |
changed_list = [] |
| 508 |
results = dict(changed=False, msg='') |
| 509 |
|
| 510 |
results['tcpip_stack'] = self.tcpip_stack |
| 511 |
net_stack_instance_key = self.get_api_net_stack_instance(self.tcpip_stack) |
| 512 |
if self.vnic.spec.netStackInstanceKey != net_stack_instance_key: |
| 513 |
self.module.fail_json(msg="The TCP/IP stack cannot be changed on an existing VMkernel adapter!") |
| 514 |
|
| 515 |
# Check MTU |
| 516 |
results['mtu'] = self.mtu |
| 517 |
if self.vnic.spec.mtu != self.mtu: |
| 518 |
changed_settings = True |
| 519 |
changed_list.append("MTU") |
| 520 |
results['mtu_previous'] = self.vnic.spec.mtu |
| 521 |
|
| 522 |
# Check IPv4 settings |
| 523 |
results['ipv4'] = self.network_type |
| 524 |
results['ipv4_ip'] = self.ip_address |
| 525 |
results['ipv4_sm'] = self.subnet_mask |
| 526 |
if self.default_gateway: |
| 527 |
results['ipv4_gw'] = self.default_gateway |
| 528 |
else: |
| 529 |
results['ipv4_gw'] = "No override" |
| 530 |
if self.vnic.spec.ip.dhcp: |
| 531 |
if self.network_type == 'static': |
| 532 |
changed_settings = True |
| 533 |
changed_list.append("IPv4 settings") |
| 534 |
results['ipv4_previous'] = "DHCP" |
| 535 |
if not self.vnic.spec.ip.dhcp: |
| 536 |
if self.network_type == 'dhcp': |
| 537 |
changed_settings = True |
| 538 |
changed_list.append("IPv4 settings") |
| 539 |
results['ipv4_previous'] = "static" |
| 540 |
elif self.network_type == 'static': |
| 541 |
if self.ip_address != self.vnic.spec.ip.ipAddress: |
| 542 |
changed_settings = True |
| 543 |
changed_list.append("IP") |
| 544 |
results['ipv4_ip_previous'] = self.vnic.spec.ip.ipAddress |
| 545 |
if self.subnet_mask != self.vnic.spec.ip.subnetMask: |
| 546 |
changed_settings = True |
| 547 |
changed_list.append("SM") |
| 548 |
results['ipv4_sm_previous'] = self.vnic.spec.ip.subnetMask |
| 549 |
if self.default_gateway: |
| 550 |
try: |
| 551 |
if self.default_gateway != self.vnic.spec.ipRouteSpec.ipRouteConfig.defaultGateway: |
| 552 |
changed_settings = True |
| 553 |
changed_list.append("GW override") |
| 554 |
results['ipv4_gw_previous'] = self.vnic.spec.ipRouteSpec.ipRouteConfig.defaultGateway |
| 555 |
except AttributeError: |
| 556 |
changed_settings = True |
| 557 |
changed_list.append("GW override") |
| 558 |
results['ipv4_gw_previous'] = "No override" |
| 559 |
else: |
| 560 |
try: |
| 561 |
if self.vnic.spec.ipRouteSpec.ipRouteConfig.defaultGateway: |
| 562 |
changed_settings = True |
| 563 |
changed_list.append("GW override") |
| 564 |
results['ipv4_gw_previous'] = self.vnic.spec.ipRouteSpec.ipRouteConfig.defaultGateway |
| 565 |
except AttributeError: |
| 566 |
pass |
| 567 |
|
| 568 |
# Check virtual port (vSS or vDS) |
| 569 |
results['portgroup'] = self.port_group_name |
| 570 |
dvs_uuid = None |
| 571 |
if self.vswitch_name: |
| 572 |
results['switch'] = self.vswitch_name |
| 573 |
try: |
| 574 |
if self.vnic.spec.distributedVirtualPort.switchUuid: |
| 575 |
changed_vds = True |
| 576 |
changed_list.append("Virtual Port") |
| 577 |
dvs_uuid = self.vnic.spec.distributedVirtualPort.switchUuid |
| 578 |
except AttributeError: |
| 579 |
pass |
| 580 |
if changed_vds: |
| 581 |
results['switch_previous'] = self.find_dvs_by_uuid(dvs_uuid) |
| 582 |
self.dv_switch_obj = find_dvs_by_name(self.content, results['switch_previous']) |
| 583 |
results['portgroup_previous'] = self.find_dvspg_by_key( |
| 584 |
self.dv_switch_obj, self.vnic.spec.distributedVirtualPort.portgroupKey |
| 585 |
) |
| 586 |
elif self.vds_name: |
| 587 |
results['switch'] = self.vds_name |
| 588 |
try: |
| 589 |
if self.vnic.spec.distributedVirtualPort.switchUuid != self.dv_switch_obj.uuid: |
| 590 |
changed_vds = True |
| 591 |
changed_list.append("Virtual Port") |
| 592 |
dvs_uuid = self.vnic.spec.distributedVirtualPort.switchUuid |
| 593 |
except AttributeError: |
| 594 |
changed_vds = True |
| 595 |
changed_list.append("Virtual Port") |
| 596 |
if changed_vds: |
| 597 |
results['switch_previous'] = self.find_dvs_by_uuid(dvs_uuid) |
| 598 |
results['portgroup_previous'] = self.vnic.spec.portgroup |
| 599 |
portgroups = self.get_all_port_groups_by_host(host_system=self.esxi_host_obj) |
| 600 |
for portgroup in portgroups: |
| 601 |
if portgroup.spec.name == self.vnic.spec.portgroup: |
| 602 |
results['switch_previous'] = portgroup.spec.vswitchName |
| 603 |
|
| 604 |
results['services'] = self.create_enabled_services_string() |
| 605 |
# Check configuration of service types (only if default TCP/IP stack is used) |
| 606 |
if self.vnic.spec.netStackInstanceKey == 'defaultTcpipStack': |
| 607 |
service_type_vmks = self.get_all_vmks_by_service_type() |
| 608 |
if (self.enable_vmotion and self.vnic.device not in service_type_vmks['vmotion']) or \ |
| 609 |
(not self.enable_vmotion and self.vnic.device in service_type_vmks['vmotion']): |
| 610 |
changed_services = changed_service_vmotion = True |
| 611 |
|
| 612 |
if (self.enable_mgmt and self.vnic.device not in service_type_vmks['management']) or \ |
| 613 |
(not self.enable_mgmt and self.vnic.device in service_type_vmks['management']): |
| 614 |
changed_services = changed_service_mgmt = True |
| 615 |
|
| 616 |
if (self.enable_ft and self.vnic.device not in service_type_vmks['faultToleranceLogging']) or \ |
| 617 |
(not self.enable_ft and self.vnic.device in service_type_vmks['faultToleranceLogging']): |
| 618 |
changed_services = changed_service_ft = True |
| 619 |
|
| 620 |
if (self.enable_vsan and self.vnic.device not in service_type_vmks['vsan']) or \ |
| 621 |
(not self.enable_vsan and self.vnic.device in service_type_vmks['vsan']): |
| 622 |
changed_services = changed_service_vsan = True |
| 623 |
|
| 624 |
if (self.enable_provisioning and self.vnic.device not in service_type_vmks['vSphereProvisioning']) or \ |
| 625 |
(not self.enable_provisioning and self.vnic.device in service_type_vmks['vSphereProvisioning']): |
| 626 |
changed_services = changed_service_prov = True |
| 627 |
|
| 628 |
if (self.enable_replication and self.vnic.device not in service_type_vmks['vSphereReplication']) or \ |
| 629 |
(not self.enable_replication and self.vnic.device in service_type_vmks['vSphereReplication']): |
| 630 |
changed_services = changed_service_rep = True |
| 631 |
|
| 632 |
if (self.enable_replication_nfc and self.vnic.device not in service_type_vmks['vSphereReplicationNFC']) or \ |
| 633 |
(not self.enable_replication_nfc and self.vnic.device in service_type_vmks['vSphereReplicationNFC']): |
| 634 |
changed_services = changed_service_rep_nfc = True |
| 635 |
|
| 636 |
if (self.enable_backup_nfc and self.vnic.device not in service_type_vmks['vSphereBackupNFC']) or \ |
| 637 |
(not self.enable_backup_nfc and self.vnic.device in service_type_vmks['vSphereBackupNFC']): |
| 638 |
changed_services = changed_service_backup_nfc = True |
| 639 |
|
| 640 |
if changed_services: |
| 641 |
changed_list.append("services") |
| 642 |
|
| 643 |
if changed_settings or changed_vds or changed_services: |
| 644 |
changed = True |
| 645 |
if self.module.check_mode: |
| 646 |
changed_suffix = ' would be updated' |
| 647 |
else: |
| 648 |
changed_suffix = ' updated' |
| 649 |
if len(changed_list) > 2: |
| 650 |
message = ', '.join(changed_list[:-1]) + ', and ' + str(changed_list[-1]) |
| 651 |
elif len(changed_list) == 2: |
| 652 |
message = ' and '.join(changed_list) |
| 653 |
elif len(changed_list) == 1: |
| 654 |
message = changed_list[0] |
| 655 |
message = "VMkernel Adapter " + message + changed_suffix |
| 656 |
if changed_settings or changed_vds: |
| 657 |
vnic_config = vim.host.VirtualNic.Specification() |
| 658 |
ip_spec = vim.host.IpConfig() |
| 659 |
if self.network_type == 'dhcp': |
| 660 |
ip_spec.dhcp = True |
| 661 |
else: |
| 662 |
ip_spec.dhcp = False |
| 663 |
ip_spec.ipAddress = self.ip_address |
| 664 |
ip_spec.subnetMask = self.subnet_mask |
| 665 |
if self.default_gateway: |
| 666 |
vnic_config.ipRouteSpec = vim.host.VirtualNic.IpRouteSpec() |
| 667 |
vnic_config.ipRouteSpec.ipRouteConfig = vim.host.IpRouteConfig() |
| 668 |
vnic_config.ipRouteSpec.ipRouteConfig.defaultGateway = self.default_gateway |
| 669 |
else: |
| 670 |
vnic_config.ipRouteSpec = vim.host.VirtualNic.IpRouteSpec() |
| 671 |
vnic_config.ipRouteSpec.ipRouteConfig = vim.host.IpRouteConfig() |
| 672 |
|
| 673 |
vnic_config.ip = ip_spec |
| 674 |
vnic_config.mtu = self.mtu |
| 675 |
|
| 676 |
if changed_vds: |
| 677 |
if self.vswitch_name: |
| 678 |
vnic_config.portgroup = self.port_group_name |
| 679 |
elif self.vds_name: |
| 680 |
vnic_config.distributedVirtualPort = vim.dvs.PortConnection() |
| 681 |
vnic_config.distributedVirtualPort.switchUuid = self.dv_switch_obj.uuid |
| 682 |
vnic_config.distributedVirtualPort.portgroupKey = self.port_group_obj.key |
| 683 |
|
| 684 |
try: |
| 685 |
if not self.module.check_mode: |
| 686 |
self.esxi_host_obj.configManager.networkSystem.UpdateVirtualNic(self.vnic.device, vnic_config) |
| 687 |
except vim.fault.NotFound as not_found: |
| 688 |
self.module.fail_json( |
| 689 |
msg="Failed to update vmk as virtual network adapter cannot be found %s" % |
| 690 |
to_native(not_found.msg) |
| 691 |
) |
| 692 |
except vim.fault.HostConfigFault as host_config_fault: |
| 693 |
self.module.fail_json( |
| 694 |
msg="Failed to update vmk due to host config issues : %s" % |
| 695 |
to_native(host_config_fault.msg) |
| 696 |
) |
| 697 |
except vim.fault.InvalidState as invalid_state: |
| 698 |
self.module.fail_json( |
| 699 |
msg="Failed to update vmk as ipv6 address is specified in an ipv4 only system : %s" % |
| 700 |
to_native(invalid_state.msg) |
| 701 |
) |
| 702 |
except vmodl.fault.InvalidArgument as invalid_arg: |
| 703 |
self.module.fail_json( |
| 704 |
msg="Failed to update vmk as IP address or Subnet Mask in the IP configuration" |
| 705 |
"are invalid or PortGroup does not exist : %s" % to_native(invalid_arg.msg) |
| 706 |
) |
| 707 |
|
| 708 |
if changed_services: |
| 709 |
changed_list.append("Services") |
| 710 |
services_previous = [] |
| 711 |
vnic_manager = self.esxi_host_obj.configManager.virtualNicManager |
| 712 |
|
| 713 |
if changed_service_mgmt: |
| 714 |
if self.vnic.device in service_type_vmks['management']: |
| 715 |
services_previous.append('Mgmt') |
| 716 |
operation = 'select' if self.enable_mgmt else 'deselect' |
| 717 |
self.set_service_type( |
| 718 |
vnic_manager=vnic_manager, vmk=self.vnic, service_type='management', operation=operation |
| 719 |
) |
| 720 |
|
| 721 |
if changed_service_vmotion: |
| 722 |
if self.vnic.device in service_type_vmks['vmotion']: |
| 723 |
services_previous.append('vMotion') |
| 724 |
operation = 'select' if self.enable_vmotion else 'deselect' |
| 725 |
self.set_service_type( |
| 726 |
vnic_manager=vnic_manager, vmk=self.vnic, service_type='vmotion', operation=operation |
| 727 |
) |
| 728 |
|
| 729 |
if changed_service_ft: |
| 730 |
if self.vnic.device in service_type_vmks['faultToleranceLogging']: |
| 731 |
services_previous.append('FT') |
| 732 |
operation = 'select' if self.enable_ft else 'deselect' |
| 733 |
self.set_service_type( |
| 734 |
vnic_manager=vnic_manager, vmk=self.vnic, service_type='faultToleranceLogging', operation=operation |
| 735 |
) |
| 736 |
|
| 737 |
if changed_service_prov: |
| 738 |
if self.vnic.device in service_type_vmks['vSphereProvisioning']: |
| 739 |
services_previous.append('Prov') |
| 740 |
operation = 'select' if self.enable_provisioning else 'deselect' |
| 741 |
self.set_service_type( |
| 742 |
vnic_manager=vnic_manager, vmk=self.vnic, service_type='vSphereProvisioning', operation=operation |
| 743 |
) |
| 744 |
|
| 745 |
if changed_service_rep: |
| 746 |
if self.vnic.device in service_type_vmks['vSphereReplication']: |
| 747 |
services_previous.append('Repl') |
| 748 |
operation = 'select' if self.enable_replication else 'deselect' |
| 749 |
self.set_service_type( |
| 750 |
vnic_manager=vnic_manager, vmk=self.vnic, service_type='vSphereReplication', operation=operation |
| 751 |
) |
| 752 |
|
| 753 |
if changed_service_rep_nfc: |
| 754 |
if self.vnic.device in service_type_vmks['vSphereReplicationNFC']: |
| 755 |
services_previous.append('Repl_NFC') |
| 756 |
operation = 'select' if self.enable_replication_nfc else 'deselect' |
| 757 |
self.set_service_type( |
| 758 |
vnic_manager=vnic_manager, vmk=self.vnic, service_type='vSphereReplicationNFC', operation=operation |
| 759 |
) |
| 760 |
|
| 761 |
if changed_service_backup_nfc: |
| 762 |
if self.vnic.device in service_type_vmks['vSphereBackupNFC']: |
| 763 |
services_previous.append('Backup_NFC') |
| 764 |
operation = 'select' if self.enable_backup_nfc else 'deselect' |
| 765 |
self.set_service_type( |
| 766 |
vnic_manager=vnic_manager, vmk=self.vnic, service_type='vSphereBackupNFC', operation=operation |
| 767 |
) |
| 768 |
|
| 769 |
if changed_service_vsan: |
| 770 |
if self.vnic.device in service_type_vmks['vsan']: |
| 771 |
services_previous.append('VSAN') |
| 772 |
results['vsan'] = self.set_vsan_service_type(self.enable_vsan) |
| 773 |
|
| 774 |
results['services_previous'] = ', '.join(services_previous) |
| 775 |
else: |
| 776 |
message = "VMkernel Adapter already configured properly" |
| 777 |
|
| 778 |
results['changed'] = changed |
| 779 |
results['msg'] = message |
| 780 |
results['device'] = self.vnic.device |
| 781 |
self.module.exit_json(**results) |
| 782 |
|
| 783 |
def find_dvs_by_uuid(self, uuid): |
| 784 |
""" |
| 785 |
Find DVS by UUID |
| 786 |
Returns: DVS name |
| 787 |
""" |
| 788 |
dvs_list = get_all_objs(self.content, [vim.DistributedVirtualSwitch]) |
| 789 |
for dvs in dvs_list: |
| 790 |
if dvs.uuid == uuid: |
| 791 |
return dvs.summary.name |
| 792 |
return None |
| 793 |
|
| 794 |
def find_dvspg_by_key(self, dv_switch, portgroup_key): |
| 795 |
""" |
| 796 |
Find dvPortgroup by key |
| 797 |
Returns: dvPortgroup name |
| 798 |
""" |
| 799 |
|
| 800 |
portgroups = dv_switch.portgroup |
| 801 |
|
| 802 |
for portgroup in portgroups: |
| 803 |
if portgroup.key == portgroup_key: |
| 804 |
return portgroup.name |
| 805 |
|
| 806 |
return None |
| 807 |
|
| 808 |
def set_vsan_service_type(self, enable_vsan): |
| 809 |
""" |
| 810 |
Set VSAN service type |
| 811 |
Returns: result of UpdateVsan_Task |
| 812 |
|
| 813 |
""" |
| 814 |
result = None |
| 815 |
vsan_system = self.esxi_host_obj.configManager.vsanSystem |
| 816 |
|
| 817 |
vsan_system_config = vsan_system.config |
| 818 |
vsan_config = vim.vsan.host.ConfigInfo() |
| 819 |
|
| 820 |
vsan_config.networkInfo = vsan_system_config.networkInfo |
| 821 |
current_vsan_vnics = [portConfig.device for portConfig in vsan_system_config.networkInfo.port] |
| 822 |
changed = False |
| 823 |
result = "%s NIC %s (currently enabled NICs: %s) : " % ("Enable" if enable_vsan else "Disable", self.vnic.device, current_vsan_vnics) |
| 824 |
if not enable_vsan: |
| 825 |
if self.vnic.device in current_vsan_vnics: |
| 826 |
vsan_config.networkInfo.port = list(filter(lambda portConfig: portConfig.device != self.vnic.device, vsan_config.networkInfo.port)) |
| 827 |
changed = True |
| 828 |
else: |
| 829 |
if self.vnic.device not in current_vsan_vnics: |
| 830 |
vsan_port_config = vim.vsan.host.ConfigInfo.NetworkInfo.PortConfig() |
| 831 |
vsan_port_config.device = self.vnic.device |
| 832 |
|
| 833 |
if vsan_config.networkInfo is None: |
| 834 |
vsan_config.networkInfo = vim.vsan.host.ConfigInfo.NetworkInfo() |
| 835 |
vsan_config.networkInfo.port = [vsan_port_config] |
| 836 |
else: |
| 837 |
vsan_config.networkInfo.port.append(vsan_port_config) |
| 838 |
changed = True |
| 839 |
|
| 840 |
if not self.module.check_mode and changed: |
| 841 |
try: |
| 842 |
vsan_task = vsan_system.UpdateVsan_Task(vsan_config) |
| 843 |
task_result = wait_for_task(vsan_task) |
| 844 |
if task_result[0]: |
| 845 |
result += "Success" |
| 846 |
else: |
| 847 |
result += "Failed" |
| 848 |
except TaskError as task_err: |
| 849 |
self.module.fail_json( |
| 850 |
msg="Failed to set service type to vsan for %s : %s" % (self.vnic.device, to_native(task_err)) |
| 851 |
) |
| 852 |
if self.module.check_mode: |
| 853 |
result += "Dry-run" |
| 854 |
return result |
| 855 |
|
| 856 |
def host_vmk_create(self): |
| 857 |
""" |
| 858 |
Create VMKernel |
| 859 |
Returns: NA |
| 860 |
|
| 861 |
""" |
| 862 |
results = dict(changed=False, message='') |
| 863 |
if self.vswitch_name: |
| 864 |
results['switch'] = self.vswitch_name |
| 865 |
elif self.vds_name: |
| 866 |
results['switch'] = self.vds_name |
| 867 |
results['portgroup'] = self.port_group_name |
| 868 |
|
| 869 |
vnic_config = vim.host.VirtualNic.Specification() |
| 870 |
|
| 871 |
ip_spec = vim.host.IpConfig() |
| 872 |
results['ipv4'] = self.network_type |
| 873 |
if self.network_type == 'dhcp': |
| 874 |
ip_spec.dhcp = True |
| 875 |
else: |
| 876 |
ip_spec.dhcp = False |
| 877 |
results['ipv4_ip'] = self.ip_address |
| 878 |
results['ipv4_sm'] = self.subnet_mask |
| 879 |
ip_spec.ipAddress = self.ip_address |
| 880 |
ip_spec.subnetMask = self.subnet_mask |
| 881 |
if self.default_gateway: |
| 882 |
vnic_config.ipRouteSpec = vim.host.VirtualNic.IpRouteSpec() |
| 883 |
vnic_config.ipRouteSpec.ipRouteConfig = vim.host.IpRouteConfig() |
| 884 |
vnic_config.ipRouteSpec.ipRouteConfig.defaultGateway = self.default_gateway |
| 885 |
vnic_config.ip = ip_spec |
| 886 |
|
| 887 |
results['mtu'] = self.mtu |
| 888 |
vnic_config.mtu = self.mtu |
| 889 |
|
| 890 |
results['tcpip_stack'] = self.tcpip_stack |
| 891 |
vnic_config.netStackInstanceKey = self.get_api_net_stack_instance(self.tcpip_stack) |
| 892 |
|
| 893 |
vmk_device = None |
| 894 |
try: |
| 895 |
if self.module.check_mode: |
| 896 |
results['msg'] = "VMkernel Adapter would be created" |
| 897 |
else: |
| 898 |
if self.vswitch_name: |
| 899 |
vmk_device = self.esxi_host_obj.configManager.networkSystem.AddVirtualNic( |
| 900 |
self.port_group_name, |
| 901 |
vnic_config |
| 902 |
) |
| 903 |
elif self.vds_name: |
| 904 |
vnic_config.distributedVirtualPort = vim.dvs.PortConnection() |
| 905 |
vnic_config.distributedVirtualPort.switchUuid = self.dv_switch_obj.uuid |
| 906 |
vnic_config.distributedVirtualPort.portgroupKey = self.port_group_obj.key |
| 907 |
vmk_device = self.esxi_host_obj.configManager.networkSystem.AddVirtualNic(portgroup="", nic=vnic_config) |
| 908 |
results['msg'] = "VMkernel Adapter created" |
| 909 |
results['changed'] = True |
| 910 |
results['device'] = vmk_device |
| 911 |
if self.network_type != 'dhcp': |
| 912 |
if self.default_gateway: |
| 913 |
results['ipv4_gw'] = self.default_gateway |
| 914 |
else: |
| 915 |
results['ipv4_gw'] = "No override" |
| 916 |
results['services'] = self.create_enabled_services_string() |
| 917 |
except vim.fault.AlreadyExists as already_exists: |
| 918 |
self.module.fail_json( |
| 919 |
msg="Failed to add vmk as portgroup already has a virtual network adapter %s" % |
| 920 |
to_native(already_exists.msg) |
| 921 |
) |
| 922 |
except vim.fault.HostConfigFault as host_config_fault: |
| 923 |
self.module.fail_json( |
| 924 |
msg="Failed to add vmk due to host config issues : %s" % |
| 925 |
to_native(host_config_fault.msg) |
| 926 |
) |
| 927 |
except vim.fault.InvalidState as invalid_state: |
| 928 |
self.module.fail_json( |
| 929 |
msg="Failed to add vmk as ipv6 address is specified in an ipv4 only system : %s" % |
| 930 |
to_native(invalid_state.msg) |
| 931 |
) |
| 932 |
except vmodl.fault.InvalidArgument as invalid_arg: |
| 933 |
self.module.fail_json( |
| 934 |
msg="Failed to add vmk as IP address or Subnet Mask in the IP configuration " |
| 935 |
"are invalid or PortGroup does not exist : %s" % to_native(invalid_arg.msg) |
| 936 |
) |
| 937 |
|
| 938 |
# do service type configuration |
| 939 |
if self.tcpip_stack == 'default' and not all( |
| 940 |
option is False for option in [self.enable_vsan, self.enable_vmotion, |
| 941 |
self.enable_mgmt, self.enable_ft, |
| 942 |
self.enable_provisioning, self.enable_replication, |
| 943 |
self.enable_replication_nfc, self.enable_backup_nfc]): |
| 944 |
self.vnic = self.get_vmkernel_by_device(device_name=vmk_device) |
| 945 |
|
| 946 |
# VSAN |
| 947 |
if self.enable_vsan: |
| 948 |
results['vsan'] = self.set_vsan_service_type(self.enable_vsan) |
| 949 |
|
| 950 |
# Other service type |
| 951 |
host_vnic_manager = self.esxi_host_obj.configManager.virtualNicManager |
| 952 |
if self.enable_vmotion: |
| 953 |
self.set_service_type(host_vnic_manager, self.vnic, 'vmotion') |
| 954 |
|
| 955 |
if self.enable_mgmt: |
| 956 |
self.set_service_type(host_vnic_manager, self.vnic, 'management') |
| 957 |
|
| 958 |
if self.enable_ft: |
| 959 |
self.set_service_type(host_vnic_manager, self.vnic, 'faultToleranceLogging') |
| 960 |
|
| 961 |
if self.enable_provisioning: |
| 962 |
self.set_service_type(host_vnic_manager, self.vnic, 'vSphereProvisioning') |
| 963 |
|
| 964 |
if self.enable_replication: |
| 965 |
self.set_service_type(host_vnic_manager, self.vnic, 'vSphereReplication') |
| 966 |
|
| 967 |
if self.enable_replication_nfc: |
| 968 |
self.set_service_type(host_vnic_manager, self.vnic, 'vSphereReplicationNFC') |
| 969 |
|
| 970 |
if self.enable_backup_nfc: |
| 971 |
self.set_service_type(host_vnic_manager, self.vnic, 'vSphereBackupNFC') |
| 972 |
|
| 973 |
self.module.exit_json(**results) |
| 974 |
|
| 975 |
def set_service_type(self, vnic_manager, vmk, service_type, operation='select'): |
| 976 |
""" |
| 977 |
Set service type to given VMKernel |
| 978 |
Args: |
| 979 |
vnic_manager: Virtual NIC manager object |
| 980 |
vmk: VMkernel managed object |
| 981 |
service_type: Name of service type |
| 982 |
operation: Select to select service type, deselect to deselect service type |
| 983 |
|
| 984 |
""" |
| 985 |
try: |
| 986 |
if operation == 'select': |
| 987 |
if not self.module.check_mode: |
| 988 |
vnic_manager.SelectVnicForNicType(service_type, vmk.device) |
| 989 |
elif operation == 'deselect': |
| 990 |
if not self.module.check_mode: |
| 991 |
vnic_manager.DeselectVnicForNicType(service_type, vmk.device) |
| 992 |
except vmodl.fault.InvalidArgument as invalid_arg: |
| 993 |
self.module.fail_json( |
| 994 |
msg="Failed to %s VMK service type '%s' on '%s' due to : %s" % |
| 995 |
(operation, service_type, vmk.device, to_native(invalid_arg.msg)) |
| 996 |
) |
| 997 |
|
| 998 |
def get_all_vmks_by_service_type(self): |
| 999 |
""" |
| 1000 |
Return information about service types and VMKernel |
| 1001 |
Returns: Dictionary of service type as key and VMKernel list as value |
| 1002 |
|
| 1003 |
""" |
| 1004 |
service_type_vmk = dict( |
| 1005 |
vmotion=[], |
| 1006 |
vsan=[], |
| 1007 |
management=[], |
| 1008 |
faultToleranceLogging=[], |
| 1009 |
vSphereProvisioning=[], |
| 1010 |
vSphereReplication=[], |
| 1011 |
vSphereReplicationNFC=[], |
| 1012 |
vSphereBackupNFC=[], |
| 1013 |
) |
| 1014 |
|
| 1015 |
for service_type in list(service_type_vmk): |
| 1016 |
vmks_list = self.query_service_type_for_vmks(service_type) |
| 1017 |
service_type_vmk[service_type] = vmks_list |
| 1018 |
return service_type_vmk |
| 1019 |
|
| 1020 |
def query_service_type_for_vmks(self, service_type): |
| 1021 |
""" |
| 1022 |
Return list of VMKernels |
| 1023 |
Args: |
| 1024 |
service_type: Name of service type |
| 1025 |
|
| 1026 |
Returns: List of VMKernel which belongs to that service type |
| 1027 |
|
| 1028 |
""" |
| 1029 |
vmks_list = [] |
| 1030 |
query = None |
| 1031 |
try: |
| 1032 |
query = self.esxi_host_obj.configManager.virtualNicManager.QueryNetConfig(service_type) |
| 1033 |
except vim.fault.HostConfigFault as config_fault: |
| 1034 |
self.module.fail_json( |
| 1035 |
msg="Failed to get all VMKs for service type %s due to host config fault : %s" % |
| 1036 |
(service_type, to_native(config_fault.msg)) |
| 1037 |
) |
| 1038 |
except vmodl.fault.InvalidArgument as invalid_argument: |
| 1039 |
self.module.fail_json( |
| 1040 |
msg="Failed to get all VMKs for service type %s due to invalid arguments : %s" % |
| 1041 |
(service_type, to_native(invalid_argument.msg)) |
| 1042 |
) |
| 1043 |
|
| 1044 |
if not query.selectedVnic: |
| 1045 |
return vmks_list |
| 1046 |
vnics_with_service_type = [vnic.device for vnic in query.candidateVnic if vnic.key in query.selectedVnic] |
| 1047 |
return vnics_with_service_type |
| 1048 |
|
| 1049 |
def create_enabled_services_string(self): |
| 1050 |
"""Create services list""" |
| 1051 |
services = [] |
| 1052 |
if self.enable_mgmt: |
| 1053 |
services.append('Mgmt') |
| 1054 |
if self.enable_vmotion: |
| 1055 |
services.append('vMotion') |
| 1056 |
if self.enable_ft: |
| 1057 |
services.append('FT') |
| 1058 |
if self.enable_vsan: |
| 1059 |
services.append('VSAN') |
| 1060 |
if self.enable_provisioning: |
| 1061 |
services.append('Prov') |
| 1062 |
if self.enable_replication: |
| 1063 |
services.append('Repl') |
| 1064 |
if self.enable_replication_nfc: |
| 1065 |
services.append('Repl_NFC') |
| 1066 |
if self.enable_backup_nfc: |
| 1067 |
services.append('Backup_NFC') |
| 1068 |
return ', '.join(services) |
| 1069 |
|
| 1070 |
@staticmethod |
| 1071 |
def get_api_net_stack_instance(tcpip_stack): |
| 1072 |
"""Get TCP/IP stack instance name or key""" |
| 1073 |
net_stack_instance = None |
| 1074 |
if tcpip_stack == 'default': |
| 1075 |
net_stack_instance = 'defaultTcpipStack' |
| 1076 |
elif tcpip_stack == 'provisioning': |
| 1077 |
net_stack_instance = 'vSphereProvisioning' |
| 1078 |
# vmotion and vxlan stay the same |
| 1079 |
elif tcpip_stack == 'vmotion': |
| 1080 |
net_stack_instance = 'vmotion' |
| 1081 |
elif tcpip_stack == 'vxlan': |
| 1082 |
net_stack_instance = 'vxlan' |
| 1083 |
elif tcpip_stack == 'defaultTcpipStack': |
| 1084 |
net_stack_instance = 'default' |
| 1085 |
elif tcpip_stack == 'vSphereProvisioning': |
| 1086 |
net_stack_instance = 'provisioning' |
| 1087 |
|
| 1088 |
return net_stack_instance |
| 1089 |
|
| 1090 |
|
| 1091 |
def main(): |
| 1092 |
"""Main""" |
| 1093 |
argument_spec = base_argument_spec() |
| 1094 |
argument_spec.update(dict( |
| 1095 |
esxi_hostname=dict(required=True, type='str'), |
| 1096 |
portgroup_name=dict(required=True, type='str', aliases=['portgroup']), |
| 1097 |
mtu=dict(required=False, type='int', default=1500), |
| 1098 |
device=dict(type='str'), |
| 1099 |
enable_vsan=dict(required=False, type='bool', default=False), |
| 1100 |
enable_vmotion=dict(required=False, type='bool', default=False), |
| 1101 |
enable_mgmt=dict(required=False, type='bool', default=False), |
| 1102 |
enable_ft=dict(required=False, type='bool', default=False), |
| 1103 |
enable_provisioning=dict(type='bool', default=False), |
| 1104 |
enable_replication=dict(type='bool', default=False), |
| 1105 |
enable_replication_nfc=dict(type='bool', default=False), |
| 1106 |
enable_backup_nfc=dict(type='bool', default=False), |
| 1107 |
vswitch_name=dict(required=False, type='str', aliases=['vswitch']), |
| 1108 |
dvswitch_name=dict(required=False, type='str', aliases=['dvswitch']), |
| 1109 |
network=dict( |
| 1110 |
type='dict', |
| 1111 |
options=dict( |
| 1112 |
type=dict(type='str', default='static', choices=['static', 'dhcp']), |
| 1113 |
ip_address=dict(type='str'), |
| 1114 |
subnet_mask=dict(type='str'), |
| 1115 |
default_gateway=dict(type='str'), |
| 1116 |
tcpip_stack=dict(type='str', default='default', choices=['default', 'provisioning', 'vmotion', 'vxlan']), |
| 1117 |
), |
| 1118 |
default=dict( |
| 1119 |
type='static', |
| 1120 |
tcpip_stack='default', |
| 1121 |
), |
| 1122 |
), |
| 1123 |
state=dict( |
| 1124 |
type='str', |
| 1125 |
default='present', |
| 1126 |
choices=['absent', 'present'] |
| 1127 |
), |
| 1128 |
)) |
| 1129 |
|
| 1130 |
module = AnsibleModule(argument_spec=argument_spec, |
| 1131 |
mutually_exclusive=[ |
| 1132 |
['vswitch_name', 'dvswitch_name'], |
| 1133 |
], |
| 1134 |
required_one_of=[ |
| 1135 |
['vswitch_name', 'dvswitch_name'], |
| 1136 |
['portgroup_name', 'device'], |
| 1137 |
], |
| 1138 |
required_if=[ |
| 1139 |
['state', 'present', ['portgroup_name']], |
| 1140 |
['state', 'absent', ['device']] |
| 1141 |
], |
| 1142 |
supports_check_mode=True) |
| 1143 |
|
| 1144 |
pyv = PyVmomiHelper(module) |
| 1145 |
pyv.ensure() |
| 1146 |
|
| 1147 |
|
| 1148 |
if __name__ == '__main__': |
| 1149 |
main() |