Dealing with list content

46 views
Skip to first unread message

Alex Wanderley

unread,
Aug 20, 2024, 4:00:56 PMAug 20
to ansible...@googlegroups.com
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

--

 

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.

Alex Wanderley

unread,
Aug 20, 2024, 6:16:52 PMAug 20
to ansible...@googlegroups.com
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

Vladimir Botka

unread,
Aug 20, 2024, 6:57:45 PMAug 20
to ansible...@googlegroups.com, Alex Wanderley
On Tue, 20 Aug 2024 16:16:17 -0600
Alex Wanderley <alex.wa...@edmonton.ca> wrote:

> 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...?

Try this

- copy:
dest: <path_to_file_here>
content: |
{% for i in vm_list.virtual_machines %}
{{ i.guest_name }}
{% endfor %}

See
https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_templating.html

--
Vladimir Botka

Nitesh Dudhe

unread,
Aug 21, 2024, 3:56:38 AMAug 21
to ansible...@googlegroups.com
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
 

Alex Wanderley

unread,
Aug 21, 2024, 2:49:51 PMAug 21
to ansible...@googlegroups.com
Thanks, Nitesh...!
I now have a much cleaner printout!

Alex

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CAGPd1LmKTJvoB5b3sWauUa6KBFpUKPVUTbPLkqBsC2KKdoYhGw%40mail.gmail.com.

Alex Wanderley

unread,
Aug 21, 2024, 2:57:54 PMAug 21
to Vladimir Botka, ansible...@googlegroups.com
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
Reply all
Reply to author
Forward
0 new messages