# Issue with default value

**URL:** <https://forum.ansible.com/t/issue-with-default-value/16661>\
**Category:** Ansible Project\
**Created:** [November 1, 2014, 7:30pm UTC](https://forum.ansible.com/t/issue-with-default-value/16661 "2014-11-01T19:30:41Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Viktor\_Petersson](https://avatars.discourse-cdn.com/v4/letter/v/9fc348/32.png) [@Viktor\_Petersson](https://forum.ansible.com/u/Viktor_Petersson)\
**Post date:** [November 1, 2014, 7:30pm UTC](https://forum.ansible.com/t/issue-with-default-value/16661/1 "2014-11-01T19:30:41Z")

</div>

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?

I’ve also published this on Serverfault [1]:

[http://serverfault.com/questions/641326/issues-with-ansibles-default-error-handling-in-a-playbook](http://serverfault.com/questions/641326/issues-with-ansibles-default-error-handling-in-a-playbook)

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [November 3, 2014, 1:36am UTC](https://forum.ansible.com/t/issue-with-default-value/16661/2 "2014-11-03T01:36:01Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Paul\_Totterman](https://avatars.discourse-cdn.com/v4/letter/p/3ec8ea/32.png) [@Paul\_Totterman](https://forum.ansible.com/u/Paul_Totterman)\
**Post date:** [September 1, 2015, 11:55am UTC](https://forum.ansible.com/t/issue-with-default-value/16661/3 "2015-09-01T11:55:02Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Javier\_Palacios](https://avatars.discourse-cdn.com/v4/letter/j/67e7ee/32.png) [@Javier\_Palacios](https://forum.ansible.com/u/Javier_Palacios)\
**Post date:** [September 1, 2015, 12:12pm UTC](https://forum.ansible.com/t/issue-with-default-value/16661/4 "2015-09-01T12:12:21Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Paul\_Totterman](https://avatars.discourse-cdn.com/v4/letter/p/3ec8ea/32.png) [@Paul\_Totterman](https://forum.ansible.com/u/Paul_Totterman)\
**Post date:** [September 1, 2015, 12:23pm UTC](https://forum.ansible.com/t/issue-with-default-value/16661/5 "2015-09-01T12:23:13Z")

</div>

> 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

---

<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:** [September 1, 2015, 12:48pm UTC](https://forum.ansible.com/t/issue-with-default-value/16661/6 "2015-09-01T12:48:29Z")

</div>

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")

---

<div class="post-metadata">

**Author:** ![Paul\_Totterman](https://avatars.discourse-cdn.com/v4/letter/p/3ec8ea/32.png) [@Paul\_Totterman](https://forum.ansible.com/u/Paul_Totterman)\
**Post date:** [September 1, 2015, 12:52pm UTC](https://forum.ansible.com/t/issue-with-default-value/16661/7 "2015-09-01T12:52:56Z")

</div>

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

---

<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:** [September 2, 2015, 12:41pm UTC](https://forum.ansible.com/t/issue-with-default-value/16661/8 "2015-09-02T12:41:49Z")

</div>

default is a fitler from the jinaj2 project, the 'fix' would have to  
happen there
