Score:2

Vmware tags and Ansible , empty out in Ansible but VMs have tags

se flag

I am trying to filter out the list of VMs with specific tags. The sample playbook is as follows :

 - hosts: localhost
   gather_facts: false
   become: false
   vars:
      vcenter_hostname: vcenter3423
      vcenter_username: center\user4325
      vcenter_password: L0gM3In4325
   tasks:
     - name: Gather all registered virtual machines
       community.vmware.vmware_vm_info:
         hostname: '{{ vcenter_hostname }}'
         username: '{{ vcenter_username }}'
         password: '{{ vcenter_password }}'
         validate_certs: no
       delegate_to: localhost
       register: vminfo

     - debug:
         msg: "{{ item.guest_name }}, {{ item.ip_address }}, {{ item.guest_fullname }}, {{ item.datacenter }}, {{ item.power_state }}, {{ item.tags }} "
       with_items:
         - "{{ vminfo.virtual_machines }} | community.general.json_query(query) }}"       
       vars:
         query: "[?guest_name=='CMCUAT']"

However, whatever combination I may try the tags[] output, the rest of the o/p is populated. Not sure what I am missing

Score:1
by flag

There could be multiple reasons, make sure you have community installed ansible-galaxy collection install community.general also modify debug to use json_query and you should add a separate query for filtering your VMs

here is your file, replace specific_tag in the query with the tag you are looking for

- hosts: localhost
  gather_facts: false
  become: false
  vars:
    vcenter_hostname: vcenter3423
    vcenter_username: center\user4325
    vcenter_password: L0gM3In4325
  tasks:
    - name: Gather all registered virtual machines
      community.vmware.vmware_vm_info:
        hostname: '{{ vcenter_hostname }}'
        username: '{{ vcenter_username }}'
        password: '{{ vcenter_password }}'
        validate_certs: no
      delegate_to: localhost
      register: vminfo

    - debug:
        msg: "{{ item.guest_name }}, {{ item.ip_address }}, {{ item.guest_fullname }}, {{ item.datacenter }}, {{ item.power_state }}, {{ item.tags }} "
      with_items: "{{ vminfo.virtual_machines | community.general.json_query(query) }}"
      vars:
        query: "[?guest_name=='CMCUAT' && tags[?contains(@, 'specific_tag')]]"
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.