| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2015, Joseph Callen <jcallen () csc.com> |
| 5 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 7 |
|
| 8 |
from __future__ import absolute_import, division, print_function |
| 9 |
__metaclass__ = type |
| 10 |
|
| 11 |
|
| 12 |
DOCUMENTATION = r''' |
| 13 |
--- |
| 14 |
module: vmware_migrate_vmk |
| 15 |
short_description: Migrate a VMK interface from VSS to VDS |
| 16 |
description: |
| 17 |
- Migrate a VMK interface from VSS to VDS |
| 18 |
author: |
| 19 |
- Joseph Callen (@jcpowermac) |
| 20 |
- Russell Teague (@mtnbikenc) |
| 21 |
options: |
| 22 |
esxi_hostname: |
| 23 |
description: |
| 24 |
- ESXi hostname to be managed |
| 25 |
required: true |
| 26 |
type: str |
| 27 |
device: |
| 28 |
description: |
| 29 |
- VMK interface name |
| 30 |
required: true |
| 31 |
type: str |
| 32 |
current_switch_name: |
| 33 |
description: |
| 34 |
- Switch VMK interface is currently on |
| 35 |
required: true |
| 36 |
type: str |
| 37 |
current_portgroup_name: |
| 38 |
description: |
| 39 |
- Portgroup name VMK interface is currently on |
| 40 |
required: true |
| 41 |
type: str |
| 42 |
migrate_switch_name: |
| 43 |
description: |
| 44 |
- Switch name to migrate VMK interface to |
| 45 |
required: true |
| 46 |
type: str |
| 47 |
migrate_portgroup_name: |
| 48 |
description: |
| 49 |
- Portgroup name to migrate VMK interface to |
| 50 |
required: true |
| 51 |
type: str |
| 52 |
migrate_vlan_id: |
| 53 |
description: |
| 54 |
- VLAN to use for the VMK interface when migrating from VDS to VSS |
| 55 |
- Will be ignored when migrating from VSS to VDS |
| 56 |
type: int |
| 57 |
extends_documentation_fragment: |
| 58 |
- vmware.vmware.base_options |
| 59 |
|
| 60 |
''' |
| 61 |
|
| 62 |
EXAMPLES = r''' |
| 63 |
- name: Migrate Management vmk |
| 64 |
community.vmware.vmware_migrate_vmk: |
| 65 |
hostname: "{{ vcenter_hostname }}" |
| 66 |
username: "{{ vcenter_username }}" |
| 67 |
password: "{{ vcenter_password }}" |
| 68 |
esxi_hostname: "{{ esxi_hostname }}" |
| 69 |
device: vmk1 |
| 70 |
current_switch_name: temp_vswitch |
| 71 |
current_portgroup_name: esx-mgmt |
| 72 |
migrate_switch_name: dvSwitch |
| 73 |
migrate_portgroup_name: Management |
| 74 |
delegate_to: localhost |
| 75 |
''' |
| 76 |
try: |
| 77 |
from pyVmomi import vim, vmodl |
| 78 |
except ImportError: |
| 79 |
pass |
| 80 |
|
| 81 |
from ansible.module_utils.basic import AnsibleModule |
| 82 |
from ansible_collections.community.vmware.plugins.module_utils.vmware import ( |
| 83 |
find_dvs_by_name, find_hostsystem_by_name, |
| 84 |
PyVmomi, find_dvspg_by_name) |
| 85 |
from ansible_collections.vmware.vmware.plugins.module_utils.argument_spec import base_argument_spec |
| 86 |
|
| 87 |
|
| 88 |
class VMwareMigrateVmk(PyVmomi): |
| 89 |
|
| 90 |
def __init__(self, module): |
| 91 |
super(VMwareMigrateVmk, self).__init__(module) |
| 92 |
self.host_system = None |
| 93 |
self.migrate_switch_name = self.module.params['migrate_switch_name'] |
| 94 |
self.migrate_portgroup_name = self.module.params['migrate_portgroup_name'] |
| 95 |
self.migrate_vlan_id = self.module.params['migrate_vlan_id'] |
| 96 |
self.device = self.module.params['device'] |
| 97 |
self.esxi_hostname = self.module.params['esxi_hostname'] |
| 98 |
self.current_portgroup_name = self.module.params['current_portgroup_name'] |
| 99 |
self.current_switch_name = self.module.params['current_switch_name'] |
| 100 |
|
| 101 |
def process_state(self): |
| 102 |
try: |
| 103 |
vmk_migration_states = { |
| 104 |
'migrate_vss_vds': self.state_migrate_vss_vds, |
| 105 |
'migrate_vds_vss': self.state_migrate_vds_vss, |
| 106 |
'migrated': self.state_exit_unchanged |
| 107 |
} |
| 108 |
|
| 109 |
vmk_migration_states[self.check_vmk_current_state()]() |
| 110 |
|
| 111 |
except vmodl.RuntimeFault as runtime_fault: |
| 112 |
self.module.fail_json(msg=runtime_fault.msg) |
| 113 |
except vmodl.MethodFault as method_fault: |
| 114 |
self.module.fail_json(msg=method_fault.msg) |
| 115 |
except Exception as e: |
| 116 |
self.module.fail_json(msg=str(e)) |
| 117 |
|
| 118 |
def state_exit_unchanged(self): |
| 119 |
self.module.exit_json(changed=False) |
| 120 |
|
| 121 |
def create_host_vnic_config_vds_vss(self): |
| 122 |
host_vnic_config = vim.host.VirtualNic.Config() |
| 123 |
host_vnic_config.spec = vim.host.VirtualNic.Specification() |
| 124 |
host_vnic_config.changeOperation = "edit" |
| 125 |
host_vnic_config.device = self.device |
| 126 |
host_vnic_config.spec.portgroup = self.migrate_portgroup_name |
| 127 |
return host_vnic_config |
| 128 |
|
| 129 |
def create_port_group_config_vds_vss(self): |
| 130 |
port_group_config = vim.host.PortGroup.Config() |
| 131 |
port_group_config.spec = vim.host.PortGroup.Specification() |
| 132 |
port_group_config.changeOperation = "add" |
| 133 |
port_group_config.spec.name = self.migrate_portgroup_name |
| 134 |
port_group_config.spec.vlanId = self.migrate_vlan_id if self.migrate_vlan_id is not None else 0 |
| 135 |
port_group_config.spec.vswitchName = self.migrate_switch_name |
| 136 |
port_group_config.spec.policy = vim.host.NetworkPolicy() |
| 137 |
return port_group_config |
| 138 |
|
| 139 |
def state_migrate_vds_vss(self): |
| 140 |
host_network_system = self.host_system.configManager.networkSystem |
| 141 |
config = vim.host.NetworkConfig() |
| 142 |
config.portgroup = [self.create_port_group_config_vds_vss()] |
| 143 |
host_network_system.UpdateNetworkConfig(config, "modify") |
| 144 |
config = vim.host.NetworkConfig() |
| 145 |
config.vnic = [self.create_host_vnic_config_vds_vss()] |
| 146 |
host_network_system.UpdateNetworkConfig(config, "modify") |
| 147 |
self.module.exit_json(changed=True) |
| 148 |
|
| 149 |
def create_host_vnic_config(self, dv_switch_uuid, portgroup_key): |
| 150 |
host_vnic_config = vim.host.VirtualNic.Config() |
| 151 |
host_vnic_config.spec = vim.host.VirtualNic.Specification() |
| 152 |
|
| 153 |
host_vnic_config.changeOperation = "edit" |
| 154 |
host_vnic_config.device = self.device |
| 155 |
host_vnic_config.portgroup = "" |
| 156 |
host_vnic_config.spec.distributedVirtualPort = vim.dvs.PortConnection() |
| 157 |
host_vnic_config.spec.distributedVirtualPort.switchUuid = dv_switch_uuid |
| 158 |
host_vnic_config.spec.distributedVirtualPort.portgroupKey = portgroup_key |
| 159 |
|
| 160 |
return host_vnic_config |
| 161 |
|
| 162 |
def create_port_group_config(self): |
| 163 |
port_group_config = vim.host.PortGroup.Config() |
| 164 |
port_group_config.spec = vim.host.PortGroup.Specification() |
| 165 |
|
| 166 |
port_group_config.changeOperation = "remove" |
| 167 |
port_group_config.spec.name = self.current_portgroup_name |
| 168 |
port_group_config.spec.vlanId = -1 |
| 169 |
port_group_config.spec.vswitchName = self.current_switch_name |
| 170 |
port_group_config.spec.policy = vim.host.NetworkPolicy() |
| 171 |
|
| 172 |
return port_group_config |
| 173 |
|
| 174 |
def state_migrate_vss_vds(self): |
| 175 |
host_network_system = self.host_system.configManager.networkSystem |
| 176 |
|
| 177 |
dv_switch = find_dvs_by_name(self.content, self.migrate_switch_name) |
| 178 |
pg = find_dvspg_by_name(dv_switch, self.migrate_portgroup_name) |
| 179 |
|
| 180 |
config = vim.host.NetworkConfig() |
| 181 |
config.portgroup = [self.create_port_group_config()] |
| 182 |
config.vnic = [self.create_host_vnic_config(dv_switch.uuid, pg.key)] |
| 183 |
host_network_system.UpdateNetworkConfig(config, "modify") |
| 184 |
self.module.exit_json(changed=True) |
| 185 |
|
| 186 |
def check_vmk_current_state(self): |
| 187 |
self.host_system = find_hostsystem_by_name(self.content, self.esxi_hostname) |
| 188 |
|
| 189 |
for vnic in self.host_system.configManager.networkSystem.networkInfo.vnic: |
| 190 |
if vnic.device == self.device: |
| 191 |
if vnic.spec.distributedVirtualPort is None: |
| 192 |
std_vswitches = [vswitch.name for vswitch in self.host_system.configManager.networkSystem.networkInfo.vswitch] |
| 193 |
if self.current_switch_name not in std_vswitches: |
| 194 |
return "migrated" |
| 195 |
if vnic.portgroup == self.current_portgroup_name: |
| 196 |
return "migrate_vss_vds" |
| 197 |
else: |
| 198 |
dvs = find_dvs_by_name(self.content, self.current_switch_name) |
| 199 |
if dvs is None: |
| 200 |
return "migrated" |
| 201 |
if vnic.spec.distributedVirtualPort.switchUuid == dvs.uuid: |
| 202 |
return "migrate_vds_vss" |
| 203 |
|
| 204 |
self.module.fail_json(msg='Unable to find the specified device %s.' % self.device) |
| 205 |
|
| 206 |
|
| 207 |
def main(): |
| 208 |
|
| 209 |
argument_spec = base_argument_spec() |
| 210 |
argument_spec.update(dict(esxi_hostname=dict(required=True, type='str'), |
| 211 |
device=dict(required=True, type='str'), |
| 212 |
current_switch_name=dict(required=True, type='str'), |
| 213 |
current_portgroup_name=dict(required=True, type='str'), |
| 214 |
migrate_switch_name=dict(required=True, type='str'), |
| 215 |
migrate_portgroup_name=dict(required=True, type='str'), |
| 216 |
migrate_vlan_id=dict(required=False, type='int'))) |
| 217 |
|
| 218 |
module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=False) |
| 219 |
|
| 220 |
vmware_migrate_vmk = VMwareMigrateVmk(module) |
| 221 |
vmware_migrate_vmk.process_state() |
| 222 |
|
| 223 |
|
| 224 |
if __name__ == '__main__': |
| 225 |
main() |