diff --git a/changelogs/fragments/1688-fix-auto-host-groups.yml b/changelogs/fragments/1688-fix-auto-host-groups.yml new file mode 100644 index 000000000..63770e995 --- /dev/null +++ b/changelogs/fragments/1688-fix-auto-host-groups.yml @@ -0,0 +1,3 @@ +bugfixes: + - vmware_vm_inventory - Fixed unwanted hostgroups being created automatically when the with_path option is enabled. + (https://github.com/ansible-collections/community.vmware/issues/1688). diff --git a/plugins/inventory/vmware_vm_inventory.py b/plugins/inventory/vmware_vm_inventory.py index 2f3a7775e..0ec5a38cd 100644 --- a/plugins/inventory/vmware_vm_inventory.py +++ b/plugins/inventory/vmware_vm_inventory.py @@ -425,7 +425,6 @@ from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict from ansible.module_utils.common.dict_transformations import _snake_to_camel from ansible.utils.display import Display -from ansible.module_utils.six import text_type from ansible_collections.community.vmware.plugins.plugin_utils.inventory import ( to_nested_dict, to_flatten_dict, @@ -929,26 +928,6 @@ def _populate_host_properties(self, host_properties, host): # Create groups based on variable values and add the corresponding hosts to it self._add_host_to_keyed_groups(self.get_option('keyed_groups'), host_properties, host, strict=strict) - with_path = self.get_option('with_path') - if with_path: - parents = host_properties['path'].split('/') - if parents: - if isinstance(with_path, text_type): - parents = [with_path] + parents - - c_name = self._sanitize_group_name('/'.join(parents)) - c_group = self.inventory.add_group(c_name) - self.inventory.add_host(host, c_group) - parents.pop() - - while len(parents) > 0: - p_name = self._sanitize_group_name('/'.join(parents)) - p_group = self.inventory.add_group(p_name) - - self.inventory.add_child(p_group, c_group) - c_group = p_group - parents.pop() - can_sanitize = self.get_option('with_sanitized_property_name') # Sanitize host properties: to snake case diff --git a/tests/integration/targets/inventory_vmware_vm_inventory/playbook/test_options.yml b/tests/integration/targets/inventory_vmware_vm_inventory/playbook/test_options.yml index 3861cc95b..2651382b8 100644 --- a/tests/integration/targets/inventory_vmware_vm_inventory/playbook/test_options.yml +++ b/tests/integration/targets/inventory_vmware_vm_inventory/playbook/test_options.yml @@ -82,7 +82,7 @@ assert: that: - "'path' in test_host.value" - - "'my_center' in test_host.value.group_names" + - "'my_center' in test_host.value.path" - name: Inventory 'with_nested_properties' option include_tasks: build_inventory.yml