Ansible:如何合并具有相同键的字典对象列表,以获取一个字典元素以及分组的不同值列表?

Ansible:如何合并具有相同键的字典对象列表,以获取一个字典元素以及分组的不同值列表?

我已经为 Ansible 苦苦挣扎了一段时间,最后请求任何可能的帮助,

拥有一套 Nifi 策略:

"nifi_raw_policies": [
    {
        "action": "read",
        "group": "8c052e6c-0184-1000-0000-000072a0bb44",
        "resource": "/data/process-groups/8b6b5c9f-0184-1000-57e3-f27fc56dd4aa"
    },
    {
        "action": "read",
        "group": "8c0536d8-0184-1000-0000-000018ba98a9",
        "resource": "/data/process-groups/8b6b5c9f-0184-1000-57e3-f27fc56dd4aa"
    }
]

我需要对它们进行分组以获得:

"nifi_grouped_policies": [
    {
        "policy": "read/data/process-groups/8b6b5c9f-0184-1000-57e3-f27fc56dd4aa",
        "groups": [ "8c052e6c-0184-1000-0000-000072a0bb44","8c0536d8-0184-1000-0000-000018ba98a9" ]
    }
]

但直到现在,我仍然无法按“action”+“resource”键进行分组,从而在新的字段“groups”中获取相关“group”的列表。我只能获得形式为 {“action”+“resource”:“group1”},...,{“action”+“resource”:“groupN”} 的字典列表

相关ansible任务代码:

- name: Declare policies (simplest)
  set_fact:
    nifi_raw_policies: [
        {
            "action": "read",
            "group": "8c052e6c-0184-1000-0000-000072a0bb44",
            "resource": "/data/process-groups/8b6b5c9f-0184-1000-57e3-f27fc56dd4aa"
        },
        {
            "action": "read",
            "group": "8c0536d8-0184-1000-0000-000018ba98a9",
            "resource": "/data/process-groups/8b6b5c9f-0184-1000-57e3-f27fc56dd4aa"
        }
    ]

- name: Declare policies - debug
  debug: var=nifi_raw_policies

- name: Combine action and resource
  set_fact:
    nifi_policies: >-
      {{ nifi_policies|default([])
        | union([{
            item.action + item.resource : item.group
            }])
      }}
  with_items: "{{ nifi_raw_policies }}"

- name: Combine action and resource - debug
  debug: var=nifi_policies

经过多次尝试使用结合联盟通过...分组ansible 过滤器没有成功。如能提供任何见解,我们将不胜感激。

此致,

答案1

使用 Jinja 创建结构。例如,给定姆雷简化数据

  raw:
    - action: read
      group: 1
      resource: data
    - action: read
      group: 2
      resource: data

声明字典如何重命名键

    keys:
      group: groups
      resource: policy

并声明结构

  grouped: |
    {% for i in raw|groupby('action') %}
    {{ i.0 }}:
    {% for k in i.1|json_query('[].keys(@)|[]')|unique %}
    {% if k != 'action' %}
      {{ keys[k] }}: {{ i.1|map(attribute=k)|unique }}
    {% endif %}
    {% endfor %}
    {% endfor %}

给出

  grouped: |-
    read:
      groups: [1, 2]
      policy: ['data']

您只能获取值

  grouped_vals: "{{ grouped|from_yaml|dict2items|map(attribute='value')|list }}"

给出

  grouped_vals:
    - groups: [1, 2]
      policy: [data]

列表中的第一项就是您要查找的内容。或者,您可以从字典中获取它

  nifi_grouped_policies: "{{ (grouped|from_yaml).read }}"

给出

  nifi_grouped_policies:
    groups: [1, 2]
    policy: [data]

完整测试剧本的示例

- hosts: localhost

  vars:

    raw:
      - action: read
        group: 1
        resource: data
      - action: read
        group: 2
        resource: data

    keys:
      group: groups
      resource: policy
    grouped: |
      {% for i in raw|groupby('action') %}
      {{ i.0 }}:
      {% for k in i.1|json_query('[].keys(@)|[]')|unique %}
      {% if k != 'action' %}
        {{ keys[k] }}: {{ i.1|map(attribute=k)|unique }}
      {% endif %}
      {% endfor %}
      {% endfor %}
    grouped_vals: "{{ grouped|from_yaml|dict2items|map(attribute='value')|list }}"
    nifi_grouped_policies: "{{ (grouped|from_yaml).read }}"

  tasks:

    - debug:
        var: grouped
    - debug:
        var: grouped_vals|to_yaml
    - debug:
        var: nifi_grouped_policies|to_yaml

相关内容