# how to filter an item list - with\_items filter using  RegEx ?

**URL:** <https://forum.ansible.com/t/how-to-filter-an-item-list-with-items-filter-using-regex/16771>\
**Category:** Ansible Project\
**Created:** [November 13, 2014, 11:12am UTC](https://forum.ansible.com/t/how-to-filter-an-item-list-with-items-filter-using-regex/16771 "2014-11-13T11:12:44Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Torsten\_Reinhard](https://avatars.discourse-cdn.com/v4/letter/t/46a35a/32.png) [@Torsten\_Reinhard](https://forum.ansible.com/u/Torsten_Reinhard)\
**Post date:** [November 13, 2014, 11:12am UTC](https://forum.ansible.com/t/how-to-filter-an-item-list-with-items-filter-using-regex/16771/1 "2014-11-13T11:12:44Z")

</div>

`  
Hi all,

like probably most of you, we have multiple servers with multiple application instances (Tomcat, Apache, …) on it, for example structured like this:

`  
[cae:children]  
cae-1  
cae-2

[cae-1]  
[pbdelsweb001.mydomain.com](http://pbdelsweb001.mydomain.com)  
[cae-2]  
[pbdelsweb002.mydomain.com](http://pbdelsweb002.mydomain.com)

`

For each group we have a \*.yml file at group\_vars directoy in the inventory:

cae-1.yml:

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [November 17, 2014, 9:13pm UTC](https://forum.ansible.com/t/how-to-filter-an-item-list-with-items-filter-using-regex/16771/2 "2014-11-17T21:13:50Z")

</div>

If you are attempting to talk to only certain instances, usage of the host group specifier “hosts:” is the way to do it.

To talk to all instances ending in that pattern

- hosts: \*\_k

The docs also show how to combine patterns and negate them.

(The group\_by module can also be used to create groups of arbitrary criteria based on facts, but is not needed here)

---

<div class="post-metadata">

**Author:** ![Torsten\_Reinhard](https://avatars.discourse-cdn.com/v4/letter/t/46a35a/32.png) [@Torsten\_Reinhard](https://forum.ansible.com/u/Torsten_Reinhard)\
**Post date:** [November 18, 2014, 8:23am UTC](https://forum.ansible.com/t/how-to-filter-an-item-list-with-items-filter-using-regex/16771/3 "2014-11-18T08:23:10Z")

</div>

Hi,

it´s not about “hosts” filtering - in my case it´s always “hosts: cae”.

I have some files in

`  
group\_vars/cae-1.yml  
group\_vars/cae-2.yml  
…  
group\_vars/cae-xx.yml

`

according to the group(s) defined in my /inventory/hosts file.

Inside these group\_vars there´s a list of instances, running on that machine:

`

---

<div class="post-metadata">

**Author:** ![Ted\_Eiles](https://avatars.discourse-cdn.com/v4/letter/t/ecccb3/32.png) [@Ted\_Eiles](https://forum.ansible.com/u/Ted_Eiles)\
**Post date:** [November 21, 2014, 5:23pm UTC](https://forum.ansible.com/t/how-to-filter-an-item-list-with-items-filter-using-regex/16771/4 "2014-11-21T17:23:20Z")

</div>

We are attempting to filter a set of AWS subnets for creating an ASG.

In a VPC, we have some subnets tagged as “Network=private”. When creating an ASG for worker instances, we need to set the ASG’s AZs and subnets to the AZs and subnets of all private subnets in the VPC. So, being able to with\_items over the subnets and filtering on tags “Network=private” is useful.

For AWS work, filtering AWS entities (hosts, IAM roles, subnets, VPC’, etc…) on one or more tags seems like it would be common.

— Ted

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [November 24, 2014, 10:50pm UTC](https://forum.ansible.com/t/how-to-filter-an-item-list-with-items-filter-using-regex/16771/5 "2014-11-24T22:50:26Z")

</div>

Sounds like you may wish to write a filter plugin.

If so, see if you need more information, if needed, we can provide some pointers.
