Using ansible facts in playbooks

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

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

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.