# How to match exactly a tags name?

**URL:** <https://forum.ansible.com/t/how-to-match-exactly-a-tags-name/40182>\
**Category:** Get Help\
**Tags:** tags, vmware\
**Created:** [February 11, 2025, 4:06pm UTC](https://forum.ansible.com/t/how-to-match-exactly-a-tags-name/40182 "2025-02-11T16:06:24Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![ansibliano](https://avatars.discourse-cdn.com/v4/letter/a/eb9ed0/32.png) [@ansibliano](https://forum.ansible.com/u/ansibliano)\
**Post date:** [February 11, 2025, 4:06pm UTC](https://forum.ansible.com/t/how-to-match-exactly-a-tags-name/40182/1 "2025-02-11T16:06:24Z")

</div>

Hi, I am trying to create an inventory from vmware

This is my partial code on ansible playbook

```auto
- name: vm read on VMWARE
  hosts: localhost
  become: false
  gather_facts: true
  vars_files:
    - secrets.yml
    - vars.yml
  collections:
    - community.vmware
  tasks:
    - name: Gather all registered virtual machines
      vmware_vm_info:
        hostname: "{{ vcenter_hostname }}"
        username: "{{ vcenter_username }}"
        password: "{{ vcenter_password }}"
        show_tag: true
        validate_certs: no
      delegate_to: localhost
      register: vm_info

    - name: Filter VMs with tag 'tagtoprocess'
      set_fact:
        filtered_vms: >-
          {{
            vm_info.virtual_machines
            | selectattr('tags', 'defined')
            | selectattr('tags', 'search', 'tagtoprocess')
            | list
          }}

```

This is the structure of my vm machines extractions:

```auto
    "ansible_facts": {
        "filtered_vms": [
            {
				.
				.
				.				
                "resource_pool": null,
                "tags": [
                    {
                        "category_id": "urn:vmomi:InventoryServiceCategory:dfjfjdfjd",
                        "category_name": "Ansible",
                        "description": "",
                        "id": "urn:vmomi:InventoryServiceTag:3ecce703-3dkfkkkfk:GLOBAL",
                        "name": "tagtoprocess-nosnmp"
                    },
                    {
                        "category_id": "urn:vmomi:InventoryServiceCategory:fc34d73f-4182-432f-b042-1e5f47fc4421:GLOBAL",
                        "category_name": "Backupp",
                        "description": "VmSecon",
                        "id": "urn:vmomi:InventoryServiceTag:6820fb9e-46b1-4eb9-8e6f-e6009ea61f3f:GLOBAL",
                        "name": "differenttag"
                    }
                ],
				.
				.
				.

```

As you can see, it matches with the tags tagtoprocess-nosnmp but I don’t like this.  
How could I change my code matching exactly the machines with tags.name=tagtoprocess?

I think tags.name isn’t right

How could I solve?  
Thanks a lot for your help

---

<div class="post-metadata">

**Author:** ![markuman](https://avatars.discourse-cdn.com/v4/letter/m/7bcc69/32.png) [@markuman](https://forum.ansible.com/u/markuman)\
**Post date:** [February 11, 2025, 4:08pm UTC](https://forum.ansible.com/t/how-to-match-exactly-a-tags-name/40182/2 "2025-02-11T16:08:46Z")

</div>

> [@ansibliano](#):
>
> `selectattr('tags', 'search', 'tagtoprocess')`

```auto
selectattr('tags', '==', 'tagtoprocess')

```

---

<div class="post-metadata">

**Author:** ![markuman](https://avatars.discourse-cdn.com/v4/letter/m/7bcc69/32.png) [@markuman](https://forum.ansible.com/u/markuman)\
**Post date:** [February 11, 2025, 4:12pm UTC](https://forum.ansible.com/t/how-to-match-exactly-a-tags-name/40182/3 "2025-02-11T16:12:26Z")

</div>

Next to building your own inventory, have you consider using the existing dynamic inventory plugin `community.vmware.vmware_vm_inventory`?

You can use there also filter and apply tags to groups

```yml
plugin: community.vmware.vmware_vm_inventory
...
filters:
  - runtime.powerState == "poweredOn"
  - "tag_category.AD_ZONE is defined"
...
keyed_groups:
  - key: tag_category.AD_ZONE

```

[https://docs.ansible.com/ansible/latest/collections/community/vmware/vmware\_vm\_inventory\_inventory.html](https://docs.ansible.com/ansible/latest/collections/community/vmware/vmware_vm_inventory_inventory.html)

---

<div class="post-metadata">

**Author:** ![ansibliano](https://avatars.discourse-cdn.com/v4/letter/a/eb9ed0/32.png) [@ansibliano](https://forum.ansible.com/u/ansibliano)\
**Post date:** [February 11, 2025, 4:31pm UTC](https://forum.ansible.com/t/how-to-match-exactly-a-tags-name/40182/4 "2025-02-11T16:31:57Z")

</div>

Thanks for your answer.

With this I obtain an empty list:

```auto
ok: [localhost] => {
    "ansible_facts": {
        "filtered_vms": []
    },
    "changed": false
}

```

so an empty list because the tag I am looking for is on the key = name with value ‘tagtoprocess’

About here [community.vmware.vmware\_vm\_inventory inventory – VMware Guest inventory source — Ansible Community Documentation](https://docs.ansible.com/ansible/latest/collections/community/vmware/vmware_vm_inventory_inventory.html)

how to filter the machines where tags name=tagtoprocess

thanks a lot

---

<div class="post-metadata">

**Author:** ![mariolenz](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/mariolenz/32/108_2.png) [@mariolenz](https://forum.ansible.com/u/mariolenz)\
**Post date:** [February 11, 2025, 6:40pm UTC](https://forum.ansible.com/t/how-to-match-exactly-a-tags-name/40182/5 "2025-02-11T18:40:34Z")

</div>

> [@markuman](#):
>
> Next to building your own inventory, have you consider using the existing dynamic inventory plugin `community.vmware.vmware_vm_inventory`?

We’re about to [deprecate this inventory plugin](https://github.com/ansible-collections/community.vmware/pull/2283) in favor of [vmware.vmware.vms](https://galaxy.ansible.com/ui/repo/published/vmware/vmware/content/inventory/vms/) 🤫

---

<div class="post-metadata">

**Author:** ![markuman](https://avatars.discourse-cdn.com/v4/letter/m/7bcc69/32.png) [@markuman](https://forum.ansible.com/u/markuman)\
**Post date:** [February 12, 2025, 5:42am UTC](https://forum.ansible.com/t/how-to-match-exactly-a-tags-name/40182/6 "2025-02-12T05:42:14Z")

</div>

> [@mariolenz](#):
>
> We’re about to [deprecate this inventory plugin](https://github.com/ansible-collections/community.vmware/pull/2283) in favor of [vmware.vmware.vms](https://galaxy.ansible.com/ui/repo/published/vmware/vmware/content/inventory/vms/) 🤫

Oh, didn’t use vmware for more than 4 years now 😃

> how to filter the machines where tags name=tagtoprocess

You must turn on the `gather_tags` parameter. I have no access to a vmware host anymore, so I cannot try it myself.

---

<div class="post-metadata">

**Author:** ![ansibliano](https://avatars.discourse-cdn.com/v4/letter/a/eb9ed0/32.png) [@ansibliano](https://forum.ansible.com/u/ansibliano)\
**Post date:** [February 12, 2025, 9:00am UTC](https://forum.ansible.com/t/how-to-match-exactly-a-tags-name/40182/7 "2025-02-12T09:00:53Z")

</div>

Oh, I didn’t know  
So…how could I solve my issue with the supported plugin.

I would like to change this:

```auto
    - name: Filter VMs with tag 'tagtoprocess'
      set_fact:
        filtered_vms: >-
          {{
            vm_info.virtual_machines
            | selectattr('tags', 'defined')
            | selectattr('tags', 'search', 'tagtoprocess')
            | list
          }}

```

to match exactly the tag name tagtoprocess.  
I didn’t find any examples for this.  
☹  
Thanks a lot  
Mario

---

<div class="post-metadata">

**Author:** ![markuman](https://avatars.discourse-cdn.com/v4/letter/m/7bcc69/32.png) [@markuman](https://forum.ansible.com/u/markuman)\
**Post date:** [February 12, 2025, 9:46am UTC](https://forum.ansible.com/t/how-to-match-exactly-a-tags-name/40182/8 "2025-02-12T09:46:56Z")

</div>

Start with the very basic inventory [vmware.vmware.vms inventory – Create an inventory containing VMware VMs — Ansible Community Documentation](https://docs.ansible.com/ansible/latest/collections/vmware/vmware/vms_inventory.html)

```yml
---
plugin: vmware.vmware.vms
hostname: 10.65.223.31
username: administrator@vsphere.local
password: Esxi@123$%
validate_certs: false
gather_tags: true

```

And do

```auto
ansible-inventory -i basic.vmware_vms.yml --list

```

Once this works and you see your vmware hosts, try e.g. to group your vms by tags

```yml
---
plugin: vmware.vmware.vms
hostname: 10.65.223.31
username: administrator@vsphere.local
password: Esxi@123$%
validate_certs: false
gather_tags: true
keyed_groups:
  - key: tags_by_category.tagtoprocess
    separator: ""

```

`tags_by_category.tagtoprocess` depends on the structure that is returned by `ansible-inventory` with the `--list` option.  
So it’s bit blind boiler templating here …

In general, hosts with the related tag should be grouped in the `tagtoprocess` group.  
`--graph` should nicely display that.

```auto
ansible-inventory -i basic.vmware_vms.yml --graph

```

Once that works, your playbook can look like that

```auto
---
- hosts: tagtoprocess

  tasks:
    ....

```

with `ansible-playbook -i basic.vmware_vms.yml your_play.yml`

Another option is, to target `hosts: all` and use `--limit tagstoprocess` in your `ansible-playbook` account.

* * *

Our proxmox dynamic inventory looks like the

```yml
---
plugin: community.general.proxmox
url: .......
validate_certs: false
user: "{{ lookup('onepassword', 'proxmox', vault='Employee', field='username') }}"
token_id: "{{ lookup('onepassword', 'proxmox', vault='Employee', field='token_id') }}"
token_secret: "{{ lookup('onepassword', 'proxmox', vault='Employee', field='password') }}"
want_facts: true
keyed_groups:
  - key: proxmox_tags_parsed
    separator: ""
# use IP address to connect, when DNS is not set yet
want_proxmox_nodes_ansible_host: false
compose:
  ansible_host: proxmox_agent_interfaces[1]["ip-addresses"][0] | ansible.utils.ipaddr('address')

```

So our proxmox tags results here also in ansible groups

While our aws dynamic inventory looks like that

```yml
---
plugin: aws_ec2
cache: true
groups:
  linux: true
regions:
  - eu-central-1
hostnames:
  - tag:Name
filters:
  instance-state-name:
    - running
  tag:ansible_group:
    - ubuntu
    - oraclelinux
    - opnsense
keyed_groups:
  - key: tags['ansible_group']
    separator: ''
  - key: tags['infra_system']
    separator: ''
  - key: placement.availability_zone
    separator: ''
exclude_filters:
  - key-name:
    - 'windowsserver'
compose:
  ansible_ssh_extra_args: '-o StrictHostKeyChecking=accept-new'

```

Our self managed dns servers got the tags `infra_system: dns`.  
And we can address them with `ansible-playbook -i hosts.aws_ec2.yml some_play.yml --limit dns`

---

<div class="post-metadata">

**Author:** ![ansibliano](https://avatars.discourse-cdn.com/v4/letter/a/eb9ed0/32.png) [@ansibliano](https://forum.ansible.com/u/ansibliano)\
**Post date:** [February 12, 2025, 11:16am UTC](https://forum.ansible.com/t/how-to-match-exactly-a-tags-name/40182/9 "2025-02-12T11:16:17Z")

</div>

Sorry…but I have some troubles to understand

Forst script with basic.vmware\_vms.yml

```auto
---
plugin: vmware.vmware.vms
hostname: 10.65.223.31
username: administrator@vsphere.local
password: Esxi@123$%
validate_certs: false
gather_tags: true

```

worked and produce a big list.

This is the structure, and I put an example of one of the vms listed:

```auto
{
    "_meta": {
        "hostvars": {
            "namehost1": {
                "ansible_host": "172.17.1.12",
                "config": {
                    "cpuHotAddEnabled": true,
                    "cpuHotRemoveEnabled": false,
                    "guestId": "windows9Server64Guest",
                    "hardware": {
                        "numCPU": 2
                    },
                    "instanceUuid": "501xhshsdh-b1cc-6bf9-5c85-f16ddefd74b",
                    "name": "namehost1",
                    "template": false,
                    "uuid": "421c2f5e-0a4d-4d0c-ab65-403727ad0849"
                },
                "customValue": {
                    "Backup Status": "Backup Job ID [422928] Client: [vcentername.mydomain.loc], Backup Set: [defaultBackupSet], Subclient: [VMMain_MA] Parent >
                    "Last Backup": "11/02/2025 21:48:26"
                },
                "guest": {
                    "guestId": "windows2019srvNext_64Guest",
                    "guestState": "running",
                    "hostName": "namehost1.mydomain.loc",
                    "ipAddress": "172.16.1.152"
                },
                "name": "namehost1",
                "path": "/MycityVX/vm/PABX",
                "runtime": {
                    "maxMemoryUsage": 8192
                },
                "summary": {
                    "runtime": {
                        "powerState": "poweredOn"
                    }
                },
                "tags": {
                    "urn:vmomi:InventoryServiceTag:04ec94c1-ff30-42b8-855d-10ce5a065303:GLOBAL": "othertagpippo",
                    "urn:vmomi:InventoryServiceTag:92b445a9-a413-416d-9cc2-c3b0ad3e1bf8:GLOBAL": "tagtoprocess"
                },
                "tags_by_category": {
                    "Ansible": [
                        {
                            "urn:vmomi:InventoryServiceTag:92b445a9-a413-416d-9cc2-c3b0ad3e1bf8:GLOBAL": "tagtoprocess"
                        }
                    ],
                    "OtherCategory": [
                        {
                            "urn:vmomi:InventoryServiceTag:04ec94c1-ff30-42b8-855d-10ce5a065303:GLOBAL": "othertagpippo"
                        }
                    ]
                }
            },
             "namehost2": {
                       "ansible_host": ...
.
.

```

so, how could I group for tag tagtoprocess, and filter only for the hosts containing that tag as the example you gave me?  
I stucked on the next step you suggested me…

Thanks a lot  
Mario

---

<div class="post-metadata">

**Author:** ![markuman](https://avatars.discourse-cdn.com/v4/letter/m/7bcc69/32.png) [@markuman](https://forum.ansible.com/u/markuman)\
**Post date:** [February 12, 2025, 2:01pm UTC](https://forum.ansible.com/t/how-to-match-exactly-a-tags-name/40182/10 "2025-02-12T14:01:44Z")

</div>

I’m not sure if I understand the returned tag structure correctly. But when the Key of the tag is `Ansible` and its value is `tagtoprocess`, than

```yml
---
plugin: vmware.vmware.vms
hostname: 10.65.223.31
username: administrator@vsphere.local
password: Esxi@123$%
validate_certs: false
gather_tags: true
keyed_groups:
  - key: tags_by_category.Ansible
    separator: ""

```

might work.  
And case-sensitive might also be matter.

---

<div class="post-metadata">

**Author:** ![ansibliano](https://avatars.discourse-cdn.com/v4/letter/a/eb9ed0/32.png) [@ansibliano](https://forum.ansible.com/u/ansibliano)\
**Post date:** [February 12, 2025, 2:41pm UTC](https://forum.ansible.com/t/how-to-match-exactly-a-tags-name/40182/11 "2025-02-12T14:41:03Z")

</div>

Super, it helps a lot, I am at a step further.

I obtain:

```auto
{
    " __urn_vmomi_InventoryServiceTag_3ecce703_3dc7_44b1_91fe_e66593973000_GLOBAL____ tagtoprocess__": {
        "hosts": [
            "namehost1"
        ]
    },
    " __urn_vmomi_InventoryServiceTag_92b445a9_a413_416d_9cc2_c3b0ad3e1bf8_GLOBAL____ anothertagtodontprocess__": {
        "hosts": [
            "namehost2"
        ]
    },
    "_meta": {
        "hostvars": {

```

So, if I want to obtain only the host namehost1 because it has the tag “tagtoprocess” is there a way?  
Because I don’t want namehost2 in the list because it has the tag “anothertagtodontprocess” in the same tags\_by\_category.Ansible

thanks thanks

---

<div class="post-metadata">

**Author:** ![markuman](https://avatars.discourse-cdn.com/v4/letter/m/7bcc69/32.png) [@markuman](https://forum.ansible.com/u/markuman)\
**Post date:** [February 12, 2025, 2:48pm UTC](https://forum.ansible.com/t/how-to-match-exactly-a-tags-name/40182/12 "2025-02-12T14:48:31Z")

</div>

This is totally okey when it lists all available hosts.

give `ansible-inventory` with `--graph` a shot.

It should display something like that

```auto
  |--@tagtoprocess:
  | |--namehost1
  |--@anothertagtodontprocess_:
  | |--namehost2

```

In your playbook you address just the group

```yml
---
- hosts: tagtoprocess

....

```

with `ansible-playbook -i yours..vmware_vms.yml yourplay.ym`

You should also try to add `prefix: ""` to the `keyed_groups:` dict to cleanup the tag value.

```auto
---
plugin: vmware.vmware.vms
hostname: 10.65.223.31
username: administrator@vsphere.local
password: Esxi@123$%
validate_certs: false
gather_tags: true
keyed_groups:
  - key: tags_by_category.tagtoprocess
    separator: ""
    prefix: ""

```

---

<div class="post-metadata">

**Author:** ![ansibliano](https://avatars.discourse-cdn.com/v4/letter/a/eb9ed0/32.png) [@ansibliano](https://forum.ansible.com/u/ansibliano)\
**Post date:** [February 12, 2025, 4:12pm UTC](https://forum.ansible.com/t/how-to-match-exactly-a-tags-name/40182/13 "2025-02-12T16:12:41Z")

</div>

I think you meant tags\_by\_category.Ansible here:

```auto
---
plugin: vmware.vmware.vms
hostname: 10.65.223.31
username: administrator@vsphere.local
password: Esxi@123$%
validate_certs: false
gather_tags: true
keyed_groups:
  - key: tags_by_category.Ansible
    separator: ""
    prefix: ""

```

With the --graph I obtain:

```auto

  | 
  |--@ __urn_vmomi_InventoryServiceTag_3ecce703_3dc7_44b1_91fe_e66593973000_GLOBAL____ tagtoprocess__:
  | |--namehost1
  |--@ __urn_vmomi_InventoryServiceTag_92b445a9_a413_416d_9cc2_c3b0ad3e1bf8_GLOBAL____ anothertagtodontprocess__:
  | |--namehost2

```

so it doesn’t produce a clean inventory only with tagtoprocess namehost1 I suppose

---

<div class="post-metadata">

**Author:** ![markuman](https://avatars.discourse-cdn.com/v4/letter/m/7bcc69/32.png) [@markuman](https://forum.ansible.com/u/markuman)\
**Post date:** [February 13, 2025, 11:55am UTC](https://forum.ansible.com/t/how-to-match-exactly-a-tags-name/40182/14 "2025-02-13T11:55:52Z")

</div>

try to add some filters to `key:`

```auto
---
plugin: vmware.vmware.vms
hostname: 10.65.223.31
username: administrator@vsphere.local
password: Esxi@123$%
validate_certs: false
gather_tags: true
keyed_groups:
  - key: tags_by_category.Ansible | regex_search("___(.*)__") | replace("_","")
    separator: ""
    prefix: ""

```

it might be worth it to open an issue in [GitHub - ansible-collections/vmware.vmware: Ansible VMWare Collection](https://github.com/ansible-collections/vmware.vmware/) and ask how to get clean tags …

---

<div class="post-metadata">

**Author:** ![mikemorency](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/mikemorency/32/2672_2.png) [@mikemorency](https://forum.ansible.com/u/mikemorency)\
**Post date:** [February 19, 2025, 8:05pm UTC](https://forum.ansible.com/t/how-to-match-exactly-a-tags-name/40182/15 "2025-02-19T20:05:49Z")

</div>

For future readers, they did open an issue here [How to create an inventory grouped by tag name? · Issue #117 · ansible-collections/vmware.vmware · GitHub](https://github.com/ansible-collections/vmware.vmware/issues/117)  
And i believe I answered their question but still waiting to hear back

---

<div class="post-metadata">

**Author:** ![jdranchman](https://avatars.discourse-cdn.com/v4/letter/j/779978/32.png) [@jdranchman](https://forum.ansible.com/u/jdranchman)\
**Post date:** [July 3, 2025, 7:06pm UTC](https://forum.ansible.com/t/how-to-match-exactly-a-tags-name/40182/16 "2025-07-03T19:06:41Z")

</div>

If I may follow up on this thread - I’d like to match N tags to make a new group. For example. We have an Applications tag category with many values and and Environment tag category with PR, DV, etc. Is there a reasonable way to combine this kind of search in keyed\_groups? To end up with something like “category”\_“environment”.

I’m using:

```auto
key: tags_by_category.Adpplications | regex_search("___(.*)__") | replace("_","")

```

to get groups of categories (as in the above example) and that works great for each of my 100+ categories. What I would like to see is a group with the category name and only production. I’m sure I’m floundering on some (blah blah & blah blah) intersection test (something like a “when”)

---

<div class="post-metadata">

**Author:** ![mikemorency](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/mikemorency/32/2672_2.png) [@mikemorency](https://forum.ansible.com/u/mikemorency)\
**Post date:** [July 3, 2025, 11:05pm UTC](https://forum.ansible.com/t/how-to-match-exactly-a-tags-name/40182/17 "2025-07-03T23:05:21Z")

</div>

You can do something like this. You cant do a when type statement AFAIK, but you can dump the non-prod hosts into a junk group. For example:

```auto
---
- hosts: localhost
  gather_facts: no
  vars:
    test1:
      applications:
        - a
        - b
        - c
      environment:
        - prod
    test2:
      applications:
        - d
        - e
        - f
      environment:
        - dev

  tasks:
    - set_fact:
        output1: >-
          {{
          test1.applications |
          map('regex_replace', '^(.*)$', '\1_' + (test1.environment | select('equalto', 'prod') | first | default('none')))
          }}
        output2: >-
          {{
          test2.applications |
          map('regex_replace', '^(.*)$', '\1_' + (test2.environment | select('equalto', 'prod') | first | default('none')))
          }}
    - name: Print ouput
      debug:
        var: output1
    - name: Print ouput
      debug:
        var: output2

```

That yields

```auto
Thursday 03 July 2025 18:48:06 -0400 (0:00:00.026) 0:00:00.032 ********* 
ok: => {
    "output1": [
        "a_prod",
        "b_prod",
        "c_prod"
    ]
}

TASK [Print ouput] *************************************************************************************************************************
Thursday 03 July 2025 18:48:06 -0400 (0:00:00.012) 0:00:00.045 ********* 
ok: => {
    "output2": [
        "d_none",
        "e_none",
        "f_none"
    ]
}

```

I dont have a tagged vmware resource to test with at the moment, but hopefully that example is close enough
