"omit" but for ordinary variables?

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.

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 }}"

Try

- set_fact:
     mylist: "{{ mylist + [ {'this': item.fred } | combine(item.mary is defined | ternary({ 'that': item.mary }, {})] }}"
  with_items: "{{ alist }}"

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 :slight_smile:

Regards, K.