# Dealing with list content

**URL:** <https://forum.ansible.com/t/dealing-with-list-content/38857>\
**Category:** Ansible Project\
**Tags:** windows, vmware\
**Created:** [August 20, 2024, 8:00pm UTC](https://forum.ansible.com/t/dealing-with-list-content/38857 "2024-08-20T20:00:16Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alex\_Wanderley](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/alex_wanderley/32/3232_2.png) [@Alex\_Wanderley](https://forum.ansible.com/u/Alex_Wanderley)\
**Post date:** [August 20, 2024, 8:00pm UTC](https://forum.ansible.com/t/dealing-with-list-content/38857/1 "2024-08-20T20:00:16Z")

</div>

Hello,

I understand this is the basics, but I’m still learning… Could anybody help?

This play collects all VMs in a VMware cluster and prints their names:

- name: List VMs in cluster  
become: no  
hosts: localhost  
gather\_facts: no  
connection: local

tasks:

- name: Collect list of VMs in cluster  
community.vmware.vmware\_vm\_info:  
validate\_certs: false  
register: vm\_list

- name: Print list  
ansible.builtin.debug:  
msg: “{{ item[‘guest\_name’] }}”  
loop: “{{ vm\_list.virtual\_machines }}”

Its output is looking like:

ok: [localhost] =\> (item={‘guest\_name’: ‘xxxxxx’, ‘guest\_fullname’: ‘Microsoft Windows Server 2022 (64-bit)’, ‘power\_state’: ‘poweredOn’, ‘ip\_address’: ‘xxxxxx’, ‘mac\_address’: [‘xxxxxx’], ‘uuid’: ‘xxxxxx’, ‘instance\_uuid’: ‘xxxxxx’, ‘vm\_network’: {‘xxxxxx’: {‘ipv4’: [‘xxxxxx’], ‘ipv6’: }}, ‘esxi\_hostname’: ‘xxxxxx’, ‘datacenter’: ‘xxxxxx’, ‘cluster’: ‘xxxxxx’, ‘resource\_pool’: None, ‘attributes’: {}, ‘tags’: , ‘folder’: ‘xxxxxx’, ‘moid’: ‘xxxxxx’, ‘datastore\_url’: [{‘name’: ‘xxxxxx’, ‘url’: ‘xxxxxx’}], ‘allocated’: {}}) =\> {  
“msg”: “server01”  
}  
ok: [localhost] =\> (item={‘guest\_name’: ‘xxxxxx’, ‘guest\_fullname’: ‘Microsoft Windows Server 2022 (64-bit)’, ‘power\_state’: ‘poweredOn’, ‘ip\_address’: ‘xxxxxx’, ‘mac\_address’: [‘xxxxxx’], ‘uuid’: ‘xxxxxx’, ‘instance\_uuid’: ‘xxxxxx’, ‘vm\_network’: {‘xxxxxx’: {‘ipv4’: [‘xxxxxx’], ‘ipv6’: }}, ‘esxi\_hostname’: ‘xxxxxx’, ‘datacenter’: ‘xxxxxx’, ‘cluster’: ‘xxxxxx’, ‘resource\_pool’: None, ‘attributes’: {}, ‘tags’: , ‘folder’: ‘xxxxxx’, ‘moid’: ‘xxxxxx’, ‘datastore\_url’: [{‘name’: ‘xxxxxx’, ‘url’: ‘xxxxxx’}], ‘allocated’: {}}) =\> {  
“msg”: “server02”  
}  
ok: [localhost] =\> (item={‘guest\_name’: ‘xxxxxx’, ‘guest\_fullname’: ‘Microsoft Windows Server 2022 (64-bit)’, ‘power\_state’: ‘poweredOn’, ‘ip\_address’: ‘xxxxxx’, ‘mac\_address’: [‘xxxxxx’], ‘uuid’: ‘xxxxxx’, ‘instance\_uuid’: ‘xxxxxx’, ‘vm\_network’: {‘xxxxxx’: {‘ipv4’: [‘xxxxxx’], ‘ipv6’: }}, ‘esxi\_hostname’: ‘xxxxxx’, ‘datacenter’: ‘xxxxxx’, ‘cluster’: ‘xxxxxx’, ‘resource\_pool’: None, ‘attributes’: {}, ‘tags’: , ‘folder’: ‘xxxxxx’, ‘moid’: ‘xxxxxx’, ‘datastore\_url’: [{‘name’: ‘xxxxxx’, ‘url’: ‘xxxxxx’}], ‘allocated’: {}}) =\> {  
“msg”: “server03”  
}

Two questions, if you don’t mind…:  
01 - How, if even possible, could I have the output looking like something like this?

ok: [localhost]

msg": “server01”

ok: [localhost]

msg": “server02”

ok: [localhost]

msg": “server03”

02 - Would it be possible to send the loop output to a file?  
I know that the whole “{{ vm\_list }}” content can be send to a file using:

- name: Copy list to file  
ansible.builtin.copy:  
content: “{{ vm\_list | to\_yaml }}”  
dest: “{{ path\_to\_file }}”

But I’d like to create a file with the already parsed content

Thanks a lot,

Alex

---

<div class="post-metadata">

**Author:** ![Alex\_Wanderley](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/alex_wanderley/32/3232_2.png) [@Alex\_Wanderley](https://forum.ansible.com/u/Alex_Wanderley)\
**Post date:** [August 20, 2024, 10:16pm UTC](https://forum.ansible.com/t/dealing-with-list-content/38857/2 "2024-08-20T22:16:17Z")

</div>

Hi,

OK, answering my own question, I was able to send the parsed vm\_list content to a file using this task:

- name: Copy list to file  
ansible.builtin.lineinfile:  
dest: \<path\_to\_file\_here\>  
line: “{{ item[‘guest\_name’] }}”  
state: present  
create: yes  
loop: “{{ vm\_list.virtual\_machines }}”

There would be a different/better way of accomplishing that…?

Thanks again,

Alex

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [August 20, 2024, 10:57pm UTC](https://forum.ansible.com/t/dealing-with-list-content/38857/3 "2024-08-20T22:57:16Z")

</div>

Try this

&nbsp;&nbsp;- copy:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dest: \<path\_to\_file\_here\>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;content: |  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{% for i in vm\_list.virtual\_machines %}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{{ i.guest\_name }}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{% endfor %}

See  
[https://docs.ansible.com/ansible/latest/playbook\_guide/playbooks\_templating.html](https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_templating.html)

---

<div class="post-metadata">

**Author:** ![Nitesh\_Dudhe](https://avatars.discourse-cdn.com/v4/letter/n/97f17d/32.png) [@Nitesh\_Dudhe](https://forum.ansible.com/u/Nitesh_Dudhe)\
**Post date:** [August 21, 2024, 6:47am UTC](https://forum.ansible.com/t/dealing-with-list-content/38857/4 "2024-08-21T06:47:00Z")

</div>

Dear Alex,

For your output minimisation:  
you can arrange your task like this:

- name: Collect list of VMs in cluster  
community.vmware.vmware\_vm\_info:  
validate\_certs: false  
register: vm\_list

- name: Print list  
ansible.builtin.debug:  
msg: “{{ item[‘guest\_name’] }}”  
loop: “{{ vm\_list.virtual\_machines }}”

loop\_control:  
label: “Collected vm-name” # either you put empty string or some relevant string

output will be like this:

ok: [localhost] =\> (item=Collected vm-name)

msg": “server01”

ok: [localhost] =\> (item=Collected vm-name)

msg": “server02”

ok: [localhost] =\> (item=Collected vm-name)

msg": “server03”

no more unwanted text during the debug for each iteration:  
eg. (item={‘guest\_name’: ‘xxxxxx’, ‘guest\_fullname’: ‘Microsoft Windows Server 2022 (64-bit)’, ‘power\_state’: ‘poweredOn’, ‘ip\_address’: ‘xxxxxx’, ‘mac\_address’: [‘xxxxxx’], ‘uuid’: ‘xxxxxx’, ‘instance\_uuid’: ‘xxxxxx’, ‘vm\_network’: {‘xxxxxx’: {‘ipv4’: [‘xxxxxx’], ‘ipv6’: }}, ‘esxi\_hostname’: ‘xxxxxx’, ‘datacenter’: ‘xxxxxx’, ‘cluster’: ‘xxxxxx’, ‘resource\_pool’: None, ‘attributes’: {}, ‘tags’: , ‘folder’: ‘xxxxxx’, ‘moid’: ‘xxxxxx’, ‘datastore\_url’: [{‘name’: ‘xxxxxx’, ‘url’: ‘xxxxxx’}], ‘allocated’: {}}) =\>

I hope this helps

Regards  
Nitesh Dudhe

---

<div class="post-metadata">

**Author:** ![Alex\_Wanderley](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/alex_wanderley/32/3232_2.png) [@Alex\_Wanderley](https://forum.ansible.com/u/Alex_Wanderley)\
**Post date:** [August 21, 2024, 6:49pm UTC](https://forum.ansible.com/t/dealing-with-list-content/38857/5 "2024-08-21T18:49:12Z")

</div>

Thanks, Nitesh…!  
I now have a much cleaner printout!

Alex

---

<div class="post-metadata">

**Author:** ![Alex\_Wanderley](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/alex_wanderley/32/3232_2.png) [@Alex\_Wanderley](https://forum.ansible.com/u/Alex_Wanderley)\
**Post date:** [August 21, 2024, 6:57pm UTC](https://forum.ansible.com/t/dealing-with-list-content/38857/6 "2024-08-21T18:57:15Z")

</div>

Thanks, Vladimir!

That worked really well.  
It generated the file much faster than the lineinfile alternative.

Thank you as well for the templating doc/link.  
After I figure out what info my “client” wants to extract from VMware, building a report will be the next step and so far I’m planning to use templates for that.

Alex
