I am trying to build a list of names from a list of objects. It works fine,
but some of the objects don't have a name. No problem, I use the default()
filter to set those elements of my list to empty strings, like this:
# Build a list of names from a list of things
- set_fact:
names: "{{ names |default([]) + [ item.name |default('') ] }}"
with_items: "{{ things }}"
This got me, as expected, this result (because at the moment, my test list
of things has two items, and neither has a name attribute):
ok: [localhost] => {
"names": [
"",
""
]
}
Now, how can I remove those empty strings from my list? I have read
numerous articles that mention "rejectattr", but I don't understand how to
use it.
I tried this:
# Remove any empty elements from the list of instance profile names
- set_fact:
names: "{{ names |default([]) |reject('equalto', '') }}"
And got this mystifying output:
ok: [localhost] => {
"names": "<generator object _select_or_reject at 0x7f4ecd9c9aa0>"
}
I also tried using rejectattr on the first loop like this:
# Build a list of names from a list of things
- set_fact:
names: "{{ names |default([]) + [ item.name |default('') ] }}"
with_items: "{{ things |rejectattr('name', 'undefined') }}"
That got me a list with ONE empty string:
ok: [localhost] => {
"names": [
""
]
}
Grateful for any pointers...
Regards, K.
PS: Pointers to USEFUL, COMPLETE examples of how to use rejectattr(),
reject() and so on would be useful too. All the examples I have found seem
to assume a huge amount of knowledge I don't have, or are tiny fragments of
code...
--
Karl Auer
Email : [email protected]
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 [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/ansible-project/CA%2B%2BT08TyAdr%3DN8ssaLC0jwqsZWvsiw4aKPddWbHXOBGZX38kUw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.