Ansible
在 ansible 列表變數中拒絕
我有這樣一個變數:
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}
?
下面的戲
- 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
通過rejectattr可以達到相同的結果
- set_fact: apps2: "{{ apps|rejectattr('name', 'match', '^C$') }}"