# Excluding String in Jinja2 Template

**URL:** <https://forum.ansible.com/t/excluding-string-in-jinja2-template/25690>\
**Category:** Ansible Project\
**Created:** [October 5, 2017, 4:38pm UTC](https://forum.ansible.com/t/excluding-string-in-jinja2-template/25690 "2017-10-05T16:38:48Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![doug](https://avatars.discourse-cdn.com/v4/letter/d/dbc845/32.png) [@doug](https://forum.ansible.com/u/doug)\
**Post date:** [October 5, 2017, 4:38pm UTC](https://forum.ansible.com/t/excluding-string-in-jinja2-template/25690/1 "2017-10-05T16:38:48Z")

</div>

Hi

Over IRC Brian was very helpful last night resolving an issue.

To summarize this now works:

{% if inventory\_hostname in groups.lab %}

# Lab SSH Rules

{% for host in groups.lab %}  
-A INPUT -s {{ lookup(‘dig’, host, ‘@dns\_server’)}} -p tcp -m state --state NEW -m tcp --dport 22 -m comment --comment “ssh from {{ host }}” -j ACCEPT  
{% endfor %}  
{% endif %}

Though I want to exclude some servers in groups.lab from being included in template. Luckily those I want to exclude contain a particular string which matches subdomain.

So could I use a regex search filter to exclude any server that contains such string?

Eg:

{% if inventory\_hostname in groups.lab | regex\_search(‘(!string)’) %}

Thanks!  
Doug

---

<div class="post-metadata">

**Author:** ![doug](https://avatars.discourse-cdn.com/v4/letter/d/dbc845/32.png) [@doug](https://forum.ansible.com/u/doug)\
**Post date:** [October 5, 2017, 5:29pm UTC](https://forum.ansible.com/t/excluding-string-in-jinja2-template/25690/2 "2017-10-05T17:29:34Z")

</div>

Thanks to halberom on IRC!

This works:

{% for host in groups.lab if ‘string’ not in host %}

I am using something similar to deploy sssd.conf on our compute nodes:

[domain/LDAP]  
autofs\_provider = ldap  
{%if ‘dude’ in inventory\_hostname %}  
ldap\_autofs\_search\_base = ou=autofs.maps,cn=dudecluster,ou=blah,dc=domain  
{%endif%}  
{%if ‘bro’ in inventory\_hostname %}  
ldap\_autofs\_search\_base = ou=autofs.maps,cn=brocluster,ou=blah,dc=domain  
{%endif%}

Though I didn’t know you could add an if within for loop without doing:

{% if %}  
{% for %}  
{% endfor %}  
{% endif %}

Thanks a lot!

Doug
