From 2a1393e0e1a67e5d4ef86aa644bda0a908329cf6 Mon Sep 17 00:00:00 2001 From: Abhijeet Kasurde Date: Tue, 6 Aug 2019 10:38:50 +0530 Subject: [PATCH] VMware: Check dvswitch presence before accessing it (#60052) Check if dvswitch object is not None before accessing it's properties such as UUID. This can be due to two reason 1. Permission issues 2. There is no association between given distributed virtual portgroup distributed virtual switch Fixes: #59952 Signed-off-by: Abhijeet Kasurde --- .../fragments/59952-vmware_guest-check_dvs.yml | 2 ++ lib/ansible/modules/cloud/vmware/vmware_guest.py | 13 +++++++------ 2 files changed, 9 insertions(+), 6 deletions(-) create mode 100644 changelogs/fragments/59952-vmware_guest-check_dvs.yml diff --git a/changelogs/fragments/59952-vmware_guest-check_dvs.yml b/changelogs/fragments/59952-vmware_guest-check_dvs.yml new file mode 100644 index 00000000000..69052b07854 --- /dev/null +++ b/changelogs/fragments/59952-vmware_guest-check_dvs.yml @@ -0,0 +1,2 @@ +minor_changes: + - Check dvs in the given portgroup before accessing any properties of dvs (https://github.com/ansible/ansible/issues/59952). This can be due to permission issue or no association between distributed virtual portgroup and dvswitch. diff --git a/lib/ansible/modules/cloud/vmware/vmware_guest.py b/lib/ansible/modules/cloud/vmware/vmware_guest.py index ee014614115..22a39a944be 100644 --- a/lib/ansible/modules/cloud/vmware/vmware_guest.py +++ b/lib/ansible/modules/cloud/vmware/vmware_guest.py @@ -1347,6 +1347,13 @@ class PyVmomiHelper(PyVmomi): else: pg_obj = self.cache.find_obj(self.content, [vim.dvs.DistributedVirtualPortgroup], network_name) + # TODO: (akasurde) There is no way to find association between resource pool and distributed virtual portgroup + # For now, check if we are able to find distributed virtual switch + if not pg_obj.config.distributedVirtualSwitch: + self.module.fail_json(msg="Failed to find distributed virtual switch which is associated with" + " distributed virtual portgroup '%s'. Make sure hostsystem is associated with" + " the given distributed virtual portgroup. Also, check if user has correct" + " 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 @@ -1363,12 +1370,6 @@ class PyVmomiHelper(PyVmomi): self.module.fail_json(msg="It seems that host system '%s' is not associated with distributed" " virtual portgroup '%s'. Please make sure host system is associated" " with given distributed virtual portgroup" % (host_system, pg_obj.name)) - # TODO: (akasurde) There is no way to find association between resource pool and distributed virtual portgroup - # For now, check if we are able to find distributed virtual switch - if not pg_obj.config.distributedVirtualSwitch: - self.module.fail_json(msg="Failed to find distributed virtual switch which is associated with" - " distributed virtual portgroup '%s'. Make sure hostsystem is associated with" - " the given distributed virtual portgroup." % pg_obj.name) dvs_port_connection.switchUuid = pg_obj.config.distributedVirtualSwitch.uuid nic.device.backing = vim.vm.device.VirtualEthernetCard.DistributedVirtualPortBackingInfo() nic.device.backing.port = dvs_port_connection