1
votes

I would like to filter on value that contain or start with 'foo_' and ignore other values in list.

values (Can not be modified):

TEST:
- foo_a
- foo_b
- bar_a
- bar_b
- test_c

Template :

{% for value in grains['TEST'] %}
- do something with value that contains "foo_"
{% endfor %}

How can i do it?

I tried with :

{% for value in grains['TEST']|map(^foo_) %}
- do something with value that contains "foo_"
{% endfor %}

=> No success

2
{% for value in grains['TEST'] if value.startswith('foo_') %} -do something {% endfor %} - Avinash Raj

2 Answers

3
votes

Assuming each value is a string (which seems to be the case), you should be able to use the standard startswith method for strings, like {% if value.startswith('foo_') %}

0
votes

If you want to do a substring search, you can check a variable for a string match like so. Make sure to make the variable into a string.

{% if '.mov' in var|string %}
   do something...
{% endif %}