diff --git a/lib/ansible/modules/cloud/vmware/vmware_vm_facts.py b/lib/ansible/modules/cloud/vmware/vmware_vm_facts.py index 2cbcd8af630..797c0fafba5 100644 --- a/lib/ansible/modules/cloud/vmware/vmware_vm_facts.py +++ b/lib/ansible/modules/cloud/vmware/vmware_vm_facts.py @@ -20,6 +20,7 @@ module: vmware_vm_facts short_description: Return basic facts pertaining to a vSphere virtual machine guest description: - Return basic facts pertaining to a vSphere virtual machine guest. +- Cluster name as fact is added in version 2.7. version_added: '2.0' author: - Joseph Callen (@jcpowermac) @@ -84,6 +85,21 @@ virtual_machines: description: dictionary of virtual machines and their facts returned: success type: dict + sample: + { + "ubuntu_t": { + "cluster": null, + "esxi_hostname": "10.76.33.226", + "guest_fullname": "Ubuntu Linux (64-bit)", + "ip_address": "", + "mac_address": [ + "00:50:56:87:a5:9a" + ], + "power_state": "poweredOff", + "uuid": "4207072c-edd8-3bd5-64dc-903fd3a0db04", + "vm_network": {} + } + } ''' try: @@ -135,8 +151,14 @@ class VmwareVmFacts(PyVmomi): net_dict[device.macAddress]['ipv4'].append(ip_addr) esxi_hostname = None + esxi_parent = None if summary.runtime.host: esxi_hostname = summary.runtime.host.summary.config.name + esxi_parent = summary.runtime.host.parent + + cluster_name = None + if esxi_parent and isinstance(esxi_parent, vim.ClusterComputeResource): + cluster_name = summary.runtime.host.parent.name virtual_machine = { summary.config.name: { @@ -147,6 +169,7 @@ class VmwareVmFacts(PyVmomi): "uuid": summary.config.uuid, "vm_network": net_dict, "esxi_hostname": esxi_hostname, + "cluster": cluster_name, } } diff --git a/test/integration/targets/vmware_vm_facts/tasks/main.yml b/test/integration/targets/vmware_vm_facts/tasks/main.yml index 97088202b4d..9052dfbf542 100644 --- a/test/integration/targets/vmware_vm_facts/tasks/main.yml +++ b/test/integration/targets/vmware_vm_facts/tasks/main.yml @@ -50,5 +50,13 @@ assert: that: - "{{ item | basename in vm_facts_0001['virtual_machines'].keys()}}" + - "vm_facts_0001['virtual_machines'][item | basename]['cluster'] is defined" + - "vm_facts_0001['virtual_machines'][item | basename]['esxi_hostname'] is defined" + - "vm_facts_0001['virtual_machines'][item | basename]['guest_fullname'] is defined" + - "vm_facts_0001['virtual_machines'][item | basename]['ip_address'] is defined" + - "vm_facts_0001['virtual_machines'][item | basename]['mac_address'] is defined" + - "vm_facts_0001['virtual_machines'][item | basename]['power_state'] is defined" + - "vm_facts_0001['virtual_machines'][item | basename]['uuid'] is defined" + - "vm_facts_0001['virtual_machines'][item | basename]['vm_network'] is defined" with_items: - "{{ host_info_result['json'] }}"