# Ansible Jinja2 template - inventory\_hostname as variable in if statement?

**URL:** <https://forum.ansible.com/t/ansible-jinja2-template-inventory-hostname-as-variable-in-if-statement/28109>\
**Category:** Ansible Project\
**Created:** [July 20, 2018, 7:37pm UTC](https://forum.ansible.com/t/ansible-jinja2-template-inventory-hostname-as-variable-in-if-statement/28109 "2018-07-20T19:37:21Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![chrisjx](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/chrisjx/32/6922_2.png) [@chrisjx](https://forum.ansible.com/u/chrisjx)\
**Post date:** [July 20, 2018, 7:37pm UTC](https://forum.ansible.com/t/ansible-jinja2-template-inventory-hostname-as-variable-in-if-statement/28109/1 "2018-07-20T19:37:21Z")

</div>

Still trying to understand the way to work jinja2 templates…

I’m looking for a way to alter the definition of a gateway address based on the name of the current inventory\_hostname.

eth0gateway and eth0gateway\_gen are variables in the playbook.

I would like to branch depending on if ecd0 is in, or starts with, the inventory\_hostname.

Can someone lead me in the right direction here:

`  
{% if {{inventory\_hostname}}.find(‘ecd0’) \> -1 %}  
GATEWAY={{eth0gateway}}  
{% else %}  
GATEWAY={{eth0gateway\_gen}}  
{% endif %}

`

Thanks for any tips,  
Chris.

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [July 20, 2018, 8:11pm UTC](https://forum.ansible.com/t/ansible-jinja2-template-inventory-hostname-as-variable-in-if-statement/28109/2 "2018-07-20T20:11:19Z")

</div>

In between Jinja statement delimiters {% and %} you can't use {{ }},  
you are in Jinja and it knows about variables, so change the line to.

&nbsp;&nbsp;&nbsp;{% if inventory\_hostname.find('ecd0') \> -1 %}
