VMware: Use existing DVPG network in vmware_guest_network (#65994)

* Handle all cases of networks

Fixes: #65968

Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>
This commit is contained in:
Abhijeet Kasurde 2020-01-15 21:56:19 +05:30 committed by GitHub
parent 69f9c3c8eb
commit afb71c14bd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 161 additions and 6 deletions

View file

@ -0,0 +1,2 @@
bugfixes:
- In vmware_guest_network module use appropriate network while creating or reconfiguring (https://github.com/ansible/ansible/issues/65968).

View file

@ -306,9 +306,27 @@ class PyVmomiHelper(PyVmomi):
nic = vim.vm.device.VirtualDeviceSpec()
nic.device = self.get_device_type(device_type=device_info.get('device_type', 'vmxnet3'))
nic.device.deviceInfo = vim.Description()
network_object = self.find_network_by_name(network_name=device_info['name'])[0]
if network_object:
if hasattr(network_object, 'portKeys'):
# DistributedVirtualPortGroup
nic.device.backing = vim.vm.device.VirtualEthernetCard.DistributedVirtualPortBackingInfo()
nic.device.backing.port = vim.dvs.PortConnection()
nic.device.backing.port.switchUuid = network_object.config.distributedVirtualSwitch.uuid
nic.device.backing.port.portgroupKey = network_object.key
elif isinstance(network_object, vim.OpaqueNetwork):
# NSX-T Logical Switch
nic.device.backing = vim.vm.device.VirtualEthernetCard.OpaqueNetworkBackingInfo()
network_id = network_object.summary.opaqueNetworkId
nic.device.backing.opaqueNetworkType = 'nsx.LogicalSwitch'
nic.device.backing.opaqueNetworkId = network_id
nic.device.deviceInfo.summary = 'nsx.LogicalSwitch: %s' % network_id
else:
# Standard vSwitch
nic.device.deviceInfo.summary = device_info['name']
nic.device.backing = vim.vm.device.VirtualEthernetCard.NetworkBackingInfo()
nic.device.backing.deviceName = device_info['name']
nic.device.backing.network = network_object
nic.device.connectable = vim.vm.device.VirtualDevice.ConnectInfo()
nic.device.connectable.startConnected = device_info.get('start_connected', True)
nic.device.connectable.allowGuestControl = True
@ -461,8 +479,30 @@ class PyVmomiHelper(PyVmomi):
if 'connected' in network and nic_device.connectable.connected != network['connected']:
nic_device.connectable.connected = network['connected']
self.change_detected = True
if 'name' in network and nic_device.deviceInfo.summary != network['name']:
nic_device.deviceInfo.summary = network['name']
if 'name' in network:
network_object = self.find_network_by_name(network_name=network['name'])[0]
if network_object and hasattr(network_object, 'portKeys') and hasattr(nic_spec.device.backing, 'port'):
if network_object.config.distributedVirtualSwitch.uuid != nic_spec.device.backing.port.switchUuid:
# DistributedVirtualPortGroup
nic_spec.device.backing = vim.vm.device.VirtualEthernetCard.DistributedVirtualPortBackingInfo()
nic_spec.device.backing.port = vim.dvs.PortConnection()
nic_spec.device.backing.port.switchUuid = network_object.config.distributedVirtualSwitch.uuid
nic_spec.device.backing.port.portgroupKey = network_object.key
self.change_detected = True
elif network_object and isinstance(network_object, vim.OpaqueNetwork) and hasattr(nic_spec.device.backing, 'opaqueNetworkId'):
if nic_spec.device.backing.opaqueNetworkId != network_object.summary.opaqueNetworkId:
# NSX-T Logical Switch
nic_spec.device.backing = vim.vm.device.VirtualEthernetCard.OpaqueNetworkBackingInfo()
network_id = network_object.summary.opaqueNetworkId
nic_spec.device.backing.opaqueNetworkType = 'nsx.LogicalSwitch'
nic_spec.device.backing.opaqueNetworkId = network_id
nic_spec.device.deviceInfo.summary = 'nsx.LogicalSwitch: %s' % network_id
self.change_detected = True
elif nic_device.deviceInfo.summary != network['name']:
# Standard vSwitch
nic_spec.device.backing = vim.vm.device.VirtualEthernetCard.NetworkBackingInfo()
nic_spec.device.backing.deviceName = network['name']
nic_spec.device.backing.network = network_object
self.change_detected = True
if 'manual_mac' in network and nic_device.macAddress != network['manual_mac']:
if vm_obj.runtime.powerState != vim.VirtualMachinePowerState.poweredOff:

View file

@ -235,3 +235,116 @@
that:
- not (no_nw_details is changed)
- no_nw_details.failed
- name: Change portgroup to dvPortgroup
vmware_guest_network:
validate_certs: False
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
name: test_vm1
networks:
- name: "{{ dvpg1 }}"
label: "Network adapter 1"
connected: false
start_connected: true
state: present
register: change_netaddr_dvp
- debug: var=change_netaddr_dvp
- name: Check changed to dvPortgroup from PortGroup
assert:
that:
- change_netaddr_dvp.changed is sameas true
- name: Change portgroup to dvPortgroup
vmware_guest_network:
validate_certs: False
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
name: test_vm1
networks:
- name: "{{ dvpg1 }}"
label: "Network adapter 1"
connected: false
start_connected: true
state: present
register: change_netaddr_dvp
- debug: var=change_netaddr_dvp
- name: Check not changed of dvPortgroup
assert:
that:
- change_netaddr_dvp.changed is sameas false
- name: Change dvPortgroup to PortGroup
vmware_guest_network:
validate_certs: False
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
name: test_vm1
networks:
- name: "VM Network"
label: "Network adapter 1"
connected: false
start_connected: true
state: present
register: change_netaddr_pg
- debug: var=change_netaddr_pg
- name: Check changed to dvPortgroup from PortGroup
assert:
that:
- change_netaddr_pg.changed is sameas true
- change_netaddr_pg.network_data['0'].name == "VM Network"
- name: Change dvPortgroup to PortGroup
vmware_guest_network:
validate_certs: False
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
name: test_vm1
networks:
- name: "VM Network"
label: "Network adapter 1"
connected: false
start_connected: true
state: present
register: change_netaddr_pg
- debug: var=change_netaddr_pg
- name: Check not changed of PortGroup
assert:
that:
- change_netaddr_pg.changed is sameas false
- change_netaddr_pg.network_data['0'].name == "VM Network"
# https://github.com/ansible/ansible/issues/65968
- name: Create a network with dvPortgroup
vmware_guest_network:
validate_certs: False
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
name: test_vm1
networks:
- name: "{{ dvpg1 }}"
label: "Network adapter 2"
connected: true
start_connected: true
state: new
register: create_netaddr_pg
- debug: var=create_netaddr_pg
- name: Check if network is created with dvpg
assert:
that:
- create_netaddr_pg.changed is sameas true