From df2a09e998205df30306de33ca3ce1dd9cae1cb5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gon=C3=A9ri=20Le=20Bouder?= Date: Tue, 13 Aug 2019 15:27:22 +0200 Subject: [PATCH] vmware: avoid unnecessary copy() call (#60476) Two vmware modules uses copy() to duplicate an internal instance of a pyvmomi object. This to be able to modify the object during an iteration. See: https://github.com/ansible/ansible/pull/60196/files#r312643761 Closes: #60399 --- lib/ansible/modules/cloud/vmware/vmware_datastore_facts.py | 6 ++---- lib/ansible/modules/cloud/vmware/vmware_guest.py | 6 ++---- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/lib/ansible/modules/cloud/vmware/vmware_datastore_facts.py b/lib/ansible/modules/cloud/vmware/vmware_datastore_facts.py index 48f4b852485..7d327708ca8 100644 --- a/lib/ansible/modules/cloud/vmware/vmware_datastore_facts.py +++ b/lib/ansible/modules/cloud/vmware/vmware_datastore_facts.py @@ -217,12 +217,10 @@ class PyVmomiCache(object): if confine_to_datacenter: if hasattr(objects, 'items'): # resource pools come back as a dictionary - tmpobjs = objects.copy() - for k, v in objects.items(): + for k, v in tuple(objects.items()): parent_dc = get_parent_datacenter(k) if parent_dc.name != self.dc_name: - tmpobjs.pop(k, None) - objects = tmpobjs + del objects[k] else: # everything else should be a list objects = [x for x in objects if get_parent_datacenter(x).name == self.dc_name] diff --git a/lib/ansible/modules/cloud/vmware/vmware_guest.py b/lib/ansible/modules/cloud/vmware/vmware_guest.py index ea71b9329ae..47312f3cf04 100644 --- a/lib/ansible/modules/cloud/vmware/vmware_guest.py +++ b/lib/ansible/modules/cloud/vmware/vmware_guest.py @@ -785,12 +785,10 @@ class PyVmomiCache(object): if hasattr(objects, 'items'): # resource pools come back as a dictionary # make a copy - tmpobjs = objects.copy() - for k, v in objects.items(): + for k, v in tuple(objects.items()): parent_dc = self.get_parent_datacenter(k) if parent_dc.name != self.dc_name: - tmpobjs.pop(k, None) - objects = tmpobjs + del objects[k] else: # everything else should be a list objects = [x for x in objects if self.get_parent_datacenter(x).name == self.dc_name]