将字典列表转换为字典字典或另一个字典列表

问题描述 投票:0回答:1

这是关于 Ansible 的 我想我可以用

set_fact
做到这一点,但不知怎的,我的思绪被卡住了。

我有这个词典列表:

    repolist:
      - org: rhosp-rhel8
        registry: registry.redhat.io
        repo: openstack-zaqar-base
      - org: rhosp-rhel8
        registry: registry.redhat.io
        repo: openstack-zaqar-wsgi
      - org: rhceph
        registry: registry.redhat.io
        repo: rhceph-4-dashboard-rhel8

我想把它转换成这样:

repolist:
  - org: rhosp-rhel8
    repos: 
        - name: openstack-zaqar-base
          registry: registry.redhat.io
        - name: openstack-zaqar-wsgi
          registry: registry.redhat.io
  - org: rhceph
    repos: 
      - name: rhceph-4-dashboard-rhel8
        registry: registry.redhat.io

或者这个:

repolist:
  org: rhosp-rhel8
  repos: 
    - name: openstack-zaqar-base
      registry: registry.redhat.io
    - name: openstack-zaqar-wsgi
       registry: registry.redhat.io
  org: rhceph
  repos: 
    - name: rhceph-4-dashboard-rhel8
      registry: registry.redhat.io

请问有什么帮助吗?

list dictionary ansible
1个回答
0
投票

最简单的选择

  result: "{{ dict(repolist|groupby('org')) }}"

给予

  result:
    rhceph:
    - org: rhceph
      registry: registry.redhat.io
      repo: rhceph-4-dashboard-rhel8
    rhosp-rhel8:
    - org: rhosp-rhel8
      registry: registry.redhat.io
      repo: openstack-zaqar-base
    - org: rhosp-rhel8
      registry: registry.redhat.io
      repo: openstack-zaqar-wsgi

(可选)创建一个列表

  result: "{{ dict(repolist|groupby('org')) |
              dict2items(key_name='org', value_name='repos') }}"

给予

  result:
  - org: rhceph
    repos:
    - org: rhceph
      registry: registry.redhat.io
      repo: rhceph-4-dashboard-rhel8
  - org: rhosp-rhel8
    repos:
    - org: rhosp-rhel8
      registry: registry.redhat.io
      repo: openstack-zaqar-base
    - org: rhosp-rhel8
      registry: registry.redhat.io
      repo: openstack-zaqar-wsgi

如果必须删除属性org,请使用过滤器ansible.utils.remove_keys

  r_groups: "{{ repolist | groupby('org') }}"
  r_keys: "{{ r_groups | map('first') }}"
  r_vals: "{{ r_groups | map('last') |
              ansible.utils.remove_keys(target=['org']) }}"
  result: "{{ dict(r_keys|zip(r_vals)) }}"

给予

  result:
    rhceph:
    - registry: registry.redhat.io
      repo: rhceph-4-dashboard-rhel8
    rhosp-rhel8:
    - registry: registry.redhat.io
      repo: openstack-zaqar-base
    - registry: registry.redhat.io
      repo: openstack-zaqar-wsgi

(可选)创建一个列表

  result: "{{ dict(r_keys|zip(r_vals)) |
              dict2items(key_name='org', value_name='repos') }}"

给予

  result:
  - org: rhceph
    repos:
    - registry: registry.redhat.io
      repo: rhceph-4-dashboard-rhel8
  - org: rhosp-rhel8
    repos:
    - registry: registry.redhat.io
      repo: openstack-zaqar-base
    - registry: registry.redhat.io
      repo: openstack-zaqar-wsgi

用于测试的完整剧本示例

- hosts: localhost

  vars:

    repolist:
      - org: rhosp-rhel8
        registry: registry.redhat.io
        repo: openstack-zaqar-base
      - org: rhosp-rhel8
        registry: registry.redhat.io
        repo: openstack-zaqar-wsgi
      - org: rhceph
        registry: registry.redhat.io
        repo: rhceph-4-dashboard-rhel8

    result: "{{ dict(repolist|groupby('org')) }}"
    resul2: "{{ dict(repolist|groupby('org')) |
                dict2items(key_name='org', value_name='repos') }}"

    r_groups: "{{ repolist | groupby('org') }}"
    r_keys: "{{ r_groups | map('first') }}"
    r_vals: "{{ r_groups | map('last') | ansible.utils.remove_keys(target=['org']) }}"
    resul3: "{{ dict(r_keys|zip(r_vals)) }}"
    resul4: "{{ dict(r_keys|zip(r_vals)) |
                dict2items(key_name='org', value_name='repos') }}"

  tasks:

    - debug:
        var: result
    - debug:
        var: resul2
    - debug:
        var: resul3
    - debug:
        var: resul4
© www.soinside.com 2019 - 2024. All rights reserved.