diff --git a/changelogs/fragments/65922-filter-VMs-of-Same-name-on-the-basis-of-folder.yml b/changelogs/fragments/65922-filter-VMs-of-Same-name-on-the-basis-of-folder.yml new file mode 100644 index 00000000000..cca940b5fda --- /dev/null +++ b/changelogs/fragments/65922-filter-VMs-of-Same-name-on-the-basis-of-folder.yml @@ -0,0 +1,2 @@ +bugfixes: + - Vmware Fix for Create overwrites a VM of same name even when the folder is different(https://github.com/ansible/ansible/issues/43161) diff --git a/lib/ansible/module_utils/vmware.py b/lib/ansible/module_utils/vmware.py index 35b2c3d77bc..11283ad76a0 100644 --- a/lib/ansible/module_utils/vmware.py +++ b/lib/ansible/module_utils/vmware.py @@ -1053,7 +1053,11 @@ class PyVmomi(object): break elif vms: # Unique virtual machine found. - vm_obj = vms[0] + actual_vm_folder_path = self.get_vm_path(content=self.content, vm_name=vms[0]) + if self.params.get('folder') is None: + vm_obj = vms[0] + elif self.params['folder'] in actual_vm_folder_path: + vm_obj = vms[0] elif 'moid' in self.params and self.params['moid']: vm_obj = VmomiSupport.templateOf('VirtualMachine')(self.params['moid'], self.si._stub)