diff --git a/changelogs/fragments/56125-vmware_vm_facts-use_folder.yml b/changelogs/fragments/56125-vmware_vm_facts-use_folder.yml new file mode 100644 index 00000000000..d0339f5c526 --- /dev/null +++ b/changelogs/fragments/56125-vmware_vm_facts-use_folder.yml @@ -0,0 +1,2 @@ +minor_changes: +- vmware_vm_facts supports folder as a filter to gather fact for VM (https://github.com/ansible/ansible/issues/56125). diff --git a/lib/ansible/modules/cloud/vmware/vmware_vm_facts.py b/lib/ansible/modules/cloud/vmware/vmware_vm_facts.py index 4af7d913123..0241ef0cf2d 100644 --- a/lib/ansible/modules/cloud/vmware/vmware_vm_facts.py +++ b/lib/ansible/modules/cloud/vmware/vmware_vm_facts.py @@ -50,6 +50,21 @@ options: default: no type: bool version_added: 2.8 + folder: + description: + - Specify a folder location of VMs to gather facts from. + - 'Examples:' + - ' folder: /ha-datacenter/vm' + - ' folder: ha-datacenter/vm' + - ' folder: /datacenter1/vm' + - ' folder: datacenter1/vm' + - ' folder: /datacenter1/vm/folder1' + - ' folder: datacenter1/vm/folder1' + - ' folder: /folder1/datacenter1/vm' + - ' folder: folder1/datacenter1/vm' + - ' folder: /folder1/datacenter1/vm/folder2' + type: str + version_added: 2.9 extends_documentation_fragment: vmware.documentation ''' @@ -161,7 +176,14 @@ class VmwareVmFacts(PyVmomi): """ Get all virtual machines and related configurations information """ - virtual_machines = get_all_objs(self.content, [vim.VirtualMachine]) + folder = self.params.get('folder') + folder_obj = None + if folder: + folder_obj = self.content.searchIndex.FindByInventoryPath(folder) + if not folder_obj: + self.module.fail_json(msg="Failed to find folder specified by %(folder)s" % self.params) + + virtual_machines = get_all_objs(self.content, [vim.VirtualMachine], folder=folder_obj) _virtual_machines = [] for vm in virtual_machines: @@ -234,6 +256,7 @@ def main(): argument_spec.update( vm_type=dict(type='str', choices=['vm', 'all', 'template'], default='all'), show_attribute=dict(type='bool', default='no'), + folder=dict(type='str'), ) module = AnsibleModule( diff --git a/test/integration/targets/vmware_vm_facts/tasks/main.yml b/test/integration/targets/vmware_vm_facts/tasks/main.yml index 80fd76725fb..74430a421ce 100644 --- a/test/integration/targets/vmware_vm_facts/tasks/main.yml +++ b/test/integration/targets/vmware_vm_facts/tasks/main.yml @@ -10,8 +10,8 @@ setup_datastore: true setup_virtualmachines: true - -- name: Get facts about available vms +- &vm_data + name: Get facts about available vms vmware_vm_facts: validate_certs: false hostname: "{{ vcenter_hostname }}" @@ -21,7 +21,8 @@ - debug: var=vm_facts_0001 -- name: Verify if VM facts exist +- &vm_fact_check + name: Verify if VM facts exist assert: that: - "item.esxi_hostname is defined" @@ -36,17 +37,37 @@ vars: query: "[?guest_name=='DC0_H0_VM0']" +- <<: *vm_data + name: Get facts about available vms in check mode + check_mode: yes -- name: Get facts about available vms in check mode - vmware_vm_facts: +- <<: *vm_fact_check + name: Verify if VM facts exist in check mode + +- name: Get folder name from VM + vmware_guest_find: validate_certs: false hostname: "{{ vcenter_hostname }}" username: "{{ vcenter_username }}" password: "{{ vcenter_password }}" - register: vm_facts_0001 - check_mode: yes + name: "DC0_H0_VM0" + register: folder_path_info -- name: Verify if VM facts exist in check mode +- set_fact: + folder_path: "{{ folder_path_info.folders[0] }}" + when: folder_path_info.folders is defined + +- name: Gather facts about VM using folder + vmware_vm_facts: + hostname: "{{ vcenter_hostname }}" + username: "{{ vcenter_username }}" + password: "{{ vcenter_password }}" + validate_certs: no + folder: "{{ folder_path }}" + register: vm_facts + when: folder_path_info.folders is defined + +- name: Check if facts are returned for VM with folder specified assert: that: - "item.esxi_hostname is defined" @@ -56,8 +77,7 @@ - "item.power_state is defined" - "item.uuid is defined" - "item.vm_network is defined" - - "item.attributes is defined" with_items: - - "{{ vm_facts_0001.virtual_machines | json_query(query) }}" + - "{{ vm_facts.virtual_machines | json_query(query) }}" vars: query: "[?guest_name=='DC0_H0_VM0']"