Debian Codegraph @

ansible_collections/community/vmware/plugins/modules/vmware_vm_vss_dvs_migrate.py

raw
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_vm_vss_dvs_migrate
15 short_description: Migrates a virtual machine from a standard vswitch to distributed
16 description:
17 - Migrates a virtual machine from a standard vswitch to distributed
18 author:
19 - Joseph Callen (@jcpowermac)
20 options:
21 vm_name:
22 description:
23 - Name of the virtual machine to migrate to a dvSwitch
24 required: true
25 type: str
26 dvportgroup_name:
27 description:
28 - Name of the portgroup to migrate to the virtual machine to
29 required: true
30 type: str
31 extends_documentation_fragment:
32 - vmware.vmware.base_options
33
34 '''
35
36 EXAMPLES = r'''
37 - name: Migrate VCSA to vDS
38 community.vmware.vmware_vm_vss_dvs_migrate:
39 hostname: '{{ vcenter_hostname }}'
40 username: '{{ vcenter_username }}'
41 password: '{{ vcenter_password }}'
42 vm_name: '{{ vm_name }}'
43 dvportgroup_name: '{{ distributed_portgroup_name }}'
44 delegate_to: localhost
45 '''
46
47 try:
48 from pyVmomi import vim, vmodl
49 except ImportError:
50 pass
51
52 from ansible.module_utils.basic import AnsibleModule
53 from ansible_collections.community.vmware.plugins.module_utils.vmware import (
54 PyVmomi, get_all_objs, wait_for_task)
55 from ansible_collections.vmware.vmware.plugins.module_utils.argument_spec import base_argument_spec
56
57
58 class VMwareVmVssDvsMigrate(PyVmomi):
59 def __init__(self, module):
60 super(VMwareVmVssDvsMigrate, self).__init__(module)
61 self.vm = None
62 self.vm_name = module.params['vm_name']
63 self.dvportgroup_name = module.params['dvportgroup_name']
64
65 def process_state(self):
66 vm_nic_states = {
67 'absent': self.migrate_network_adapter_vds,
68 'present': self.state_exit_unchanged,
69 }
70
71 vm_nic_states[self.check_vm_network_state()]()
72
73 def find_dvspg_by_name(self):
74 vmware_distributed_port_group = get_all_objs(self.content, [vim.dvs.DistributedVirtualPortgroup])
75 for dvspg in vmware_distributed_port_group:
76 if dvspg.name == self.dvportgroup_name:
77 return dvspg
78 return None
79
80 def find_vm_by_name(self):
81 virtual_machines = get_all_objs(self.content, [vim.VirtualMachine])
82 for vm in virtual_machines:
83 if vm.name == self.vm_name:
84 return vm
85 return None
86
87 def migrate_network_adapter_vds(self):
88 vm_configspec = vim.vm.ConfigSpec()
89 nic = vim.vm.device.VirtualEthernetCard.DistributedVirtualPortBackingInfo()
90 port = vim.dvs.PortConnection()
91 devicespec = vim.vm.device.VirtualDeviceSpec()
92
93 pg = self.find_dvspg_by_name()
94
95 if pg is None:
96 self.module.fail_json(msg="The standard portgroup was not found")
97
98 dvswitch = pg.config.distributedVirtualSwitch
99 port.switchUuid = dvswitch.uuid
100 port.portgroupKey = pg.key
101 nic.port = port
102
103 for device in self.vm.config.hardware.device:
104 if isinstance(device, vim.vm.device.VirtualEthernetCard):
105 devicespec.device = device
106 devicespec.operation = vim.vm.device.VirtualDeviceSpec.Operation.edit
107 devicespec.device.backing = nic
108 vm_configspec.deviceChange.append(devicespec)
109
110 task = self.vm.ReconfigVM_Task(vm_configspec)
111 changed, result = wait_for_task(task)
112 self.module.exit_json(changed=changed, result=result)
113
114 def state_exit_unchanged(self):
115 self.module.exit_json(changed=False)
116
117 def check_vm_network_state(self):
118 try:
119 self.vm = self.find_vm_by_name()
120
121 if self.vm is None:
122 self.module.fail_json(msg="A virtual machine with name %s does not exist" % self.vm_name)
123 for device in self.vm.config.hardware.device:
124 if isinstance(device, vim.vm.device.VirtualEthernetCard):
125 if isinstance(device.backing, vim.vm.device.VirtualEthernetCard.DistributedVirtualPortBackingInfo):
126 return 'present'
127 return 'absent'
128 except vmodl.RuntimeFault as runtime_fault:
129 self.module.fail_json(msg=runtime_fault.msg)
130 except vmodl.MethodFault as method_fault:
131 self.module.fail_json(msg=method_fault.msg)
132
133
134 def main():
135
136 argument_spec = base_argument_spec()
137 argument_spec.update(dict(vm_name=dict(required=True, type='str'),
138 dvportgroup_name=dict(required=True, type='str')))
139
140 module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=False)
141
142 vmware_vmnic_migrate = VMwareVmVssDvsMigrate(module)
143 vmware_vmnic_migrate.process_state()
144
145
146 if __name__ == '__main__':
147 main()