在Ansible的列表中附加到dict

时间:2017-03-08 13:52:49

标签: ansible jinja2 ansible-2.x

我需要将以下结构合并到单个dicts列表中。

我可以更改public class Line extends JPanel{ @Override public void paintComponents(Graphics g){ super.paintComponent(g); g.setColor(Color.red); g.drawLine(100, 10, 30, 40); } public static void main(String[] args) { Line l =new Line(); JFrame myFrame = new JFrame("Line"); myFrame.setSize(600, 400); myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); myFrame.add(l); myFrame.setVisible(true); } } 列表的语法/结构(如果更容易,可以更改为其他内容)。

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 }}"

1 个答案:

答案 0 :(得分:4)

例如:

---
- 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 }}"