Help with parsing "vmware_vm_info" output

112 views
Skip to first unread message

Alex Wanderley

unread,
May 2, 2023, 2:00:41 PM5/2/23
to ansible...@googlegroups.com
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
--

 

Edmonton_sig_RGB_S.jpg

Alex Wanderley

Application and Infrastructure Analyst II
Server Solutions & Automation

Financial and Corporate Services | Open City and Technology  

 

780-496-4156  Office

780-819-0273  Mobile

 

City of Edmonton

Century Place, 19th Floor

9803 102A Avenue NW

Edmonton AB, T5J 3A3

 

All information contained in this email post is proprietary to the City of Edmonton, confidential and intended only for the addressed recipient. If you have received this post in error, please disregard the contents, inform the sender of the misdirection, and remove it from your system. The copying, dissemination or distribution of this email, if misdirected, is strictly prohibited.


The contents of this message and any attachment(s) are confidential, proprietary to the City of Edmonton, and are intended only for the addressed recipient. If you have received this in error, please disregard the contents, inform the sender of the misdirection, and remove it from your system. The copying, dissemination, or distribution of this message, if misdirected, is strictly prohibited.

Todd Lewis

unread,
May 2, 2023, 2:38:36 PM5/2/23
to ansible...@googlegroups.com, Alex Wanderley, uto...@gmail.com
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:
---
- 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

Ravi yadav

unread,
May 3, 2023, 12:32:20 PM5/3/23
to Ansible Project
I have just tested as suggested by Todd, it is working fine for me.
below is the yml github link for the same.



Regards
Ravi Y

[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

Reply all
Reply to author
Forward
0 new messages