2016-04-08 8 views
2

をJinja2のフィルタを使用して辞書項目のリストを抽出し、構築する私はこのようなSTHを持っている:どうansible

データ:

--- 
modules: 
- name: m1 
    migrations: 
     - name: abc 
     attr: testabc 
     - name: def 
     attr: testdef 
- name: m2 
    migrations: 
     - name: ghi 
     attr: testabc 
- name: m3 
    migrations: [] 

Ansibleタスク:

- name: All migrations 
    set_fact: 
    migrations: "{{modules|map(attribute='migrations')|list }}" 

出力:

出力は次のようにsthです:

私は必要なもの
[ 
    [ 
    { 
    name: abc 
    attr: testabc 
    }, 
    { 
    name: def 
    attr: testdef 
    } 
], 
[ 
    { 
    name: ghi 
    attr: testabc 
    } 
], 
[] 
] 

されました:

[ 
    { 
    name: abc 
    attr: testabc 
    }, 
    { 
    name: def 
    attr: testdef 
    },  
    { 
    name: ghi 
    attr: testabc 
    }  
] 

答えて

1

これは誰かに役立つかもしれない、私はで辞書のリストを取得するために管理:

- name: All migrations 
    set_fact: 
    migrations: "{% set migrations = migrations|default([]) + [item.1] %}{{migrations|list}}" 
    with_subelements: 
     - "{{ modules }}" 
     - migrations 

短いバージョンがあるかもしれませんが、私は知りませんまだ!

関連する問題