diff --git a/plugins/inventory/esxi_hosts.py b/plugins/inventory/esxi_hosts.py index e23fb97f..6497a246 100644 --- a/plugins/inventory/esxi_hosts.py +++ b/plugins/inventory/esxi_hosts.py @@ -73,17 +73,17 @@ type: list elements: str default: [] - group_by_path: + group_by_paths: description: - If true, groups will be created based on the ESXI hosts' paths. - - Any slashes in the paths will be replaced by underscores in the group names. + - Paths will be sanitized to match Ansible group name standards. For example, any slashes or dashes in the paths will be replaced by underscores in the group names. - A group is created for each step down in the path, with the group from the step above containing subsequent groups. - - For example, a path /DC/hosts/Cluster will create groups 'DC' which contains group 'DC_hosts' which contains group 'DC_hosts_Cluster' + - For example, a path /DC-01/hosts/Cluster will create groups 'DC_01' which contains group 'DC_01_hosts' which contains group 'DC_01_hosts_Cluster' default: false type: bool - group_by_path_prefix: + group_by_paths_prefix: description: - - If O(group_by_path) is true, set this variable if you want to add a prefix to any groups created based on paths. + - If O(group_by_paths) is true, set this variable if you want to add a prefix to any groups created based on paths. - By default, no prefix is added to the group names. default: '' type: str @@ -113,7 +113,7 @@ plugin: vmware.vmware.esxi_hosts # Create groups based on host paths -group_by_path: true +group_by_paths: true # Create a group with hosts that support vMotion using the vmotionSupported property properties: ["name", "capability"] @@ -404,15 +404,15 @@ def add_host_to_groups_based_on_path(self, esxi_host: EsxiInventoryHost): Optionally, the user can add a prefix to the groups created by this process. The final group in the path will be where the ESXi host is added. """ - if not self.get_option("group_by_path"): + if not self.get_option("group_by_paths"): return path_parts = esxi_host.path.split('/') group_name_parts = [] last_created_group = None - if self.get_option("group_by_path_prefix"): - group_name_parts = [self.get_option("group_by_path_prefix")] + if self.get_option("group_by_paths_prefix"): + group_name_parts = [self.get_option("group_by_paths_prefix")] for path_part in path_parts: if not path_part: diff --git a/tests/integration/targets/vmware_inventory_esxi_hosts/defaults/main.yml b/tests/integration/targets/vmware_inventory_esxi_hosts/defaults/main.yml new file mode 100644 index 00000000..539b766c --- /dev/null +++ b/tests/integration/targets/vmware_inventory_esxi_hosts/defaults/main.yml @@ -0,0 +1 @@ +run_on_simulator: false diff --git a/tests/integration/targets/vmware_inventory_esxi_hosts/files/test.esxi_hosts.yml b/tests/integration/targets/vmware_inventory_esxi_hosts/files/test.esxi_hosts.yml new file mode 100644 index 00000000..00b5173c --- /dev/null +++ b/tests/integration/targets/vmware_inventory_esxi_hosts/files/test.esxi_hosts.yml @@ -0,0 +1,6 @@ +--- +plugin: vmware.vmware.esxi_hosts +cache: false +group_by_paths: true +group_by_paths_prefix: test +gather_tags: true diff --git a/tests/integration/targets/vmware_inventory_esxi_hosts/run.yml b/tests/integration/targets/vmware_inventory_esxi_hosts/run.yml new file mode 100644 index 00000000..55b14107 --- /dev/null +++ b/tests/integration/targets/vmware_inventory_esxi_hosts/run.yml @@ -0,0 +1,13 @@ +- hosts: localhost + gather_facts: no + tasks: + - name: Import eco-vcenter credentials + ansible.builtin.include_vars: + file: ../../integration_config.yml + tags: eco-vcenter-ci + + - name: Call esxi_hosts inventory role + ansible.builtin.import_role: + name: vmware_inventory_esxi_hosts + tags: + - eco-vcenter-ci diff --git a/tests/integration/targets/vmware_inventory_esxi_hosts/tasks/main.yml b/tests/integration/targets/vmware_inventory_esxi_hosts/tasks/main.yml new file mode 100644 index 00000000..32f00c95 --- /dev/null +++ b/tests/integration/targets/vmware_inventory_esxi_hosts/tasks/main.yml @@ -0,0 +1,29 @@ +--- +- block: + - name: Import common vars + ansible.builtin.include_vars: + file: ../group_vars.yml + + - name: Run Inventory Plugin + ansible.builtin.command: ansible-inventory -i "{{ role_path }}/files/test.esxi_hosts.yml" --list + register: _inventory_out + + - name: Parse Inventory Results as JSON + ansible.builtin.set_fact: + inventory_results: "{{ _inventory_out.stdout | from_json }}" + + # you can't reference the 'all' property here for some reason. It reverts back to the test playbook inventory + # instead of the inventory_results + - name: Check Output + ansible.builtin.assert: + that: + - first_host.ansible_host + - first_host.tags is defined + - first_host.tags_by_category is defined + - >- + (inventory_results.poweredOn.hosts | length) == + (inventory_results._meta.hostvars.values() | selectattr('summary.runtime.powerState', 'equalto', 'poweredOn') | length) + - (inventory_results | length) > 3 + - ('test_' + vcenter_datacenter | replace('-', '_')) in inventory_results.keys() + vars: + first_host: "{{ (inventory_results._meta.hostvars.values() | first) }}"