I have a list of dicts like this:
list_of_dicts:
- name: Item1
- name: Item2
type: special
- name: Item3
- name: Item4
type: small
As you can see, not all items have a type
attribute. Context: If no type
attribute is set, that means the item is of the default type.
I would like to select all items which are NOT of type special
.
I've tried this: list_of_dicts | rejectattr('type', 'equalto', 'special')
Unfortunately, this fails with a AnsibleUndefinedVariable: 'dict object' has no attribute 'type'
error because some of the items do not have a type.
What is a good way to solve this?
Best Answer
Edit: I actually didn't pay enough attention to the latest edit of the existing answer from @ranjandas and the following comments. My first solution is almost similar. I guess you are more interested by the second one.
Here are 2 different ways to achieve your requirement.
The first solution uses only filters available in ansible by default:
rejectattr
you already mentionned and its counterpartselectattr
. The idea is to add two lists. The first one is made by selecting all dicts not having thetype
atttribute. The second one is made by selecting dict having thetype
attribute and rejecting those where it equalsspecial
.For the second solution, I used the
json_query
filter which requirespip install jmespath
on the controller. As you can see below it is much more compact.The choice is yours !
The demo playbook:
Which gives: