Ansible - 如何将列表组合为嵌套字典中的值

时间:2021-03-25 21:38:36

标签: dictionary ansible

我有一个单独的列表和一个字典列表,我试图将它们组合成一个字典,以便在剧本中更有效地访问:

一个简单的列表,名为“volume_device_path”:

ok: [node1] => {
    "volume_device_path": [
        "/dev/sanstorage/0987654321",
        "/dev/sanstorage/1234567890"
    ]
}

列表中的字典,名为“volumes”:

ok: [node1] => {
    "volumes": [
        {
            "format_opts": null,
            "fs_type": "xfs",
            "mount_opts": "noatime",
            "mount_path": "/data/fc100",
            "storage_protocol": "fc",
            "vol_desc": "ansible volume for fc",
            "vol_name": "data100",
            "vol_perf_pol": "high",
            "vol_size": 100000,
            "vol_suffix": "data100"
        },
        {
            "format_opts": null,
            "fs_type": "ext4",
            "mount_opts": "noatime",
            "mount_path": "/data/fc111",
            "storage_protocol": "fc",
            "vol_desc": "ansible volume for fc",
            "vol_name": "data111",
            "vol_size": 100000,
            "vol_suffix": "data111"
        }
    ]
}

我想创建一个名为“volumes_combined”的新字典,它看起来像这样,结合了两个早期的数据结构:

ok: [node1] => {
    "volumes_combined": [
        {
            "volume_device_path": "/dev/sanstorage/0987654321",
            "format_opts": null,
            "fs_type": "xfs",
            "mount_opts": "noatime",
            "mount_path": "/data/fc100",
            "storage_protocol": "fc",
            "vol_desc": "ansible volume for fc",
            "vol_name": "data100",
            "vol_size": 100000,
            "vol_suffix": "data100"
        },
        {
            "volume_device_path": "/dev/sanstorage/1234567890",
            "format_opts": null,
            "fs_type": "ext4",
            "mount_opts": "noatime",
            "mount_path": "/data/fc111",
            "storage_protocol": "fc",
            "vol_desc": "ansible volume for fc",
            "vol_name": "data111",
            "vol_size": 100000,
            "vol_suffix": "data111"
        }
    ]
}

我能够非常接近以下内容,但遇到了复杂情况。

'volume_device_path' 列表值不会像我希望的那样嵌套在字典中。如何将初始列表值组合到嵌套字典中?

    - name: combine volumes input file and volume_device_paths
      set_fact:
        volumes_combined: "{{ volumes_combined|default({}) | combine({ item.0: item.1 }) }}"
      loop: "{{ query('together', volume_device_path, volumes) }}"

调试输出:

ok: [node1] => {
    "volumes_combined": {
        "/dev/sanstorage/0987654321": {
            "format_opts": null,
            "fs_type": "xfs",
            "mount_opts": "noatime",
            "mount_path": "/data/fc100",
            "storage_protocol": "fc",
            "vol_desc": "ansible volume for fc",
            "vol_name": "data100",
            "vol_size": 100000,
            "vol_suffix": "data100"
        },
        "/dev/sanstorage/1234567890": {
            "format_opts": null,
            "fs_type": "ext4",
            "mount_opts": "noatime",
            "mount_path": "/data/fc111",
            "storage_protocol": "fc",
            "voldesc": "ansible volume for fc",
            "volname": "data111",
            "volsize": 100000,
            "volsuffix": "data111"
        }
    }
}

谢谢!

1 个答案:

答案 0 :(得分:1)

问:"'volume_device_path' 列表值不嵌套在字典中"

  volumes_combined: "{{ volumes_combined|default({})|
                        combine({ item.0: item.1 }) }}"

A:需要进行一些修复

  • default([]) ... 变量 volumes_combined 是一个列表。
  • + 而不是 combine ... 添加一个项目到列表中。
  • [item.1|combine({'volume_device_path': item.0 })] ... 组合字典
  volumes_combined: "{{ volumes_combined|default([]) +
                        [item.1|combine({'volume_device_path': item.0 })] }}"