# Can we filter inventories using Jinja filters?

**URL:** <https://forum.ansible.com/t/can-we-filter-inventories-using-jinja-filters/25892>\
**Category:** AWX Project\
**Created:** [November 1, 2017, 9:13pm UTC](https://forum.ansible.com/t/can-we-filter-inventories-using-jinja-filters/25892 "2017-11-01T21:13:21Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![David\_Reagan](https://avatars.discourse-cdn.com/v4/letter/d/71e660/32.png) [@David\_Reagan](https://forum.ansible.com/u/David_Reagan)\
**Post date:** [November 1, 2017, 9:13pm UTC](https://forum.ansible.com/t/can-we-filter-inventories-using-jinja-filters/25892/1 "2017-11-01T21:13:21Z")

</div>

Is it possible to filter inventories and sources using Jinja filters? As in, filters listed here: [http://docs.ansible.com/ansible/latest/playbooks\_filters.html](http://docs.ansible.com/ansible/latest/playbooks_filters.html)

When I try to use the regex\_search filter, I get this error:

> jinja2.exceptions.TemplateAssertionError: no filter named ‘regex\_search’

Specifically, I have tried to set the INSTANCE FILTERS field on my source to _{{ config.name | regex\_search(‘(.-test-.’) }}_.

I am looking for a more flexible way to filter hosts than setting INSTANCE FILTERS to something like _{{ “foo” in config.name }}_ can provide.

---

<div class="post-metadata">

**Author:** ![matburt](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/matburt/32/3612_2.png) [@matburt](https://forum.ansible.com/u/matburt)\
**Post date:** [November 2, 2017, 3:09am UTC](https://forum.ansible.com/t/can-we-filter-inventories-using-jinja-filters/25892/2 "2017-11-02T03:09:46Z")

</div>

That’s probably not going to work, instance filters are specific to whatever inventory source you are using. Currently they are implemented on ec2 and vmware inventory sources and you can see more information here:

[http://docs.ansible.com/ansible-tower/latest/html/userguide/inventories.html#amazon-web-services-ec2](http://docs.ansible.com/ansible-tower/latest/html/userguide/inventories.html#amazon-web-services-ec2)

[http://docs.ansible.com/ansible-tower/latest/html/userguide/inventories.html#vmware-vcenter](http://docs.ansible.com/ansible-tower/latest/html/userguide/inventories.html#vmware-vcenter)

---

<div class="post-metadata">

**Author:** ![David\_Reagan](https://avatars.discourse-cdn.com/v4/letter/d/71e660/32.png) [@David\_Reagan](https://forum.ansible.com/u/David_Reagan)\
**Post date:** [November 2, 2017, 8:55pm UTC](https://forum.ansible.com/t/can-we-filter-inventories-using-jinja-filters/25892/3 "2017-11-02T20:55:22Z")

</div>

That doesn’t really answer my question.

The INSTANCE FILTERS field appears to use the same {{}} syntax as a normal Ansible Template. Thus, I would think you’d be able to do anything in that field you can do in a template. At least as far as configuring what the stuff between the brackets turns into.

That’s why I’m wondering if I should be able to use the regex\_search jinja filter. If I am, then the fact I got that error means there is a problem. If I’m not, then I just can’t use the filters and have to figure out another way of doing what I want.

Thanks!

---

<div class="post-metadata">

**Author:** ![Bill\_Nottingham1](https://avatars.discourse-cdn.com/v4/letter/b/58f4c7/32.png) [@Bill\_Nottingham1](https://forum.ansible.com/u/Bill_Nottingham1)\
**Post date:** [November 3, 2017, 2:46pm UTC](https://forum.ansible.com/t/can-we-filter-inventories-using-jinja-filters/25892/4 "2017-11-03T14:46:30Z")

</div>

David Reagan (jerrac@gmail.com) said:

> That doesn't really answer my question.
> 
> The INSTANCE FILTERS field appears to use the same {{}} syntax as a normal  
> Ansible Template. Thus, I would think you'd be able to do anything in that  
> field you can do in a template. At least as far as configuring what the  
> stuff between the brackets turns into.

As Matt said, only for VMware... ec2 uses that field differently, and other  
scripts not at all.

> That's why I'm wondering if I should be able to use the regex\_search jinja  
> filter. If I am, then the fact I got that error means there is a problem.  
> If I'm not, then I just can't use the filters and have to figure out  
> another way of doing what I want.

As long as your're just worried about VMware...

It does not use Ansible's custom filters.  
See [https://github.com/ansible/ansible/blob/devel/contrib/inventory/vmware\_inventory.py#L136-L139](https://github.com/ansible/ansible/blob/devel/contrib/inventory/vmware_inventory.py#L136-L139)  
for the regex\_match filter you can use.

Bill

---

<div class="post-metadata">

**Author:** ![David\_Reagan](https://avatars.discourse-cdn.com/v4/letter/d/71e660/32.png) [@David\_Reagan](https://forum.ansible.com/u/David_Reagan)\
**Post date:** [November 3, 2017, 6:35pm UTC](https://forum.ansible.com/t/can-we-filter-inventories-using-jinja-filters/25892/5 "2017-11-03T18:35:26Z")

</div>

Oh… Now I see how Matt answered my question…

So the answer to my question is that you can’t use just any jinja filter. You can only use specific filters that each kind of source supports.

Thanks!
