# Possible to set default value for missing key when using with\_items and when?

**URL:** <https://forum.ansible.com/t/possible-to-set-default-value-for-missing-key-when-using-with-items-and-when/25242>\
**Category:** Ansible Project\
**Created:** [August 13, 2017, 10:27pm UTC](https://forum.ansible.com/t/possible-to-set-default-value-for-missing-key-when-using-with-items-and-when/25242 "2017-08-13T22:27:46Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ryan\_Fisher](https://avatars.discourse-cdn.com/v4/letter/r/a4c791/32.png) [@Ryan\_Fisher](https://forum.ansible.com/u/Ryan_Fisher)\
**Post date:** [August 13, 2017, 10:27pm UTC](https://forum.ansible.com/t/possible-to-set-default-value-for-missing-key-when-using-with-items-and-when/25242/1 "2017-08-13T22:27:46Z")

</div>

I’m trying to avoid having to put a key into every host\_vars that uses a role that executes against multiple environments. Maybe I’m thinking about this wrong?

**host\_vars:**

`

---

<div class="post-metadata">

**Author:** ![Ryan\_Fisher](https://avatars.discourse-cdn.com/v4/letter/r/a4c791/32.png) [@Ryan\_Fisher](https://forum.ansible.com/u/Ryan_Fisher)\
**Post date:** [August 14, 2017, 12:58am UTC](https://forum.ansible.com/t/possible-to-set-default-value-for-missing-key-when-using-with-items-and-when/25242/2 "2017-08-14T00:58:51Z")

</div>

It seems that I can use [selectattr](http://jinja.pocoo.org/docs/2.9/templates/#selectattr) to accomplish this.

Example:

`@>>> from jinja2 import Template @>>> t = Template("{{ some_var|selectattr('allow', 'defined')|list }}") @>>> json = { @... "some_var": [@... { @... "name": "one", @... "param": "foo", @... "allow": "True" @... }, @... { @... "name": "two", @... "param": "bar", @... } @...] @... } @>>> t.render(json) u"[{'allow': 'True', 'name': 'one', 'param': 'foo'}]"`

---

<div class="post-metadata">

**Author:** ![Ryan\_Fisher](https://avatars.discourse-cdn.com/v4/letter/r/a4c791/32.png) [@Ryan\_Fisher](https://forum.ansible.com/u/Ryan_Fisher)\
**Post date:** [August 14, 2017, 1:24am UTC](https://forum.ansible.com/t/possible-to-set-default-value-for-missing-key-when-using-with-items-and-when/25242/3 "2017-08-14T01:24:54Z")

</div>

It would be nice if there was a way to assign the key with a default value. That way all the objects can be passed to with\_items and skipped by when, which will show them in the log/output.

---

<div class="post-metadata">

**Author:** ![Pshem\_Kowalczyk](https://avatars.discourse-cdn.com/v4/letter/p/e480ec/32.png) [@Pshem\_Kowalczyk](https://forum.ansible.com/u/Pshem_Kowalczyk)\
**Post date:** [August 14, 2017, 1:29am UTC](https://forum.ansible.com/t/possible-to-set-default-value-for-missing-key-when-using-with-items-and-when/25242/4 "2017-08-14T01:29:02Z")

</div>

Hi,

You can always use " | default(‘value’) " filter if you’re not sure if a value is there. Value can be also a list or a dictionary.  
Alternatively for ‘when’ you can test if the value is defined:

when: item.my\_var is defined and my\_var == …

kind regards  
Pshem

---

<div class="post-metadata">

**Author:** ![Ryan\_Fisher](https://avatars.discourse-cdn.com/v4/letter/r/a4c791/32.png) [@Ryan\_Fisher](https://forum.ansible.com/u/Ryan_Fisher)\
**Post date:** [August 14, 2017, 2:41am UTC](https://forum.ansible.com/t/possible-to-set-default-value-for-missing-key-when-using-with-items-and-when/25242/5 "2017-08-14T02:41:40Z")

</div>

Thanks, Pshem, using when/defineds/and get’s me exactly what I wanted.

I was using this initially and still getting a fatal error because when was trying to check an attribute that didn’t exist:

`  
when:

- item.my\_var is defined
- item.myvar  
`

It seems that syntax doesn’t short-circuit like you would expect `and` to behave. You must use “when: item.my\_var is defined **and** item.my\_var.”

I also attempted to use the “|default(False)” approach, but I couldn’t figure out a way to apply the default value to only an attribute in a list of objects (“flag: False” in my example).

Thanks again for the help,
