Using ansible facts in playbooks

110 views
Skip to first unread message

Linto Paulson

unread,
Nov 25, 2014, 5:38:36 AM11/25/14
to ansible...@googlegroups.com
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



Michael Peters

unread,
Nov 25, 2014, 1:59:44 PM11/25/14
to ansible...@googlegroups.com
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
> --
> You received this message because you are subscribed to the Google Groups
> "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ansible-proje...@googlegroups.com.
> To post to this group, send email to ansible...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/2eed6eeb-07bc-46b1-89e6-f4294c04d209%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Brian Coca

unread,
Nov 25, 2014, 5:21:35 PM11/25/14
to ansible...@googlegroups.com
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.


--
Brian Coca
Reply all
Reply to author
Forward
0 new messages