5

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} 

?

1 Answer 1

7

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$') }}" 
1
  • ty!!! did the first way - everything works. Commented May 30, 2019 at 7:19

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.