Merge pull request #1626 from jcpowermac/vmware_migrate_vmk_fix_20

Resolves issue with vmware_migrate_vmk module for v2.0
reviewable/pr18780/r1
Brian Coca 9 years ago
commit f6d2763671

@ -75,8 +75,6 @@ Example from Ansible playbook
migrate_switch_name: dvSwitch migrate_switch_name: dvSwitch
migrate_portgroup_name: Management migrate_portgroup_name: Management
''' '''
try: try:
from pyVmomi import vim, vmodl from pyVmomi import vim, vmodl
HAS_PYVMOMI = True HAS_PYVMOMI = True
@ -84,88 +82,93 @@ except ImportError:
HAS_PYVMOMI = False HAS_PYVMOMI = False
def state_exit_unchanged(module): class VMwareMigrateVmk(object):
module.exit_json(changed=False) def __init__(self, module):
self.module = module
self.host_system = None
def state_migrate_vds_vss(module): self.migrate_switch_name = self.module.params['migrate_switch_name']
module.exit_json(changed=False, msg="Currently Not Implemented") self.migrate_portgroup_name = self.module.params['migrate_portgroup_name']
self.device = self.module.params['device']
self.esxi_hostname = self.module.params['esxi_hostname']
def create_host_vnic_config(dv_switch_uuid, portgroup_key, device): self.current_portgroup_name = self.module.params['current_portgroup_name']
self.current_switch_name = self.module.params['current_switch_name']
host_vnic_config = vim.host.VirtualNic.Config() self.content = connect_to_api(module)
host_vnic_config.spec = vim.host.VirtualNic.Specification()
host_vnic_config.changeOperation = "edit" def process_state(self):
host_vnic_config.device = device try:
host_vnic_config.portgroup = "" vmk_migration_states = {
host_vnic_config.spec.distributedVirtualPort = vim.dvs.PortConnection() 'migrate_vss_vds': self.state_migrate_vss_vds,
host_vnic_config.spec.distributedVirtualPort.switchUuid = dv_switch_uuid 'migrate_vds_vss': self.state_migrate_vds_vss,
host_vnic_config.spec.distributedVirtualPort.portgroupKey = portgroup_key 'migrated': self.state_exit_unchanged
}
return host_vnic_config
vmk_migration_states[self.check_vmk_current_state()]()
def create_port_group_config(switch_name, portgroup_name): except vmodl.RuntimeFault as runtime_fault:
port_group_config = vim.host.PortGroup.Config() self.module.fail_json(msg=runtime_fault.msg)
port_group_config.spec = vim.host.PortGroup.Specification() except vmodl.MethodFault as method_fault:
self.module.fail_json(msg=method_fault.msg)
port_group_config.changeOperation = "remove" except Exception as e:
port_group_config.spec.name = portgroup_name self.module.fail_json(msg=str(e))
port_group_config.spec.vlanId = -1
port_group_config.spec.vswitchName = switch_name def state_exit_unchanged(self):
port_group_config.spec.policy = vim.host.NetworkPolicy() self.module.exit_json(changed=False)
return port_group_config def state_migrate_vds_vss(self):
self.module.exit_json(changed=False, msg="Currently Not Implemented")
def state_migrate_vss_vds(module): def create_host_vnic_config(self, dv_switch_uuid, portgroup_key):
content = module.params['content'] host_vnic_config = vim.host.VirtualNic.Config()
host_system = module.params['host_system'] host_vnic_config.spec = vim.host.VirtualNic.Specification()
migrate_switch_name = module.params['migrate_switch_name']
migrate_portgroup_name = module.params['migrate_portgroup_name'] host_vnic_config.changeOperation = "edit"
current_portgroup_name = module.params['current_portgroup_name'] host_vnic_config.device = self.device
current_switch_name = module.params['current_switch_name'] host_vnic_config.portgroup = ""
device = module.params['device'] host_vnic_config.spec.distributedVirtualPort = vim.dvs.PortConnection()
host_vnic_config.spec.distributedVirtualPort.switchUuid = dv_switch_uuid
host_network_system = host_system.configManager.networkSystem host_vnic_config.spec.distributedVirtualPort.portgroupKey = portgroup_key
dv_switch = find_dvs_by_name(content, migrate_switch_name) return host_vnic_config
pg = find_dvspg_by_name(dv_switch, migrate_portgroup_name)
def create_port_group_config(self):
config = vim.host.NetworkConfig() port_group_config = vim.host.PortGroup.Config()
config.portgroup = [create_port_group_config(current_switch_name, current_portgroup_name)] port_group_config.spec = vim.host.PortGroup.Specification()
config.vnic = [create_host_vnic_config(dv_switch.uuid, pg.key, device)]
host_network_system.UpdateNetworkConfig(config, "modify") port_group_config.changeOperation = "remove"
module.exit_json(changed=True) port_group_config.spec.name = self.current_portgroup_name
port_group_config.spec.vlanId = -1
port_group_config.spec.vswitchName = self.current_switch_name
def check_vmk_current_state(module): port_group_config.spec.policy = vim.host.NetworkPolicy()
device = module.params['device'] return port_group_config
esxi_hostname = module.params['esxi_hostname']
current_portgroup_name = module.params['current_portgroup_name'] def state_migrate_vss_vds(self):
current_switch_name = module.params['current_switch_name'] host_network_system = self.host_system.configManager.networkSystem
content = connect_to_api(module) dv_switch = find_dvs_by_name(self.content, self.migrate_switch_name)
pg = find_dvspg_by_name(dv_switch, self.migrate_portgroup_name)
host_system = find_hostsystem_by_name(content, esxi_hostname)
config = vim.host.NetworkConfig()
module.params['content'] = content config.portgroup = [self.create_port_group_config()]
module.params['host_system'] = host_system config.vnic = [self.create_host_vnic_config(dv_switch.uuid, pg.key)]
host_network_system.UpdateNetworkConfig(config, "modify")
for vnic in host_system.configManager.networkSystem.networkInfo.vnic: self.module.exit_json(changed=True)
if vnic.device == device:
module.params['vnic'] = vnic def check_vmk_current_state(self):
if vnic.spec.distributedVirtualPort is None: self.host_system = find_hostsystem_by_name(self.content, self.esxi_hostname)
if vnic.portgroup == current_portgroup_name:
return "migrate_vss_vds" for vnic in self.host_system.configManager.networkSystem.networkInfo.vnic:
else: if vnic.device == self.device:
dvs = find_dvs_by_name(content, current_switch_name) #self.vnic = vnic
if dvs is None: if vnic.spec.distributedVirtualPort is None:
return "migrated" if vnic.portgroup == self.current_portgroup_name:
if vnic.spec.distributedVirtualPort.switchUuid == dvs.uuid: return "migrate_vss_vds"
return "migrate_vds_vss" else:
dvs = find_dvs_by_name(self.content, self.current_switch_name)
if dvs is None:
return "migrated"
if vnic.spec.distributedVirtualPort.switchUuid == dvs.uuid:
return "migrate_vds_vss"
def main(): def main():
@ -181,23 +184,10 @@ def main():
module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=False) module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=False)
if not HAS_PYVMOMI: if not HAS_PYVMOMI:
module.fail_json(msg='pyvmomi required for this module') self.module.fail_json(msg='pyvmomi required for this module')
try: vmware_migrate_vmk = VMwareMigrateVmk(module)
vmk_migration_states = { vmware_migrate_vmk.process_state()
'migrate_vss_vds': state_migrate_vss_vds,
'migrate_vds_vss': state_migrate_vds_vss,
'migrated': state_exit_unchanged
}
vmk_migration_states[check_vmk_current_state(module)](module)
except vmodl.RuntimeFault as runtime_fault:
module.fail_json(msg=runtime_fault.msg)
except vmodl.MethodFault as method_fault:
module.fail_json(msg=method_fault.msg)
except Exception as e:
module.fail_json(msg=str(e))
from ansible.module_utils.vmware import * from ansible.module_utils.vmware import *
from ansible.module_utils.basic import * from ansible.module_utils.basic import *

Loading…
Cancel
Save