# skip a task with\_subelements if its dictionary is undefined

**URL:** <https://forum.ansible.com/t/skip-a-task-with-subelements-if-its-dictionary-is-undefined/15990>\
**Category:** Ansible Project\
**Created:** [August 20, 2014, 8:46am UTC](https://forum.ansible.com/t/skip-a-task-with-subelements-if-its-dictionary-is-undefined/15990 "2014-08-20T08:46:04Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nakai\_Tooru](https://avatars.discourse-cdn.com/v4/letter/n/5f9b8f/32.png) [@Nakai\_Tooru](https://forum.ansible.com/u/Nakai_Tooru)\
**Post date:** [August 20, 2014, 8:46am UTC](https://forum.ansible.com/t/skip-a-task-with-subelements-if-its-dictionary-is-undefined/15990/1 "2014-08-20T08:46:04Z")

</div>

Hello,

Is it possible to skip a task with\_subelements if its dictionary is undefined?

For example,

- authorized\_key: “user={{ item.0.name }} key=‘{{ lookup(‘file’, item.1)}}’”  
with\_subelements:
- users
- authorized  
when: users is defined

This is failed with;  
fatal: [localhost] =\> subelements lookup expects a dictionary, got ‘users’.

It seems with\_subelements evaluate the action before check condition.

Thanks

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [August 20, 2014, 12:51pm UTC](https://forum.ansible.com/t/skip-a-task-with-subelements-if-its-dictionary-is-undefined/15990/2 "2014-08-20T12:51:01Z")

</div>

With a simple list, you can do stuff like:

- shell: echo {{ item }}  
with\_items: alist\_that\_may\_be\_undefined | default()

Which will iterate over a list of 0 if there is nothing in it.

What you might wish to do is set users by default to an empty array (perhaps in roles/foo/defaults.yml) or something similar, to ensure it always has a value.

---

<div class="post-metadata">

**Author:** ![C\_Morgan\_Hamill](https://avatars.discourse-cdn.com/v4/letter/c/ebca7d/32.png) [@C\_Morgan\_Hamill](https://forum.ansible.com/u/C_Morgan_Hamill)\
**Post date:** [August 20, 2014, 1:12pm UTC](https://forum.ansible.com/t/skip-a-task-with-subelements-if-its-dictionary-is-undefined/15990/3 "2014-08-20T13:12:29Z")

</div>

Excerpts from Michael DeHaan's message of 2014-08-20 08:51:01 -0400:

> With a simple list, you can do stuff like:
> 
> - shell: echo {{ item }}  
> &nbsp;&nbsp;with\_items: alist\_that\_may\_be\_undefined | default()

I'm fairly sure this works also:

&nbsp;&nbsp;- shell: echo {{ item }}  
&nbsp;&nbsp;&nbsp;&nbsp;with\_subelements:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- some\_list|default()  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- some\_subkey

---

<div class="post-metadata">

**Author:** ![Nakai\_Tooru](https://avatars.discourse-cdn.com/v4/letter/n/5f9b8f/32.png) [@Nakai\_Tooru](https://forum.ansible.com/u/Nakai_Tooru)\
**Post date:** [August 21, 2014, 2:08am UTC](https://forum.ansible.com/t/skip-a-task-with-subelements-if-its-dictionary-is-undefined/15990/4 "2014-08-21T02:08:14Z")

</div>

Morgan Hamill:

> Excerpts from Michael DeHaan’s message of 2014-08-20 08:51:01 -0400:
> 
> > With a simple list, you can do stuff like:
> > 
> > - shell: echo {{ item }}  
> > with\_items: alist\_that\_may\_be\_undefined | default()
> 
> I’m fairly sure this works also:
> 
> - shell: echo {{ item }}  
> with\_subelements:
> - some\_list|default()
> - some\_subkey

This is what I want, awesome!  
Thank you.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [August 21, 2014, 2:41am UTC](https://forum.ansible.com/t/skip-a-task-with-subelements-if-its-dictionary-is-undefined/15990/5 "2014-08-21T02:41:00Z")

</div>

Ha, a nice consequence that that also works. Much rejoicing!
