【问题标题】:Looping over list of dictionaries over a list of dictionaries in a nested loop in ansible playbook在ansible playbook的嵌套循环中遍历字典列表
【发布时间】:2021-02-04 18:30:43
【问题描述】:

我需要在嵌套循环中遍历另一组 dicts 列表的 dicts 列表,以从我得到的输出中读取所有值。我是 ansible 的新手,也尝试过 with_items、loop、with_subelements 和 with_nested。没有什么能满足我从输出中读取所有值的需要。有人可以阐明我们如何进行嵌套循环吗?非常感谢您的帮助。

Outer-List: [    
  { 
     Key1: value1
     Key2: value2          
     Inner-List: [ 
        {
            Key31: value-A
            Key32: value-B
        }
        {
            Key33: value-C
            Key34: value-D
        }
     ],
  },              ====> OuterList[0] ends here
  {               ====> OuterList[1] starts here
     Key1: value1
     Key2: value2
     Inner-List: [       
        {
            Key31: value-E
            Key32: value-F
        }
        {
            Key33: value-G
            Key34: value-H
        }
     ],
   }              ====> OuterList[1] ends here
]                 ====> OuterList ends here

【问题讨论】:

    标签: dictionary ansible nested-loops


    【解决方案1】:

    问:循环遍历字典列表。

    A:给定一个有效的YAML 和有效的Ansible variable

    Outer_List:
      - Inner-List: 
          - Key31: value-A
            Key32: value-B
          - Key33: value-C
            Key34: value-D
        Key1: value1
        Key2: value2
      - Inner-List: 
          - Key31: value-E
            Key32: value-F
          - Key33: value-G
            Key34: value-H
        Key1: value1
        Key2: value2
    

    使用subelements。比如下面的任务

        - debug:
            msg: "{{ item.0.Key1 }} {{ item.0.Key2 }} {{ item.1 }}"
          with_subelements:
            - "{{ Outer_List }}"
            - Inner-List
    

    给予(删节)

      msg: 'value1 value2 {''Key31'': ''value-A'', ''Key32'': ''value-B''}'
      msg: 'value1 value2 {''Key33'': ''value-C'', ''Key34'': ''value-D''}'
      msg: 'value1 value2 {''Key31'': ''value-E'', ''Key32'': ''value-F''}'
      msg: 'value1 value2 {''Key33'': ''value-G'', ''Key34'': ''value-H''}'
    

    如果需要,下面是有效的JSON

    {
    "Outer_List":
        [
            {
                "Inner-List": [
                    {
                        "Key31": "value-A",
                        "Key32": "value-B"
                    },
                    {
                        "Key33": "value-C",
                        "Key34": "value-D"
                    }
                ],
                "Key1": "value1",
                "Key2": "value2"
            },
            {
                "Inner-List": [
                    {
                        "Key31": "value-E",
                        "Key32": "value-F"
                    },
                    {
                        "Key33": "value-G",
                        "Key34": "value-H"
                    }
                ],
                "Key1": "value1",
                "Key2": "value2"
            }
        ]
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-22
      • 1970-01-01
      • 2014-05-11
      • 1970-01-01
      • 2021-06-11
      相关资源
      最近更新 更多