# Using ansible facts in playbooks

**URL:** <https://forum.ansible.com/t/using-ansible-facts-in-playbooks/16873>\
**Category:** Ansible Project\
**Created:** [November 25, 2014, 10:38am UTC](https://forum.ansible.com/t/using-ansible-facts-in-playbooks/16873 "2014-11-25T10:38:36Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Linto\_Paulson](https://avatars.discourse-cdn.com/v4/letter/l/2bfe46/32.png) [@Linto\_Paulson](https://forum.ansible.com/u/Linto_Paulson)\
**Post date:** [November 25, 2014, 10:38am UTC](https://forum.ansible.com/t/using-ansible-facts-in-playbooks/16873/1 "2014-11-25T10:38:36Z")

</div>

I wanted to accomplish something like this. I wanted to use the ansible facts like ansible\_eth0.ipv4.address to check if this fact is defined.

- name: Check if eth0 interface setup  
fail: msg=“eth0 interface not setup”  
when: ansible\_eth0.ipv4.address is not defined

But ansible is throwing error at me

I am using gather\_facts: yes

fatal: [xxxxxxxxx] =\> error while evaluating conditional: ansible\_eth1.ipv4.address is not defined

I am using 1.5.4

---

<div class="post-metadata">

**Author:** ![Michael\_Peters](https://avatars.discourse-cdn.com/v4/letter/m/c2a13f/32.png) [@Michael\_Peters](https://forum.ansible.com/u/Michael_Peters)\
**Post date:** [November 25, 2014, 6:59pm UTC](https://forum.ansible.com/t/using-ansible-facts-in-playbooks/16873/2 "2014-11-25T18:59:40Z")

</div>

First, that's a really old version of ansible, and you need to upgrade.

Second, the "when" clause uses jinja syntax and it's complaining that  
it can't evaluate your syntax: it's a syntax error, not a fact error.  
In jinja, the "not" needs to come before the expression it's negating.  
Try something like this:

when: not ansible\_eth1.ipv4.address is defined

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [November 25, 2014, 10:21pm UTC](https://forum.ansible.com/t/using-ansible-facts-in-playbooks/16873/3 "2014-11-25T22:21:32Z")

</div>

this is not a single variable, but 3, this for example checks the  
whole tree of variables down:

when: ansible\_eth0 is defined and 'ipv4' in ansible\_eth0 and 'address'  
in ansible\_eth0.ipv4

as you see this can fail at 3 diff levels.
