合并多个词典列表

时间:2019-07-18 11:48:14

标签: ansible yaml jinja2

我的角色很简单,它将模板推入/etc/sysconfig/network-scripts/route-<interface>

我们的某些路由是相同的,所以我想在角色vars和组/主机vars的帮助下分解路由。

我尝试应用this solution,但它期望有一个字典,并且我有一个字典列表,目前我对jinja过滤器还不太满意。

这是我的数据结构:

management_default_gateway: '192.168.128.254'
management_interface: eth1
routes:
  - interface: "{{management_interface}}"
    route_array:
      - gateway: "{{management_default_gateway}}"
        network: 10.0.0.1/24
      - gateway: "{{management_default_gateway}}"
        network: 10.0.0.5/24

即使模板上有其他接口循环路由列表(见下文),并按预期为每个接口创建一个文件,它也能正常工作。

roles/routing/tasks/main.yml

- name: Template
  template:
    src: route.j2
    dest: "{{'-'.join((route_script_file,item.interface))}}"
    owner: root
    group: root
    mode: 0640
  notify: networking restart
  with_list: "{{routes}}"

roles/routing/templates/route.j2

{% for i in item.route_array %}
{{i.network ~ ' via ' ~ i.gateway ~ '\n'}}
{%- endfor %}

我希望在角色vars文件夹(或group_vars/all)上具有“标准”路由,并在组或主机级别添加路由。

因此,我尝试使用: roles/routing/vars/main.yml

default_routes:
  - interface: "{{management_interface}}"
    route_array:
      - gateway: "{{management_default_gateway}}"
        network: 10.0.0.1/24
      - gateway: "{{management_default_gateway}}"
        network: 10.0.0.5/24

routes_merged: "{{ default_routes + specific_routes }}"

inventories/prod/group_vars/XXX.yml

specific_routes:
  - interface: "{{management_interface}}"
    route_array:
      - gateway: "{{management_default_gateway}}"
        network: 10.8.0.1/24

然后我用debug var得到了这个结果:

ok: [hostname] => {
    "routes": [
        {
            "interface": "eth1",
            "route_array": [
                {
                    "gateway": "192.168.128.254",
                    "network": "10.0.0.1/24"
                },
                {
                    "gateway": "192.168.128.254",
                    "network": "10.0.0.5/24"
                }
            ]
        },
        {
            "interface": "eth1",
            "route_array": [
                {
                    "gateway": "192.168.128.254",
                    "network": "10.8.0.1/24"
                }
            ]
        }
    ]
}

我想要:

ok: [hostname] => {
    "routes": [
        {
            "interface": "eth1",
            "route_array": [
                {
                    "gateway": "192.168.128.254",
                    "network": "10.0.0.1/24"
                },
                {
                    "gateway": "192.168.128.254",
                    "network": "10.0.0.5/24"
                },
                {
                    "gateway": "192.168.128.254",
                    "network": "10.8.0.1/24"
                }
            ]
        }
    ]
}

我想按接口合并route_array,以便仅由接口生成一个模板文件。

1 个答案:

答案 0 :(得分:0)

使用groupby进行循环

- set_fact:
    routes: "{{ default_routes + specific_routes }}"
- set_fact:
    routes2: "{{ routes2|default([]) +
                [{'interface': item.1|map(attribute='interface')|list|first,
                  'route_array': item.1|map(attribute='route_array')|list|flatten}] }}"
  loop: "{{ routes| groupby('interface') }}"
- debug:
    var: routes2

给予

"routes2": [
    {
        "interface": "eth1", 
        "route_array": [
            {
                "gateway": "192.168.128.254", 
                "network": "10.0.0.1/24"
            }, 
            {
                "gateway": "192.168.128.254", 
                "network": "10.0.0.5/24"
            }, 
            {
                "gateway": "192.168.128.254", 
                "network": "10.8.0.1/24"
            }
        ]
    }
]