# Fetch VM Info from Ansible

**URL:** <https://forum.ansible.com/t/fetch-vm-info-from-ansible/36932>\
**Category:** Ansible Project\
**Created:** [August 2, 2022, 12:28pm UTC](https://forum.ansible.com/t/fetch-vm-info-from-ansible/36932 "2022-08-02T12:28:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pugazhendhi\_Ramakris](https://avatars.discourse-cdn.com/v4/letter/p/f05b48/32.png) [@Pugazhendhi\_Ramakris](https://forum.ansible.com/u/Pugazhendhi_Ramakris)\
**Post date:** [August 2, 2022, 12:28pm UTC](https://forum.ansible.com/t/fetch-vm-info-from-ansible/36932/1 "2022-08-02T12:28:26Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![dnmvisser](https://avatars.discourse-cdn.com/v4/letter/d/8e8cbc/32.png) [@dnmvisser](https://forum.ansible.com/u/dnmvisser)\
**Post date:** [August 2, 2022, 3:46pm UTC](https://forum.ansible.com/t/fetch-vm-info-from-ansible/36932/2 "2022-08-02T15:46:46Z")

</div>

> 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’]”

Try backticks:

query: “[?guest\_name==`server2`]”

---

<div class="post-metadata">

**Author:** ![Pugazhendhi\_Ramakris](https://avatars.discourse-cdn.com/v4/letter/p/f05b48/32.png) [@Pugazhendhi\_Ramakris](https://forum.ansible.com/u/Pugazhendhi_Ramakris)\
**Post date:** [August 2, 2022, 3:55pm UTC](https://forum.ansible.com/t/fetch-vm-info-from-ansible/36932/3 "2022-08-02T15:55:51Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Walter\_Rowe](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/walter_rowe/32/4506_2.png) [@Walter\_Rowe](https://forum.ansible.com/u/Walter_Rowe)\
**Post date:** [August 3, 2022, 6:11pm UTC](https://forum.ansible.com/t/fetch-vm-info-from-ansible/36932/4 "2022-08-03T18:11:20Z")

</div>

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**
