# Help with parsing "vmware\_vm\_info" output

**URL:** <https://forum.ansible.com/t/help-with-parsing-vmware-vm-info-output/37954>\
**Category:** Ansible Project\
**Tags:** vmware\
**Created:** [May 2, 2023, 6:00pm UTC](https://forum.ansible.com/t/help-with-parsing-vmware-vm-info-output/37954 "2023-05-02T18:00:09Z")\
**Posts on this page:** 3\
**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:** [May 2, 2023, 6:00pm UTC](https://forum.ansible.com/t/help-with-parsing-vmware-vm-info-output/37954/1 "2023-05-02T18:00:09Z")

</div>

Hello,

I need to collect some info from VMs in our VMware cluster and am having trouble figuring out how to print only the information I need.  
When running the code below:

- name: collect VMs in specific folder  
community.vmware.vmware\_vm\_info:  
validate\_certs: False  
hostname: “{{ vcenter\_hostname }}”  
username: “{{ vcenter\_username }}”  
password: “{{ vcenter\_passwd }}”  
folder: “{{ vcenter\_folder }}”  
delegate\_to: localhost  
register: vm\_info

- name: print VM info  
debug:  
msg: “{{ item[‘guest\_name’] }}”  
with\_items: “{{ vm\_info.virtual\_machines }}”

I’m getting this result:

TASK [print VM info] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost] =\> (item={‘guest\_name’: ‘rhel8tst’, ‘guest\_fullname’: ‘Red Hat Enterprise Linux 8 (64-bit)’, ‘power\_state’: ‘poweredOn’, ‘ip\_address’: ‘xxx.xxx.xxx.xxx’, ‘mac\_address’: [‘xxx’], ‘uuid’: ‘422c523e-7bb0-1f95-1c16-742c30ba445d’, ‘vm\_network’: {‘xxx’: {‘ipv4’: [‘xxx.xxx.xxx.xxx’], ‘ipv6’: [‘xxx’]}}, ‘esxi\_hostname’: ‘.xxxx’, ‘datacenter’: ‘xxxx’, ‘cluster’: ‘xxxxx’, ‘resource\_pool’: None, ‘attributes’: {}, ‘tags’: , ‘folder’: None, ‘moid’: ‘vm-502018’, ‘datastore\_url’: [{‘name’: ‘xxxx’, ‘url’: ‘xxx’}], ‘allocated’: {}}) =\> {  
“msg”: “rhel8tst”  
}

How could I have (in this case) only the VM name “rhel8tst” printed out?

Thanks a lot,

Alex

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [May 2, 2023, 6:38pm UTC](https://forum.ansible.com/t/help-with-parsing-vmware-vm-info-output/37954/2 "2023-05-02T18:38:11Z")

</div>

The debug module prints to the job log. It’s a common mistake to try to use the job log as formatted output for other purposes, like reports. Instead you could use the ansible.builtin.template module to output the desired data in some other file. Or you can take the often-warned-against shortcut of using the copy module to do the same, like so:

```auto
---
- name: Example
  hosts: localhost
  gather_facts: false
  vars:
    vm_info:
      virtual_machines:
        - {'guest_name': 'rhel8tst', 'guest_fullname': 'Red Hat Enterprise Linux 8 (64-bit)', 'power_state': 'poweredOn', 'ip_address': 'xxx.xxx.xxx.xxx', 'mac_address': ['xxx'], 'uuid': '422c523e-7bb0-1f95-1c16-742c30ba445d', 'vm_network': {'xxx': {'ipv4': ['xxx.xxx.xxx.xxx'], 'ipv6': ['xxx']}}, 'esxi_hostname': '.xxxx', 'datacenter': 'xxxx', 'cluster': 'xxxxx', 'resource_pool': None, 'attributes': {}, 'tags': [], 'folder': None, 'moid': 'vm-502018', 'datastore_url': [{'name': 'xxxx', 'url': 'xxx'}], 'allocated': {}}
  tasks:
    - name: Show virtual machines guest_name in job log
      ansible.builtin.debug:
        msg: "{{ item['guest_name'] }}"
      with_items: "{{ vm_info.virtual_machines }}"
    - name: Print virtual machines guest_name in output file
      ansible.builtin.copy: # should really use template here
        content: |
            Virtual Machine Names
            {% for vm in vm_info.virtual_machines %}
            - {{ vm['guest_name'] }}
            {% endfor %}
        dest: /tmp/virtual_machine_names.txt

```

---

<div class="post-metadata">

**Author:** ![area51coder](https://avatars.discourse-cdn.com/v4/letter/a/a88e57/32.png) [@area51coder](https://forum.ansible.com/u/area51coder)\
**Post date:** [May 3, 2023, 4:32pm UTC](https://forum.ansible.com/t/help-with-parsing-vmware-vm-info-output/37954/3 "2023-05-03T16:32:20Z")

</div>

I have just tested as suggested by Todd, it is working fine for me.  
below is the yml github link for the same.

[https://github.com/area51coder/ansible\_group\_issues](https://github.com/area51coder/ansible_group_issues)

Regards  
Ravi Y  
[yoursmartbook.blogspot.com](http://yoursmartbook.blogspot.com)

[root ~]# cat /etc/ansible/reports/virtual\_machine\_names.txt  
Virtual Machine Names

- windows10
- windows server 2008
- windows server 2012 client
- Centos 7-test new-29April2023
- ESXi6.7
- windows server 2012
- New Virtual Machine
- CentosTowerRedHat\_192.168.29.71\_134
- Centos-7-Ansible new lab\_181
- testvm\_1
- windows\_test
- Ubuntu\_Node2
- CentOS\_Node1\_192.168.29.237
