# default filter on with\_items with from\_json

**URL:** <https://forum.ansible.com/t/default-filter-on-with-items-with-from-json/28296>\
**Category:** Ansible Project\
**Created:** [August 8, 2018, 8:10pm UTC](https://forum.ansible.com/t/default-filter-on-with-items-with-from-json/28296 "2018-08-08T20:10:11Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![rjwagner.dba](https://avatars.discourse-cdn.com/v4/letter/r/e8c25b/32.png) [@rjwagner.dba](https://forum.ansible.com/u/rjwagner.dba)\
**Post date:** [August 8, 2018, 8:10pm UTC](https://forum.ansible.com/t/default-filter-on-with-items-with-from-json/28296/1 "2018-08-08T20:10:11Z")

</div>

I’m running into the infamous issue where with\_items isn’t skipped if when evaluates to false ([https://github.com/ansible/ansible/issues/13791](https://github.com/ansible/ansible/issues/13791)). That said, I’m at loss as to how to workaround my particular issue.

Consider these two tasks (simplified for discussion):

`  
#\> cat b.yml

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [August 8, 2018, 9:33pm UTC](https://forum.ansible.com/t/default-filter-on-with-items-with-from-json/28296/2 "2018-08-08T21:33:49Z")

</div>

> I'm running into the infamous issue where with\_items isn't skipped if when  
> evaluates to false ([https://github.com/ansible/ansible/issues/13791](https://github.com/ansible/ansible/issues/13791)). That  
> said, I'm at loss as to how to workaround my particular issue.

It's not that infamous, it's just how Jinja work, and when you understand the concept it is easy to deal with.

> Consider these two tasks (simplified for discussion):
> 
> #\> cat b.yml  
> ---  
> - hosts: 127.0.0.1  
> &nbsp;&nbsp;connection: local  
> &nbsp;&nbsp;gather\_facts: False  
> &nbsp;&nbsp;tasks:  
> &nbsp;&nbsp;- name: get json  
> &nbsp;&nbsp;&nbsp;&nbsp;shell: "echo '{ \"a\": [1,2,3] }'"  
> &nbsp;&nbsp;&nbsp;&nbsp;register: b  
> &nbsp;&nbsp;&nbsp;&nbsp;when: X=='1'  
> &nbsp;&nbsp;- name: echo a array  
> &nbsp;&nbsp;&nbsp;&nbsp;debug: msg="{{ item }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;with\_items: "{{ (b.stdout | from\_json).a }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;when: X=='1'

When X=0 the variable b will be defined, it contains data about the sipped reason.  
But b will not contain the key stdout so this variable need to be filtered through default filter.

Since b.stdout is filter through from\_json the default filter need to provide a empty json string to satisfy the filter.

&nbsp;&nbsp;&nbsp;with\_items: "{{ (b.stdout | from\_json).a }}"

becomes

&nbsp;&nbsp;&nbsp;with\_items: "{{ (b.stdout | default('{}') | from\_json).a }}"

but this will also fail since the empty {} json string doesn't contain a key name "a" so this also need to go through the default filter.  
Since with\_items need a list this must be an empty list , so it becomes

&nbsp;&nbsp;&nbsp;with\_items: "{{ (b.stdout | default('{}') | from\_json).a | default() }}"

So the principle is, if a variable is not defined it need to go through the default filter.

I hope this explanation gave some meaning.

---

<div class="post-metadata">

**Author:** ![rjwagner.dba](https://avatars.discourse-cdn.com/v4/letter/r/e8c25b/32.png) [@rjwagner.dba](https://forum.ansible.com/u/rjwagner.dba)\
**Post date:** [August 9, 2018, 1:47pm UTC](https://forum.ansible.com/t/default-filter-on-with-items-with-from-json/28296/3 "2018-08-09T13:47:15Z")

</div>

Thanks very much, Kai. This does help my understanding. I was trying to use default() after the b.stdout to generate the JSON (i.e., have default generate ‘{ “a”: “” }’) and wasn’t having any luck because of all the nested quotes. I still consider it counterintuitive that Ansible evaluates with\_items: even if when: skips it, but no use arguing here.

Thanks again for your help. Appreciate it.

Rob

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [August 9, 2018, 4:06pm UTC](https://forum.ansible.com/t/default-filter-on-with-items-with-from-json/28296/4 "2018-08-09T16:06:41Z")

</div>

That's because when is evaluated for each element in with\_items, so with\_items must run before when.  
Remember that you can use the item variable in when, just to illustrate

&nbsp;&nbsp;&nbsp;- debug: msg="I run"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: item == True  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;with\_items:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- True  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- False  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- True

with\_items must run first so when has the correct value for the variable item.

---

<div class="post-metadata">

**Author:** ![rjwagner.dba](https://avatars.discourse-cdn.com/v4/letter/r/e8c25b/32.png) [@rjwagner.dba](https://forum.ansible.com/u/rjwagner.dba)\
**Post date:** [August 10, 2018, 9:04pm UTC](https://forum.ansible.com/t/default-filter-on-with-items-with-from-json/28296/5 "2018-08-10T21:04:41Z")

</div>

Ah, yes. Makes sense now. Thanks Kai.

Rob
