Skip to content

Commit

Permalink
VMware: support folder param in vmware_vm_facts (ansible#56298)
Browse files Browse the repository at this point in the history
folder can be used as a filter while gathering facts about VMs.

Fixes: ansible#56125

Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>
  • Loading branch information
Akasurde authored and bcoca committed Feb 21, 2020
1 parent 1f7b2a3 commit b617894
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 11 deletions.
2 changes: 2 additions & 0 deletions 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).
25 changes: 24 additions & 1 deletion lib/ansible/modules/cloud/vmware/vmware_vm_facts.py
Expand Up @@ -50,6 +50,21 @@
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
'''

Expand Down Expand Up @@ -161,7 +176,14 @@ def get_all_virtual_machines(self):
"""
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:
Expand Down Expand Up @@ -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(
Expand Down
40 changes: 30 additions & 10 deletions test/integration/targets/vmware_vm_facts/tasks/main.yml
Expand Up @@ -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 }}"
Expand All @@ -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"
Expand All @@ -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

- <<: *vm_fact_check
name: Verify if VM facts exist in check mode

- name: Get facts about available vms in check mode
vmware_vm_facts:
- 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

- set_fact:
folder_path: "{{ folder_path_info.folders[0] }}"
when: folder_path_info.folders is defined

- name: Verify if VM facts exist in check mode
- 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"
Expand All @@ -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']"

0 comments on commit b617894

Please sign in to comment.