# "omit" but for ordinary variables?

**URL:** <https://forum.ansible.com/t/omit-but-for-ordinary-variables/29309>\
**Category:** Ansible Project\
**Created:** [December 20, 2018, 6:17am UTC](https://forum.ansible.com/t/omit-but-for-ordinary-variables/29309 "2018-12-20T06:17:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Karl\_Auer](https://avatars.discourse-cdn.com/v4/letter/k/cc9497/32.png) [@Karl\_Auer](https://forum.ansible.com/u/Karl_Auer)\
**Post date:** [December 20, 2018, 6:17am UTC](https://forum.ansible.com/t/omit-but-for-ordinary-variables/29309/1 "2018-12-20T06:17:12Z")

</div>

Is there anything like the “default(omit)” filter available for ordinary variable declarations?

I am building a list of volume details, which will be passed too the ec2 module. With module parameters, one can comvert an undefined parameter inot no parameter at all with the “default(omit)” filter, but I cn’t find a way to simply leave out an undefined item from the list I m building.

Say my input is a list of tables like this:

alist:

- { ‘fred’: value1, ‘mary’: value2 }
- { 'fred: value3 }

I want to process that list into another list using set\_fact, so:

- set\_fact:  
mylist: “{{ mylist + [{‘this’: item.fred, ‘that’: item.mary }] }}”  
with\_items: “{{ alist }}”

… and end up with

{ ‘this’: value1, ‘that’: value2}  
{ ‘this’: value3 }

But I can’t figure out how to omit item.mary if it is undefined.

Any clues?

Regards, K.

---

<div class="post-metadata">

**Author:** ![cassell](https://avatars.discourse-cdn.com/v4/letter/c/ecae2f/32.png) [@cassell](https://forum.ansible.com/u/cassell)\
**Post date:** [December 21, 2018, 4:22am UTC](https://forum.ansible.com/t/omit-but-for-ordinary-variables/29309/2 "2018-12-21T04:22:10Z")

</div>

> Is there anything like the "default(omit)" filter available for ordinary  
> variable declarations?
> 
> I am building a list of volume details, which will be passed too the ec2  
> module. With module parameters, one can comvert an undefined parameter inot  
> no parameter at all with the "default(omit)" filter, but I cn't find a way  
> to simply leave out an undefined item from the list I m building.
> 
> Say my input is a list of tables like this:
> 
> alist:  
> &nbsp;&nbsp;&nbsp;- { 'fred': value1, 'mary': value2 }  
> &nbsp;&nbsp;&nbsp;- { 'fred: value3 }
> 
> I want to process that list into another list using set\_fact, so:
> 
> - set\_fact:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mylist: "{{ mylist + [{'this': item.fred, 'that': item.mary }] }}"  
> &nbsp;&nbsp;with\_items: "{{ alist }}"

Try

- set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mylist: "{{ mylist + [{'this': item.fred } | combine(item.mary is defined | ternary({ 'that': item.mary }, {})] }}"  
&nbsp;&nbsp;with\_items: "{{ alist }}"

---

<div class="post-metadata">

**Author:** ![Karl\_Auer](https://avatars.discourse-cdn.com/v4/letter/k/cc9497/32.png) [@Karl\_Auer](https://forum.ansible.com/u/Karl_Auer)\
**Post date:** [December 21, 2018, 7:00am UTC](https://forum.ansible.com/t/omit-but-for-ordinary-variables/29309/3 "2018-12-21T07:00:03Z")

</div>

Nice! Thank you.

I am a bit annoyed with myself, because I have used exactly that technique to avoid a missing list. I don’t know why it never occurred to me to avoid a missing map the same way 🙂

Regards, K.
