2017-06-16 35 views
0

在Ansible中我有以下列表並希望獲得扁平結構。 輸入:Ansible list flatten

[ 
{ 
    id: "one", 
    level: "3" 
}, 
{ 
    id: "two", 
    level: "8" 
}, 
... 
] 

所需的輸出:

[ 
{ 
    one: "3" 
}, 
{ 
    two: "8" 
}, 
... 
] 

回答

1

有沒有簡單的方法開箱。

您可以自定義template filter使用:

my_list | map('template','{"<<item.id>>":"<<item.level>>"}',from_json=true) | list 

或產生與set_fact + register + with_items新的變量,看Process complex variables with set_fact and with_items

- set_fact: 
    tmp_item: '{ "{{ item.id }}": "{{ item.level }}" }' 
    with_items: "{{ my_list }}" 
    register: tmp_list 
- debug: 
    msg: "{{ tmp_list.results | map(attribute='ansible_facts.tmp_item') | list }}"