"omit" but for ordinary variables?

13 views
Skip to first unread message

Karl Auer

unread,
Dec 20, 2018, 1:17:33 AM12/20/18
to ansible-project
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.

--
Karl Auer

Email  : ka...@2pisoftware.com
Website: http://2pisoftware.com


GPG/PGP : 958A 2647 6C44 D376 3D63 86A5 FFB2 20BC 0257 5816
Previous: F0AB 6C70 A49D 1927 6E05 81E7 AD95 268F 2AB6 40EA

James Cassell

unread,
Dec 20, 2018, 11:22:26 PM12/20/18
to Ansible List
On Thu, Dec 20, 2018, at 1:17 AM, Karl Auer wrote:
> 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 }}"


> ... 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.
>
> --
> Karl Auer
>
> Email : ka...@2pisoftware.com
> Website: http://2pisoftware.com
>
> GPG/PGP : 958A 2647 6C44 D376 3D63 86A5 FFB2 20BC 0257 5816
> Previous: F0AB 6C70 A49D 1927 6E05 81E7 AD95 268F 2AB6 40EA
>
> --
> You received this message because you are subscribed to the Google
> Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to ansible-proje...@googlegroups.com.
> To post to this group, send email to ansible...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/CA%2B%2BT08Q7kOqLoVar0J1n8tpgm%2BovauRw17m7ONhwdJgeMKvh9g%40mail.gmail.com.
> For more options, visit https://groups.google.com/d/optout.

Karl Auer

unread,
Dec 21, 2018, 2:00:23 AM12/21/18
to ansible-project
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.



For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages