Can we filter inventories using Jinja filters?

1,119 views
Skip to first unread message

David Reagan

unread,
Nov 1, 2017, 5:13:22 PM11/1/17
to AWX Project

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

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.

Matthew Jones

unread,
Nov 1, 2017, 11:09:47 PM11/1/17
to David Reagan, AWX Project
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:



--
You received this message because you are subscribed to the Google Groups "AWX Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to awx-project+unsubscribe@googlegroups.com.
To post to this group, send email to awx-p...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/awx-project/697cd8e1-9b66-465b-84de-95f6674e68a1%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Matt Jones
Principal Software Engineer
Ansible Tower

David Reagan

unread,
Nov 2, 2017, 4:55:22 PM11/2/17
to AWX Project
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!


On Wednesday, November 1, 2017 at 8:09:47 PM UTC-7, Matthew Jones wrote:
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:


On Wed, Nov 1, 2017 at 5:13 PM, David Reagan <jer...@gmail.com> wrote:

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

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.

--
You received this message because you are subscribed to the Google Groups "AWX Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to awx-project...@googlegroups.com.

To post to this group, send email to awx-p...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/awx-project/697cd8e1-9b66-465b-84de-95f6674e68a1%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Bill Nottingham

unread,
Nov 3, 2017, 10:46:42 AM11/3/17
to David Reagan, AWX Project
David Reagan (jer...@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
for the regex_match filter you can use.

Bill

David Reagan

unread,
Nov 3, 2017, 2:35:26 PM11/3/17
to AWX Project
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!
Reply all
Reply to author
Forward
0 new messages