带有嵌套列表的Ansible set_fact字典

时间:2019-06-20 00:13:52

标签: ansible

我需要能够将其转换为

{     “ 529eacea00000003”:[

    [

        {

            "limitBwInMbps": 0,

            "limitIops": 0,

            "sdcId": "2e18899800000000",

            "sdcIp": "172.21.41.71"

        },

        {

            "limitBwInMbps": 0,

            "limitIops": 0,

            "sdcId": "2e18899900000001",

            "sdcIp": "172.21.41.70"

        },

    ]

],
"529eacea00000002": [

    [

        {

            "limitBwInMbps": 0,

            "limitIops": 0,

            "sdcId": "2e18899800000002",

            "sdcIp": "172.21.41.72"

        },

        {

            "limitBwInMbps": 0,

            "limitIops": 0,

            "sdcId": "2e18899900000003",

            "sdcIp": "172.21.41.73"

        },

    ]

]

}

对此:

529eacea00000003,2e18899900000000 529eacea00000003,2e18899900000001 529eacea00000002,2e18899900000002 529eacea00000003,2e18899900000003

我已经尝试过with_items等的每一种组合,但是都没有运气

2 个答案:

答案 0 :(得分:1)

从数据部分开始

],
"529eacea00000002": [

我假设“ ”是列表的2个元素。让我们将列表命名为 data1 。然后执行以下任务

- set_fact:
    data2: "{{ data1|
               map('dict2items')|
               list|
               json_query('[].[key,
                               value[0][0].sdcId,
                               key,
                               value[0][1].sdcId]') }}"
- debug:
    msg: "{{ data2|to_yaml }}"

给予

  msg: |-
    - [529eacea00000003, 2e18899800000000, 529eacea00000003, 2e18899900000001]
    - [529eacea00000002, 2e18899800000002, 529eacea00000002, 2e18899900000003]

答案 1 :(得分:0)

      vars:
        vol_list:
          529eace700000000:
            - { sdcId: 2e18b0ae00000005, sdcIp: INVALID }
          529eace800000001:
            - { sdcId: 2e18b0b100000006, sdcIp: 172.21.41.74 }
          529eace900000002:
            - { sdcId: 2e18b0b100000006, sdcIp: 172.21.41.74 }
            - { sdcId: 2e18899c00000004, sdcIp: 172.21.41.67 }
          529eacea00000003:
            - { sdcId: 2e18899800000000, sdcIp: 172.21.41.71 }
            - { sdcId: 2e18899900000001, sdcIp: 172.21.41.70 }
            - { sdcId: 2e18899b00000003, sdcIp: 172.21.41.69 }
            - { sdcId: 2e18899a00000002, sdcIp: 172.21.41.68 }
            - { sdcId: 2e18899c00000004, sdcIp: 172.21.41.67 }
            - { sdcId: 2e18b0b100000006, sdcIp: 172.21.41.74 }
      tasks:
      - debug: var=vol_list

      - set_fact:
          v2s: "{{v2s|default('')}}{% for i in range(0,(vol_list[item])| length) %}{% if i > 0%};{%endif%}{{item}},{{vol_list[item][i].sdcId}}{% if i == (vol_list[item])| length%};{%endif%}{% endfor %};"
        with_items: "{{vol_list}}"

      - set_fact:
          sdc2vol1: "{{ sdc2vol1|default([]) + [ { 'vol': item.split(',')[0], 'sdc': item.split(',')[1]} ] }}"
        with_items: "{{v2s.split(';')}}"
        when: "item | list | length > 0"

      - debug: var=sdc2vol1 

#Yields the following results:
# {
# "sdc2vol1": [
#     {
#         "sdc": "2e18b0b100000006",
#         "vol": "529eace800000001"
#     },
#     {
#         "sdc": "2e18899800000000",
#         "vol": "529eacea00000003"
#     },
#     {
#         "sdc": "2e18899900000001",
#         "vol": "529eacea00000003"
#     },
#     {
#         "sdc": "2e18899b00000003",
#         "vol": "529eacea00000003"
#     },
#     {
#         "sdc": "2e18899a00000002",
#         "vol": "529eacea00000003"
#     },
#     {
#         "sdc": "2e18899c00000004",
#         "vol": "529eacea00000003"
#     },
#     {
#         "sdc": "2e18b0b100000006",
#         "vol": "529eacea00000003"
#     },
#     {
#         "sdc": "2e18b0b100000006",
#         "vol": "529eace900000002"
#     },
#     {
#         "sdc": "2e18899c00000004",
#         "vol": "529eace900000002"
#     },
#     {
#         "sdc": "2e18b0ae00000005",
#         "vol": "529eace700000000"
#     }
# ]
#}