2017-03-08 79 views
1

我需要將以下結構合併到一個單獨的字典列表中。附加到Ansible中的列表中的字典

我可以更改secrets列表的語法/結構(如果更容易,可以將其改爲別的東西)。

vars: 
    users: 
    - name: foo 
     database: foodb 
    - name: bar 
     database: bardb 

    secrets: 
    - name: foo 
     password: foopass 
    - name: bar 
     password: barpass 

預期結果:

users: 
    - name: foo 
    database: foodb 
    password: foopass 
    - name: bar 
    database: bardb 
    password: barpass 

我嘗試了好幾種過濾器無濟於事,我來最接近的是這樣的:

tasks: 
    - set_fact: foo: "{{ foo|default([]) + [item | combine(secrets, recursive=True)] }}" 
    with_items: "{{ users }}" 

結果:

fatal: [localhost]: FAILED! => {"failed": true, "msg": "|combine expects dictionaries, got u'foo'"} 

回答

2

例如:

--- 
- hosts: localhost 
    gather_facts: no 
    vars: 
    users: 
     - name: foo 
     database: foodb 
     - name: bar 
     database: bardb 
    secrets: 
     - name: foo 
     password: foopass 
     - name: bar 
     password: barpass 
    tasks: 
    - set_fact: 
     tmp_user: "{{ item | combine(secrets | selectattr('name','equalto',item.name) | first) }}" 
     with_items: "{{ users }}" 
     register: tmp_users 
    - set_fact: 
     users: "{{ tmp_users.results | map(attribute='ansible_facts.tmp_user') | list }}" 
    - debug: 
     msg: "{{ users }}" 
+0

優秀!按原樣工作:-) –