# "when" and "with\_items" again, with a twist

**URL:** <https://forum.ansible.com/t/when-and-with-items-again-with-a-twist/21855>\
**Category:** Ansible Project\
**Created:** [June 24, 2016, 4:37pm UTC](https://forum.ansible.com/t/when-and-with-items-again-with-a-twist/21855 "2016-06-24T16:37:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![henrique.rodrigues](https://avatars.discourse-cdn.com/v4/letter/h/22d042/32.png) [@henrique.rodrigues](https://forum.ansible.com/u/henrique.rodrigues)\
**Post date:** [June 24, 2016, 4:37pm UTC](https://forum.ansible.com/t/when-and-with-items-again-with-a-twist/21855/1 "2016-06-24T16:37:01Z")

</div>

Hi,

I’m getting an error when using “when” and “with\_items” together. As I’ve read many times, this happens because “when” runs inside “with\_items”, so we can use “| default()” to bypass the error, but I think I found a new twist on it. Here’s an example:

`  
$ cat example.yml

---

<div class="post-metadata">

**Author:** ![Barry\_Kaplan](https://avatars.discourse-cdn.com/v4/letter/b/48db29/32.png) [@Barry\_Kaplan](https://forum.ansible.com/u/Barry_Kaplan)\
**Post date:** [February 28, 2017, 4:47pm UTC](https://forum.ansible.com/t/when-and-with-items-again-with-a-twist/21855/2 "2017-02-28T16:47:13Z")

</div>

Anybody figure out to get around these deprecation warnings?

---

<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:** [February 28, 2017, 4:55pm UTC](https://forum.ansible.com/t/when-and-with-items-again-with-a-twist/21855/3 "2017-02-28T16:55:13Z")

</div>

default only applies to the 'last' item, so if you are going to do  
several depths of var defintion that can be undefined, you should have  
several defaults.

"{{ (example.json|default({})).url | default() }}"

---

<div class="post-metadata">

**Author:** ![Barry\_Kaplan](https://avatars.discourse-cdn.com/v4/letter/b/48db29/32.png) [@Barry\_Kaplan](https://forum.ansible.com/u/Barry_Kaplan)\
**Post date:** [February 28, 2017, 5:06pm UTC](https://forum.ansible.com/t/when-and-with-items-again-with-a-twist/21855/4 "2017-02-28T17:06:49Z")

</div>

Thanks Brian. Just saw that example in another post. It does work of course, but boy is it ugly.
