Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vmware_guest - fix network part idempotency #1997

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions plugins/modules/vmware_guest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1923,6 +1923,7 @@ def configure_network(self, vm_obj):
"Removing interfaces is not allowed"
% (len(network_devices), len(current_net_devices)))

# Loop over requested devices
for key in range(0, len(network_devices)):
nic_change_detected = False
network_name = network_devices[key]['name']
Expand All @@ -1941,18 +1942,17 @@ def configure_network(self, vm_obj):
nic.device.connectable.startConnected != network_devices[key].get("start_connected"):
nic.device.connectable.startConnected = network_devices[key].get("start_connected")
nic_change_detected = True
# Valid only when VM is running
if "connected" in network_devices[key] and \
nic.device.connectable.connected != network_devices[key].get("connected"):
nic.device.connectable.connected != network_devices[key].get("connected") and \
vm_obj.runtime.powerState == vim.VirtualMachinePowerState.poweredOn:
nic.device.connectable.connected = network_devices[key].get("connected")
nic_change_detected = True
if "allow_guest_control" in network_devices[key] and \
nic.device.connectable.allowGuestControl != network_devices[key].get("allow_guest_control"):
nic.device.connectable.allowGuestControl = network_devices[key].get("allow_guest_control")
nic_change_detected = True

if nic.device.deviceInfo.summary != network_name:
nic.device.deviceInfo.summary = network_name
nic_change_detected = True
if 'device_type' in network_devices[key]:
device = self.device_helper.nic_device_type.get(network_devices[key]['device_type'])
if not isinstance(nic.device, device):
Expand All @@ -1975,8 +1975,9 @@ def configure_network(self, vm_obj):

net_obj = self.cache.get_network(network_name)
if hasattr(net_obj, 'portKeys'):
# VDS switch
# network_name matched existing VDS switch portgroup
pg_obj = None
# if dvswitch_name was also defined we need to make sure we have port_group of that dwswitch
if 'dvswitch_name' in network_devices[key]:
dvs_name = network_devices[key]['dvswitch_name']
dvs_obj = find_dvs_by_name(self.content, dvs_name)
Expand All @@ -1998,15 +1999,13 @@ def configure_network(self, vm_obj):
" permission to access distributed virtual switch in the given portgroup."
% pg_obj.name
)
if nic.device.backing and (
not hasattr(nic.device.backing, "port")
or (
nic.device.backing.port.portgroupKey != pg_obj.key
or nic.device.backing.port.switchUuid
!= pg_obj.config.distributedVirtualSwitch.uuid
)
):
nic_change_detected = True
if nic.device.backing:
if not hasattr(nic.device.backing, "port"):
# backing is not set to dvs pg yet
nic_change_detected = True
elif nic.device.backing.port.portgroupKey != pg_obj.key:
# backing is set to different dvs pg
nic_change_detected = True

dvs_port_connection = vim.dvs.PortConnection()
dvs_port_connection.portgroupKey = pg_obj.key
Expand Down Expand Up @@ -3588,6 +3587,7 @@ def main():
'powered-off', 'restarted', 'suspended',
'shutdownguest', 'shutdown-guest',
'rebootguest', 'reboot-guest']:
# With these states there are no config changes, just power state changes
if module.check_mode:
# Identify if the power state would have changed if not in check mode
current_powerstate = vm.summary.runtime.powerState.lower()
Expand Down