diff --git a/plugins/modules/vm_list_group_by_clusters.py b/plugins/modules/vm_list_group_by_clusters.py index 1787b8fd..bbf31f2f 100644 --- a/plugins/modules/vm_list_group_by_clusters.py +++ b/plugins/modules/vm_list_group_by_clusters.py @@ -18,29 +18,22 @@ author: - Ansible Cloud Team (@ansible-collections) options: - detailed_vms: - default: true - description: - - Wither or not get the vms detailed. - type: bool + detailed_vms: + default: true + description: + - If I(true) gather detailed information about virtual machines. + type: bool extends_documentation_fragment: - vmware.vmware.vmware_rest_client.documentation ''' EXAMPLES = r''' -- name: Gather list of VMs +- name: Gather list of VMs group by clusters and folders vmware.vmware.vm_list_group_by_clusters: hostname: "https://vcenter" username: "username" password: "password" - -- name: Gather list of VMs in clusterA - vmware.vmware.vm_list_group_by_clusters: - hostname: "https://vcenter" - username: "username" - password: "password" - detailed_vms: False ''' RETURN = r''' @@ -132,24 +125,34 @@ def get_all_folders(self): type=self.api_client.vcenter.Folder.Type.VIRTUAL_MACHINE) return self.api_client.vcenter.Folder.list(folder_filter_spec) - def get_all_vms_in_cluster_and_folder(self, cluster, folder): + def get_all_vms_in_cluster_and_folder(self, cluster, folder, host): vms_filter = self.api_client.vcenter.VM.FilterSpec(clusters=set([cluster]), - folders=set([folder])) + folders=set([folder]), + hosts=set([host])) + return self.api_client.vcenter.VM.list(vms_filter) def get_vm_detailed(self, vm): return self.api_client.vcenter.VM.get(vm=vm) + def get_cluster_hosts(self, cluster): + host_filter = self.api_client.vcenter.Host.FilterSpec( + clusters=set([cluster])) + return self.api_client.vcenter.Host.list(host_filter) + def get_vm_list_group_by_clusters(self): clusters = self.get_all_clusters() folders = self.get_all_folders() - result_dict = {} for cluster in clusters: vm_list_group_by_folder_dict = {} - empty = True + hosts = self.get_cluster_hosts(cluster.cluster) for folder in folders: - vms = self.get_all_vms_in_cluster_and_folder(cluster.cluster, folder.folder) + vms = [] + # iterate each host due to an error too_many_matches when looking at all vms on a cluster + # https://github.com/vmware/vsphere-automation-sdk-python/issues/142 + for host in hosts: + vms.extend(self.get_all_vms_in_cluster_and_folder(cluster.cluster, folder.folder, host.host)) vms_detailed = [] for vm in vms: if self.detailed_vms: @@ -158,10 +161,9 @@ def get_vm_list_group_by_clusters(self): vms_detailed.append(self._vvars(vm)) if vms_detailed: - empty = False vm_list_group_by_folder_dict[folder.name] = vms_detailed - if not empty: + if vm_list_group_by_folder_dict: result_dict[cluster.name] = vm_list_group_by_folder_dict return result_dict diff --git a/tests/integration/targets/vm_list_group_by_clusters/mock.json b/tests/integration/targets/vm_list_group_by_clusters/mock.json index 8a05cf8a..ee2ebeea 100644 --- a/tests/integration/targets/vm_list_group_by_clusters/mock.json +++ b/tests/integration/targets/vm_list_group_by_clusters/mock.json @@ -61,11 +61,11 @@ "body": { "type": "JSON", "matchType": "PARTIAL", - "json": "{\"params\":{\"serviceId\":\"com.vmware.vcenter.VM\",\"operationId\":\"list\"}}" + "json": "{\"params\":{\"serviceId\":\"com.vmware.vcenter.host\",\"operationId\":\"list\"}}" } }, "httpResponseTemplate": { - "template": "{\"statusCode\": 200, \"headers\": {\"Content-type\": \"application/json\"}, \"body\": {\"jsonrpc\": \"2.0\", \"result\": {\"output\": [{\"STRUCTURE\": {\"com.vmware.vcenter.VM\": {\"name\": \"vm1\", \"power_state\": \"POWERED_OFF\", \"vm\": \"vm1\"}}}]}, 'id': '2'}}", + "template": "{\"statusCode\": 200, \"headers\": {\"Content-type\": \"application/json\"}, \"body\": {\"jsonrpc\": \"2.0\", \"result\": {\"output\": [{\"STRUCTURE\": {\"com.vmware.vcenter.host\": {\"connection_state\": \"CONNECTED\", \"host\": \"host1\", \"name\": \"host1\", \"power_state\": {\"OPTIONAL\": \"POWERED_ON\"}}}}]}, 'id': '2'}}", "templateType": "VELOCITY" }, "priority": 1 @@ -77,11 +77,11 @@ "body": { "type": "JSON", "matchType": "PARTIAL", - "json": "{\"params\":{\"serviceId\":\"com.vmware.cis.session\",\"operationId\":\"get\"}}" + "json": "{\"params\":{\"serviceId\":\"com.vmware.vcenter.VM\",\"operationId\":\"list\"}}" } }, "httpResponseTemplate": { - "template": "{\"statusCode\": 200, \"headers\": {\"Content-type\": \"application/json\"}, \"body\": {\"jsonrpc\": \"2.0\", \"result\": {\"output\": 'hostname.example.com'}, 'id': '${json.parse($!request.body)['id']}'}}", + "template": "{\"statusCode\": 200, \"headers\": {\"Content-type\": \"application/json\"}, \"body\": {\"jsonrpc\": \"2.0\", \"result\": {\"output\": [{\"STRUCTURE\": {\"com.vmware.vcenter.VM\": {\"name\": \"vm1\", \"power_state\": \"POWERED_OFF\", \"vm\": \"vm1\"}}}]}, 'id': '3'}}", "templateType": "VELOCITY" }, "priority": 1 diff --git a/tests/integration/targets/vm_list_group_by_clusters/tasks/main.yml b/tests/integration/targets/vm_list_group_by_clusters/tasks/main.yml index 51fb0853..a76c358c 100644 --- a/tests/integration/targets/vm_list_group_by_clusters/tasks/main.yml +++ b/tests/integration/targets/vm_list_group_by_clusters/tasks/main.yml @@ -13,4 +13,6 @@ ansible.builtin.assert: that: - __res.changed == False - - __res.vm_list_group_by_clusters | length == 1 \ No newline at end of file + - __res.vm_list_group_by_clusters | length == 1 + - __res.vm_list_group_by_clusters['cluster1'] | length == 1 + - __res.vm_list_group_by_clusters['cluster1']['folder1'] | length == 1 diff --git a/tests/integration/targets/vm_list_group_by_clusters/vars.yml b/tests/integration/targets/vm_list_group_by_clusters/vars.yml index 4283c137..e0be4100 100644 --- a/tests/integration/targets/vm_list_group_by_clusters/vars.yml +++ b/tests/integration/targets/vm_list_group_by_clusters/vars.yml @@ -3,4 +3,4 @@ vcenter_username: "user" vcenter_password: "pass" vcenter_port: 1080 -mock_file: "vm_list_group_by_clusters" \ No newline at end of file +mock_file: "vm_list_group_by_clusters"