# Condition in Templates

**URL:** <https://forum.ansible.com/t/condition-in-templates/19300>\
**Category:** Ansible Project\
**Created:** [September 18, 2015, 1:50pm UTC](https://forum.ansible.com/t/condition-in-templates/19300 "2015-09-18T13:50:26Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jpallentin](https://avatars.discourse-cdn.com/v4/letter/j/e79b87/32.png) [@jpallentin](https://forum.ansible.com/u/jpallentin)\
**Post date:** [September 18, 2015, 1:50pm UTC](https://forum.ansible.com/t/condition-in-templates/19300/1 "2015-09-18T13:50:26Z")

</div>

Hi everyone,

one simple question and sry for my english skills 🙂

I have a playbook with some templates, in one template I added a condition to write a specific IP Address in my template.  
(My servers have several network ports and unfortunately they have sometimes different names. Ok, i could use different templates but i wanted to use a single file)

The code:

{% if ansible\_mgt.ipv4.address is defined %}  
server\_address={{ansible\_mgt.ipv4.address}}  
{% else %}  
server\_address={{ansible\_eth0.ipv4.address}}  
{% endif %}

If I run the playbook against a server which has not the network port ansible\_mgt the playbook fails because the variable ansible\_mgt.ipv4.address is not defined.

What I am doing wrong? Or it is not possible to check in a template if a variable is not defined?

Thanks for your help!

Regards,  
Jost

---

<div class="post-metadata">

**Author:** ![David\_Karban](https://avatars.discourse-cdn.com/v4/letter/d/90db22/32.png) [@David\_Karban](https://forum.ansible.com/u/David_Karban)\
**Post date:** [September 18, 2015, 2:20pm UTC](https://forum.ansible.com/t/condition-in-templates/19300/2 "2015-09-18T14:20:24Z")

</div>

Hi,

I guess, in that case not even ansible\_mgt will be defined? What is exact error message (try ansible-playbook with -vvv) ?

David

---

<div class="post-metadata">

**Author:** ![jpallentin](https://avatars.discourse-cdn.com/v4/letter/j/e79b87/32.png) [@jpallentin](https://forum.ansible.com/u/jpallentin)\
**Post date:** [September 18, 2015, 2:34pm UTC](https://forum.ansible.com/t/condition-in-templates/19300/3 "2015-09-18T14:34:31Z")

</div>

Hi David,

the error message is:

fatal: [testserver.xxx] =\> {‘msg’: “AnsibleUndefinedVariable: One or more undefined variables: ‘ansible\_mgt’ is undefined”, ‘failed’: True}  
fatal: [testserver.xxx] =\> {‘msg’: “AnsibleUndefinedVariable: One or more undefined variables: ‘ansible\_mgt’ is undefined”, ‘failed’: True}  
FATAL: all hosts have already failed – aborting

Well, that is true. On this machine I have only ansible\_eth0:

“ansible\_eth0”: {  
“active”: true,  
“device”: “eth0”,  
“ipv4”: {  
“address”: “10.101.10.133”,  
“netmask”: “255.255.255.0”,  
“network”: “10.101.10.0”  
},

My idea was to use the condition, if ansible\_mgt is defined → use ansible\_mgt and if not → ansible\_eth0

Regards,  
Jost

---

<div class="post-metadata">

**Author:** ![David\_Karban](https://avatars.discourse-cdn.com/v4/letter/d/90db22/32.png) [@David\_Karban](https://forum.ansible.com/u/David_Karban)\
**Post date:** [September 18, 2015, 2:39pm UTC](https://forum.ansible.com/t/condition-in-templates/19300/4 "2015-09-18T14:39:11Z")

</div>

Hi Josh,

your idea is right:

{% if ansible\_mgt is defined and ansible\_mgt.ipv4.address is defined %}  
server\_address={{ansible\_mgt.ipv4.address}}  
{% else %}  
server\_address={{ansible\_eth0.ipv4.address}}  
{% endif %}

you have to check ansible\_mgt first.

---

<div class="post-metadata">

**Author:** ![jpallentin](https://avatars.discourse-cdn.com/v4/letter/j/e79b87/32.png) [@jpallentin](https://forum.ansible.com/u/jpallentin)\
**Post date:** [September 18, 2015, 2:49pm UTC](https://forum.ansible.com/t/condition-in-templates/19300/5 "2015-09-18T14:49:04Z")

</div>

Hi David,

of course! thank you very much!

I didn’t recognized that the error message was only saying “ansible\_mgt” and not “ansible\_mgt.ipv4.address”…facepalm

Regards,  
Jost
