Issue with default value

127 views
Skip to first unread message

Viktor Petersson

unread,
Nov 1, 2014, 3:30:41 PM11/1/14
to ansible...@googlegroups.com
Hey guys,


I'm trying to write an ansible playbook with the shell module. In the command, I'm want to use the eth1 interface if it exists. If the eth1 interface doesn't exist, it should fallback to the loopback interface. 

My initial approach was to use the defaulting undefined variables as follows:

shell: docker run -d -p {{ ansible_eth1.ipv4.address | default(127.0.0.1) }}:27017:27017 [...] dockerfile/mongodb

Unfortunately, Ansible threw the following error:
fatal: [server] => One or more undefined variables: 'ansible_eth1' is undefined

I also tried versions of this, such as:

shell: docker run -d -p {{ ansible_eth1.ipv4.address | ansible_lo.ipv4.address }}:27017:27017 [...] dockerfile/mongodb

That gave me another error:

failed: [server] => {"changed": true, "cmd": "docker run -d -p {# ansible_eth1.ipv4.address | ansible_lo.ipv4.address #}:27017:27017 [...]", "delta": "0:00:00.021053", "end": "2014-11-01     17:22:04.457375", "rc": 127, "start": "2014-11-01 17:22:04.436322"}
stderr: /bin/sh: 1: ansible_lo.ipv4.address: not found
2014/11/01 17:22:04 Invalid containerPort: {{

Could anyone help point me in the right direction?

Matt Martz

unread,
Nov 2, 2014, 8:37:56 PM11/2/14
to ansible...@googlegroups.com
Jinja2 is a little more picky about using dot notation as opposed to [] type notation as it pertains to using "default"

You might want to try:

ansible_eth1["ipv4"]["address"] | default("127.0.0.1")

You may also need to place the default closer to ansible_eth1 as opposed to deeper nesting like it is currently.

--
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/ad7a8411-abbe-46b9-bff7-a9a122eb115d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--
Matt Martz
@sivel
sivel.net

Paul Tötterman

unread,
Sep 1, 2015, 7:55:02 AM9/1/15
to Ansible Project
Sorry to resurrect this old thread.
 
ansible_eth1["ipv4"]["address"] | default("127.0.0.1")

I tried this and it didn't work. Is it supposed to? Is there some other way of using default that would work? Would it make sense to fix the default filter to work in this case?

Cheers,
Paul

Javier Palacios

unread,
Sep 1, 2015, 8:12:39 AM9/1/15
to ansible...@googlegroups.com
Try with
 default("127.0.0.1", true)
It is not clear on the filter description, but without the second argument it does not perform a boolean evaluation, but just strict variable undefinition. That means that an empty string will not trigger the default. Actually, even if the variable you are testing is None, the default will not be used.

Javier Palacios

--
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.

Paul Tötterman

unread,
Sep 1, 2015, 8:23:14 AM9/1/15
to Ansible Project
 default("127.0.0.1", true)

Didn't help.
 
It is not clear on the filter description, but without the second argument it does not perform a boolean evaluation, but just strict variable undefinition. That means that an empty string will not trigger the default. Actually, even if the variable you are testing is None, the default will not be used.

I'm sorry, I didn't specify that I'm getting the same "One or more undefined variables" as Viktor was. The undefined variable can be in my case be on any level. So for ansible_eth1.ipv4.address any of ansible_eth1, ipv4 or address could be undefined.

Cheers,
Paul

Brian Coca

unread,
Sep 1, 2015, 8:48:34 AM9/1/15
to Ansible Project
default does not do multilevel variables, you need to have something
on each level for it to work, something like:

((ansible_eth1|default({})["ipv4"]|default({}))["address"] |
default("127.0.0.1")



--
Brian Coca

Paul Tötterman

unread,
Sep 1, 2015, 8:52:56 AM9/1/15
to Ansible Project
((ansible_eth1|default({})["ipv4"]|default({}))["address"] |
default("127.0.0.1")

Ugh. I was expecting something like try: ... except KeyError: ...

Is there any chance of fixing default to work with multiple levels without so much typing? I think it would be a rather big usability improvement.

Cheers,
Paul 

Brian Coca

unread,
Sep 2, 2015, 8:41:54 AM9/2/15
to Ansible Project
default is a fitler from the jinaj2 project, the 'fix' would have to
happen there
> --
> 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/51c31f67-be57-4b97-845c-256465f9f573%40googlegroups.com.
>
> For more options, visit https://groups.google.com/d/optout.



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