mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
VMware: support folder param in vmware_vm_facts (#56298)
folder can be used as a filter while gathering facts about VMs. Fixes: #56125 Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>
This commit is contained in:
parent
f5326aa6cd
commit
9f86257a65
3 changed files with 56 additions and 11 deletions
|
@ -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).
|
|
@ -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(
|
||||
|
|
|
@ -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']"
|
||||
|
|
Loading…
Reference in a new issue