# vmware\_vm\_inventory filters

**URL:** <https://forum.ansible.com/t/vmware-vm-inventory-filters/33744>\
**Category:** Ansible Project\
**Created:** [September 4, 2020, 7:12pm UTC](https://forum.ansible.com/t/vmware-vm-inventory-filters/33744 "2020-09-04T19:12:03Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kiran\_Kumar](https://avatars.discourse-cdn.com/v4/letter/k/f475e1/32.png) [@Kiran\_Kumar](https://forum.ansible.com/u/Kiran_Kumar)\
**Post date:** [September 4, 2020, 7:12pm UTC](https://forum.ansible.com/t/vmware-vm-inventory-filters/33744/1 "2020-09-04T19:12:03Z")

</div>

Hi

Needs any suggestion in my case as below

We have vCenter where many linux bases vm which are use by other lob, there is no proper naming standard , no ip based filter possible , no folders arranged properly

With all this, one thing which is in my control is add “custom attributes”

Is there any way we can filter using "custom attributes’

Thanks

---

<div class="post-metadata">

**Author:** ![Kiran\_Kumar](https://avatars.discourse-cdn.com/v4/letter/k/f475e1/32.png) [@Kiran\_Kumar](https://forum.ansible.com/u/Kiran_Kumar)\
**Post date:** [September 4, 2020, 7:21pm UTC](https://forum.ansible.com/t/vmware-vm-inventory-filters/33744/2 "2020-09-04T19:21:46Z")

</div>

@abijit reported community.vmware.vmware\_vm\_inventory but i did not find doc about it ie what all capabilities its filter has

---

<div class="post-metadata">

**Author:** ![Kiran\_Kumar](https://avatars.discourse-cdn.com/v4/letter/k/f475e1/32.png) [@Kiran\_Kumar](https://forum.ansible.com/u/Kiran_Kumar)\
**Post date:** [September 5, 2020, 3:16am UTC](https://forum.ansible.com/t/vmware-vm-inventory-filters/33744/3 "2020-09-05T03:16:21Z")

</div>

tried but no luck

plugin: community.vmware.vmware\_vm\_inventory  
strict: False  
hostname: xxx  
username: administrator@vsphere.local  
password: xxx  
validate\_certs: False  
#properties:  
#- runtime.powerState  
#- config.name  
properties:

- ‘name’
- ‘config.uuid’
- ‘config.name’
- ‘guest.toolsStatus’
- ‘guest.toolsRunningStatus’
- ‘guest.ipAddress’
- ‘configIssue’
- ‘config.bootOptions’
- ‘config.annotation’
- ‘config.alternateGuestName’  
compose:  
ansible\_host: ‘guest.ipAddress’  
keyed\_groups:
- key: ‘tags’  
separator: ‘’  
filters:
- guest.GuestFullName == “CentOS 7 (64-bit)”

---

<div class="post-metadata">

**Author:** ![Kiran\_Kumar](https://avatars.discourse-cdn.com/v4/letter/k/f475e1/32.png) [@Kiran\_Kumar](https://forum.ansible.com/u/Kiran_Kumar)\
**Post date:** [September 5, 2020, 3:25am UTC](https://forum.ansible.com/t/vmware-vm-inventory-filters/33744/4 "2020-09-05T03:25:47Z")

</div>

i tested & found below works though

filters: - runtime.powerState == “poweredOff”

i am using  
ansible 2.9.10

---

<div class="post-metadata">

**Author:** ![Kiran\_Kumar](https://avatars.discourse-cdn.com/v4/letter/k/f475e1/32.png) [@Kiran\_Kumar](https://forum.ansible.com/u/Kiran_Kumar)\
**Post date:** [September 5, 2020, 3:40am UTC](https://forum.ansible.com/t/vmware-vm-inventory-filters/33744/5 "2020-09-05T03:40:36Z")

</div>

The page

[https://docs.ansible.com/ansible/devel/scenario\_guides/vmware\_scenarios/vmware\_inventory\_hostnames.html](https://docs.ansible.com/ansible/devel/scenario_guides/vmware_scenarios/vmware_inventory_hostnames.html)

Does not talk or give examples of filter …

---

<div class="post-metadata">

**Author:** ![Akasurde](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/akasurde/32/230_2.png) [@Akasurde](https://forum.ansible.com/u/Akasurde)\
**Post date:** [September 8, 2020, 7:16am UTC](https://forum.ansible.com/t/vmware-vm-inventory-filters/33744/6 "2020-09-08T07:16:48Z")

</div>

Hi,  
I raised a PR to address - how to use filters in vmware\_vm\_inventory - [https://github.com/ansible/ansible/pull/71670](https://github.com/ansible/ansible/pull/71670)

Please provide the feedback.

---

<div class="post-metadata">

**Author:** ![Kiran\_Kumar](https://avatars.discourse-cdn.com/v4/letter/k/f475e1/32.png) [@Kiran\_Kumar](https://forum.ansible.com/u/Kiran_Kumar)\
**Post date:** [September 8, 2020, 11:01pm UTC](https://forum.ansible.com/t/vmware-vm-inventory-filters/33744/7 "2020-09-08T23:01:30Z")

</div>

Thanks a lot Abhijit

I did look at [https://github.com/ansible/ansible/pull/71670](https://github.com/ansible/ansible/pull/71670) , but not sure where i can give feedback.

I am sure you got it & see a value & hence raise PR … so for records Again i can give below feedback :-

If user get filter based on "custom attributes " will provide a lot of flexibility to users & with their own queries …

eg my org has 1 vcenter & has many lobs using it, a linux vm can be of of engg, infra, infosec & so on … so i have a custom tag called “lob”…

Thanks

---

<div class="post-metadata">

**Author:** ![Kiran\_Kumar](https://avatars.discourse-cdn.com/v4/letter/k/f475e1/32.png) [@Kiran\_Kumar](https://forum.ansible.com/u/Kiran_Kumar)\
**Post date:** [February 7, 2021, 10:12am UTC](https://forum.ansible.com/t/vmware-vm-inventory-filters/33744/8 "2021-02-07T10:12:59Z")

</div>

I checked that one, seems close, does that mean this feature is now in ? the docs does not talk about that - [https://docs.ansible.com/ansible/devel/scenario\_guides/vmware\_scenarios/vmware\_inventory\_filters.html](https://docs.ansible.com/ansible/devel/scenario_guides/vmware_scenarios/vmware_inventory_filters.html)

Thanks

---

<div class="post-metadata">

**Author:** ![Akasurde](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/akasurde/32/230_2.png) [@Akasurde](https://forum.ansible.com/u/Akasurde)\
**Post date:** [February 8, 2021, 4:17am UTC](https://forum.ansible.com/t/vmware-vm-inventory-filters/33744/9 "2021-02-08T04:17:07Z")

</div>

Hi,

You can use custom attributes assigned to vms using inventory. Let us assume that a VM has attributes as “OS” = “Linux” and “Backup” = “Monday” then  
your inventory configuration looks like -

plugin: community.vmware.vmware\_vm\_inventory  
strict: False  
hostname: 10.8.36.25  
username: administrator@vsphere.local  
password: 2gJ.aGq8AU\*tPAaii\_  
validate\_certs: False  
hostnames:

- ‘[config.name](http://config.name)’  
properties:
- ‘[config.name](http://config.name)’
- ‘config.guestId’
- ‘guest.ipAddress’
- ‘summary.runtime.powerState’
- ‘customValue’ # \<---------------------------------- This way you can specify the customValues  
filters:
- OS is defined and OS == ‘Linux’ # \<---------------------------------- This way you can filter customValue

You will get results like

# ansible-inventory -i filters\_custom\_attributes\_vmware.yml --list

{  
“\_meta”: {  
“hostvars”: {  
“centos\_7”: {  
“Backup”: “Monday”,  
“OS”: “Linux”,  
“config.guestId”: “centos7\_64Guest”,  
“[config.name](http://config.name)”: “centos\_7”,  
“runtime.connectionState”: “connected”,  
“summary.runtime.powerState”: “poweredOff”  
}  
}  
},  
“all”: {  
“children”: [  
“centos7\_64Guest”,  
“poweredOff”,  
“ungrouped”  
]  
},  
“centos7\_64Guest”: {  
“hosts”: [  
“centos\_7”  
]  
},  
“poweredOff”: {  
“hosts”: [  
“centos\_7”  
]  
}  
}

Let me know if you need any additional information.

---

<div class="post-metadata">

**Author:** ![Kiran\_Kumar](https://avatars.discourse-cdn.com/v4/letter/k/f475e1/32.png) [@Kiran\_Kumar](https://forum.ansible.com/u/Kiran_Kumar)\
**Post date:** [February 9, 2021, 12:33am UTC](https://forum.ansible.com/t/vmware-vm-inventory-filters/33744/10 "2021-02-09T00:33:47Z")

</div>

Abhijit actually i came across that one & tried below … “per the documents” … which were confusing BTW … soon i will give link

filters:

- “tag\_category.OS is defined and ‘Ubuntu16.04’ in tag\_category.OS”

I had difficult time to get this tested … as for “tags” you see some extra stuff install & that install though looks straighforward but took me for spin … so i am more into fixing the python issues … May be you can help… soon i will give all i tried & error i got …

---

<div class="post-metadata">

**Author:** ![Kiran\_Kumar](https://avatars.discourse-cdn.com/v4/letter/k/f475e1/32.png) [@Kiran\_Kumar](https://forum.ansible.com/u/Kiran_Kumar)\
**Post date:** [February 9, 2021, 12:36am UTC](https://forum.ansible.com/t/vmware-vm-inventory-filters/33744/11 "2021-02-09T00:36:05Z")

</div>

Link :

[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)

last 2 lines

filters: - “tag\_category.OS is defined and ‘Linux’ in tag\_category.OS”

How to i add “AND” or “OR” here ?

OS = CentOS7

And

LOB = Infra

---

<div class="post-metadata">

**Author:** ![Kiran\_Kumar](https://avatars.discourse-cdn.com/v4/letter/k/f475e1/32.png) [@Kiran\_Kumar](https://forum.ansible.com/u/Kiran_Kumar)\
**Post date:** [February 9, 2021, 12:46am UTC](https://forum.ansible.com/t/vmware-vm-inventory-filters/33744/12 "2021-02-09T00:46:24Z")

</div>

Now the error :

ansible-inventory --list -i vmware.yml  
[WARNING]: \* Failed to parse /etc/ansible/playbooks/vmware.yml with ansible\_collections.community.vmware.plugins.inventory.vmware\_vm\_inventory plugin: Unable to find ‘vSphere  
Automation SDK’ Python library which is required. Please refer this URL for installation steps - [https://code.vmware.com/web/sdk/7.0/vsphere-automation-python](https://code.vmware.com/web/sdk/7.0/vsphere-automation-python)  
[WARNING]: Unable to parse /etc/ansible/playbooks/vmware.yml as an inventory source  
[WARNING]: No inventory was parsed, only implicit localhost is available  
{  
“\_meta”: {  
“hostvars”: {}  
},  
“all”: {  
“children”: [  
“ungrouped”  
]  
}  
}

---

<div class="post-metadata">

**Author:** ![Kiran\_Kumar](https://avatars.discourse-cdn.com/v4/letter/k/f475e1/32.png) [@Kiran\_Kumar](https://forum.ansible.com/u/Kiran_Kumar)\
**Post date:** [February 10, 2021, 3:01am UTC](https://forum.ansible.com/t/vmware-vm-inventory-filters/33744/13 "2021-02-10T03:01:46Z")

</div>

tested on Ubuntu 20.04 …

I am able to see all vms

eg

“xx”: {  
“Application”: “xx”,  
“Environment”: “xx”,  
“LOB”: “xx”,  
“Location”: “xx”,  
“NB\_LAST\_BACKUP”: “xx”,  
“OS”: “Ubuntu18.04”,  
“ansible\_host”: “xx”,  
“categories”: [  
“Backup Retention Policy”,  
“OS”  
],  
“config.guestId”: “ubuntu64Guest”,  
“config.name”: “xx”,  
“guest.ipAddress”: “xx”,  
“runtime.connectionState”: “connected”,  
“summary.runtime.powerState”: “poweredOn”,  
“tag\_category.Backup Retention Policy”: [  
“Backup-Bronze”  
],  
“tag\_category.OS”: [  
“Ubuntu18.04”  
],  
“tags”: [  
“Backup-Bronze”,  
“Ubuntu18.04”  
]  
}  
}  
},

As soon as i added

filters:

- OS is defined and OS == ‘Ubuntu18.04’

I get below

{  
“\_meta”: {  
“hostvars”: {}  
},  
“all”: {  
“children”: [  
“ungrouped”  
]  
}  
}

Any suggestions Please ?

---

<div class="post-metadata">

**Author:** ![Kiran\_Kumar](https://avatars.discourse-cdn.com/v4/letter/k/f475e1/32.png) [@Kiran\_Kumar](https://forum.ansible.com/u/Kiran_Kumar)\
**Post date:** [February 10, 2021, 3:06am UTC](https://forum.ansible.com/t/vmware-vm-inventory-filters/33744/14 "2021-02-10T03:06:40Z")

</div>

Please ignore…i removed & re-added the tag & its working … Thanks @ Abhijeet Kasurde

---

<div class="post-metadata">

**Author:** ![Kiran\_Kumar](https://avatars.discourse-cdn.com/v4/letter/k/f475e1/32.png) [@Kiran\_Kumar](https://forum.ansible.com/u/Kiran_Kumar)\
**Post date:** [February 10, 2021, 3:10am UTC](https://forum.ansible.com/t/vmware-vm-inventory-filters/33744/15 "2021-02-10T03:10:44Z")

</div>

Great …it filter not only based on Tags but custom attributes as well… !!!

---

<div class="post-metadata">

**Author:** ![Kiran\_Kumar](https://avatars.discourse-cdn.com/v4/letter/k/f475e1/32.png) [@Kiran\_Kumar](https://forum.ansible.com/u/Kiran_Kumar)\
**Post date:** [February 10, 2021, 11:42pm UTC](https://forum.ansible.com/t/vmware-vm-inventory-filters/33744/16 "2021-02-10T23:42:24Z")

</div>

BTW how to use this as dynamic inv ?

Tried below  
ansible-playbook uptime-inv-based.yml -i vmware.yml

---

<div class="post-metadata">

**Author:** ![Kiran\_Kumar](https://avatars.discourse-cdn.com/v4/letter/k/f475e1/32.png) [@Kiran\_Kumar](https://forum.ansible.com/u/Kiran_Kumar)\
**Post date:** [February 11, 2021, 1:32am UTC](https://forum.ansible.com/t/vmware-vm-inventory-filters/33744/17 "2021-02-11T01:32:33Z")

</div>

fixed

i changed

hosts: localhost

to

hosts: all

---

<div class="post-metadata">

**Author:** ![Kiran\_Kumar](https://avatars.discourse-cdn.com/v4/letter/k/f475e1/32.png) [@Kiran\_Kumar](https://forum.ansible.com/u/Kiran_Kumar)\
**Post date:** [February 11, 2021, 3:10am UTC](https://forum.ansible.com/t/vmware-vm-inventory-filters/33744/18 "2021-02-11T03:10:30Z")

</div>

Is it possible to connect multiple vCenters at same time ?

---

<div class="post-metadata">

**Author:** ![Akasurde](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/akasurde/32/230_2.png) [@Akasurde](https://forum.ansible.com/u/Akasurde)\
**Post date:** [February 11, 2021, 4:11am UTC](https://forum.ansible.com/t/vmware-vm-inventory-filters/33744/19 "2021-02-11T04:11:31Z")

</div>

No, for different vCenter you need to have multiple configuration files.

---

<div class="post-metadata">

**Author:** ![Kiran\_Kumar](https://avatars.discourse-cdn.com/v4/letter/k/f475e1/32.png) [@Kiran\_Kumar](https://forum.ansible.com/u/Kiran_Kumar)\
**Post date:** [February 11, 2021, 4:58am UTC](https://forum.ansible.com/t/vmware-vm-inventory-filters/33744/20 "2021-02-11T04:58:06Z")

</div>

That will be a good & handy feature request… as you can imagin for so many use cases & for ease … BTW with powercli it is possible to do this… so is technically possible as well

Please suggest what you think Abhijeet
