how to filter an item list - with_items filter using RegEx ?

1,403 views
Skip to first unread message

Torsten Reinhard

unread,
Nov 13, 2014, 6:12:44 AM11/13/14
to ansible...@googlegroups.com
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
[cae-2]
pbdelsweb002
.mydomain.com


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

cae-1.yml:
---
cae_instance
:
-
    index
: _1_a
    port
: 8700
-
    index
: _1_b
    port
: 9700
-
    index
: _1_c
    port
: 10700
-
    index
: _1_k
    port
: 12700

All these instances are setup by a simple playbook:

- hosts: cae
  user
: myuser

  vars_files
:
   
- vars/stage-{{stage}}.yml  

  roles
:
   
- { role: cae-tomcat, params: "{{ cae_instance }}" }

This is all fine - but now I have to fullfill the requirement to be able to address only the "_k" instances - or only the "_a" instances - that meens one or more instances out of the complete list.

Is there an option to "filter" the list of instances ? Something like

  with_items
   - filter:
(".*_k" )

Do I have to write my own Iterator(s) for this ?

Thanx for any hints, Torsten




Michael DeHaan

unread,
Nov 17, 2014, 4:13:54 PM11/17/14
to ansible...@googlegroups.com
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)





--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/fbbdc812-3908-43a8-921b-f7705a6314cc%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Torsten Reinhard

unread,
Nov 18, 2014, 3:23:11 AM11/18/14
to ansible...@googlegroups.com
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:

---
cae_instance
:
-
    index
: _1_a
    port
: 8700
...

-
    index
: _1_k
    port
: 12700



The question is now: how to filter that list ?

I know I can iterate the list using the "with_items" expression - but I want to "skip" or "filter" some items, lets say by using RegEx on one (or more) attributes of the list items.

Any solution for that ?

Regards, Torsten

Ted Eiles

unread,
Nov 21, 2014, 12:23:20 PM11/21/14
to ansible...@googlegroups.com
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

Michael DeHaan

unread,
Nov 24, 2014, 5:50:29 PM11/24/14
to ansible...@googlegroups.com
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.



Reply all
Reply to author
Forward
0 new messages