Reject in ansible list variable
I have such a variable:
apps:
- {name: kapitalism, extension: .war}
- {name: socialism, extension: .war}
- {name: somethingelse, extension: .ear}
And I need to make another one based on it, which will not include some of the list. I'm trying this:
- name: Reject this please
set_fact:
apps: "{{ apps | map(attribute='name') | reject('search',item.name) | list }}"
when: "item.name.find('socialism') != -1"
with_items: "{{ apps }}"
But here I get such a problem that the extension parameter is lost and I get the variable:
apps: [u'kapitalism', u'somethingelse']
How to make a variable like this:
apps:
- {name: kapitalism, extension: .war}
- {name: somethingelse, extension: .ear}
?
The play below
- hosts: localhost
vars:
apps2: []
apps:
- {name: A, extension: .ab}
- {name: B, extension: .ab}
- {name: C, extension: .c}
tasks:
- set_fact:
apps2: "{{ apps2 + [ item ] }}"
loop: "{{ apps }}"
when: item.name != 'C'
- debug:
var: apps2
gives (abridged):
"apps2": [
{
"extension": ".ab",
"name": "A"
},
{
"extension": ".ab",
"name": "B"
}
]
The same result can be achieved by rejectattr
- set_fact:
apps2: "{{ apps|rejectattr('name', 'match', 'C')|list }}"