我有这样一个变量:
apps:
- {name: kapitalism, extension: .war}
- {name: socialism, extension: .war}
- {name: somethingelse, extension: .ear}
我需要在此基础上再制作一个,但其中不会包含列表中的部分内容。我正在尝试这样做:
- 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 }}"
但是这里我遇到了这样一个问题,扩展参数丢失了,我得到了变量:
apps: [u'kapitalism', u'somethingelse']
如何创建这样的变量:
apps:
- {name: kapitalism, extension: .war}
- {name: somethingelse, extension: .ear}
?
答案1
下面的剧情
- 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
给出(删节版):
apps2:
- extension: .ab
name: A
- extension: .ab
name: B
可以通过以下方式实现相同的结果拒绝属性
- set_fact:
apps2: "{{ apps|rejectattr('name', 'match', '^C$') }}"