# Ansible Get vSphere Virtual Machines

**URL:** <https://forum.ansible.com/t/ansible-get-vsphere-virtual-machines/32634>\
**Category:** Ansible Project\
**Created:** [April 8, 2020, 11:12am UTC](https://forum.ansible.com/t/ansible-get-vsphere-virtual-machines/32634 "2020-04-08T11:12:06Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![David\_Foley](https://avatars.discourse-cdn.com/v4/letter/d/9e8a1a/32.png) [@David\_Foley](https://forum.ansible.com/u/David_Foley)\
**Post date:** [April 8, 2020, 11:12am UTC](https://forum.ansible.com/t/ansible-get-vsphere-virtual-machines/32634/1 "2020-04-08T11:12:06Z")

</div>

Hi All,

Trying to Output only the Virtual Machine Name: but having issues with the Json output

`

---

<div class="post-metadata">

**Author:** ![Gobi](https://avatars.discourse-cdn.com/v4/letter/g/76d3ee/32.png) [@Gobi](https://forum.ansible.com/u/Gobi)\
**Post date:** [April 8, 2020, 11:32am UTC](https://forum.ansible.com/t/ansible-get-vsphere-virtual-machines/32634/2 "2020-04-08T11:32:08Z")

</div>

Hi David,

virtual\_machines is a list.  
This will work:

- debug:  
var: vminfo.virtual\_machines[0].guest\_name

---

<div class="post-metadata">

**Author:** ![David\_Foley](https://avatars.discourse-cdn.com/v4/letter/d/9e8a1a/32.png) [@David\_Foley](https://forum.ansible.com/u/David_Foley)\
**Post date:** [April 8, 2020, 11:46am UTC](https://forum.ansible.com/t/ansible-get-vsphere-virtual-machines/32634/3 "2020-04-08T11:46:37Z")

</div>

> Hi Gobi

that outputs the first on the Json List, how would i do a loop ?

i Tried with\_items

```auto
 - debug:
        msg: "{{ item.tags }}"
      with_items:
        - "{{ vm_info.virtual_machines.guest_name }}"

```

---

<div class="post-metadata">

**Author:** ![Gobi](https://avatars.discourse-cdn.com/v4/letter/g/76d3ee/32.png) [@Gobi](https://forum.ansible.com/u/Gobi)\
**Post date:** [April 8, 2020, 11:56am UTC](https://forum.ansible.com/t/ansible-get-vsphere-virtual-machines/32634/4 "2020-04-08T11:56:09Z")

</div>

Hi David,  
You can do a loop like this

- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: "{{ item.tags }}"  
&nbsp;&nbsp;&nbsp;with\_items:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- "{{ vm\_info.virtual\_machines }}"

---

<div class="post-metadata">

**Author:** ![David\_Foley](https://avatars.discourse-cdn.com/v4/letter/d/9e8a1a/32.png) [@David\_Foley](https://forum.ansible.com/u/David_Foley)\
**Post date:** [April 8, 2020, 12:13pm UTC](https://forum.ansible.com/t/ansible-get-vsphere-virtual-machines/32634/5 "2020-04-08T12:13:15Z")

</div>

> Hi Gobi,

I was using that above, but it outputs all information,  
I was trying to only output the guest\_name with the with\_item loop

---

<div class="post-metadata">

**Author:** ![Mani](https://avatars.discourse-cdn.com/v4/letter/m/b4bc9f/32.png) [@Mani](https://forum.ansible.com/u/Mani)\
**Post date:** [April 30, 2020, 10:56am UTC](https://forum.ansible.com/t/ansible-get-vsphere-virtual-machines/32634/6 "2020-04-30T10:56:18Z")

</div>

Hi David,

You can try the below loop:

- debug:  
msg: “{{ item.guest\_name }}”  
with\_items:
- “{{ vm\_info.virtual\_machines }}”
