Fetch VM Info from Ansible

421 views
Skip to first unread message

Pugazhendhi Ramakrishnan

unread,
Aug 2, 2022, 8:28:27 AM8/2/22
to Ansible Project

I'm using the "vmware_vm_info" module to fetch the VM name with the below playbook and it is working fine but if I try to add the ?guest_name=='vm_name' as variable it is not working. Can you please suggest how to generalize the play by adding the variable to the guestn_name?

---
- name: Check if the VM existing the Vcenter
  hosts: localhost
  vars_files: 1credentials.yml
  vars:
    vm_name: server2
  tasks:
    - name: Checking VM details
      block:
        - name: Get virtual machine info
          vmware_vm_info:
            hostname: '{{ vcenter_hostname }}'
            username: '{{ vcenter_username }}'
            password: '{{ vcenter_password }}'
            validate_certs: no
          delegate_to: localhost
          register: vm_info

        - debug:
            msg: "{{ item.guest_name }}"
          with_items:
            - "{{ vm_info.virtual_machines | json_query(query) }}"
          vars:
            query: "[?guest_name=='server2']"

Dick Visser

unread,
Aug 2, 2022, 11:47:18 AM8/2/22
to ansible...@googlegroups.com
Try backticks:

             query: "[?guest_name==`server2`]"

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/7291198e-2913-4dff-9aab-d8dba4a3d9f5n%40googlegroups.com.

Pugazhendhi Ramakrishnan

unread,
Aug 2, 2022, 11:55:51 AM8/2/22
to Ansible Project
No I mean, it is working if I give 'server2' but if I give the variable defined in the vars: section it is not working.

Walter Rowe

unread,
Aug 3, 2022, 2:11:20 PM8/3/22
to Ansible Project
Where are you specifying the vm_name in vmware_vm_info? ansible-doc shows this is a required parameter.

- name: Gather one specific VM
  community.vmware.vmware_vm_info:
    hostname: '{{ vcenter_hostname }}'
    username: '{{ vcenter_username }}'
    password: '{{ vcenter_password }}'
    vm_name: 'vm_name_as_per_vcenter'
  delegate_to: localhost
  register: vm_info

In your example you would need to use "{{ vm_name }}" as the value of the vm_name parameter.

- name: Gather one specific VM
  community.vmware.vmware_vm_info:
    hostname: '{{ vcenter_hostname }}'
    username: '{{ vcenter_username }}'
    password: '{{ vcenter_password }}'
    vm_name: '{{ vm_name }}'
  delegate_to: localhost
  register: vm_info

--
Walter Rowe, Chief
Infrastructure Services
Office of Information Systems Management
National Institute of Standards and Technology
United States Department of Commerce
Reply all
Reply to author
Forward
0 new messages