【问题标题】:Ansible: iterate over a list of dictionaries - loop vs. with_itemsAnsible:遍历字典列表 - 循环与 with_items
【发布时间】:2019-03-29 20:49:24
【问题描述】:

在尝试迭代字典列表时,使用 loop 与 with_items 时,我得到了不同的结果。

我尝试过使用 loop|dict2items(结构不是字典,它告诉我的也一样多。呵呵)并使用 flatten 过滤器循环。

这里是词典列表:

    "msg": [
        {
            "id": "id1", 
            "ip": "ip1", 
            "name": "name1"
        }, 
        {
            "id": "id2", 
            "ip": "ip2", 
            "name": "name2"
        }, 
        {
            "id": "id3", 
            "ip": "ip3", 
            "name": "name3"
        }, 
        {
            "id": "id4", 
            "ip": "ip4", 
            "name": "name4"
        }
    ]
}

这是剧本中的任务:

 - name: Add privateIp windows_instances to inventory
        add_host:
          name: "{{ item.ip }}"
          aws_name: "{{ item.name }}"
          groups: windows_instances
          aws_instanceid: "{{ item.id }}"
          ansible_user: "{{ windows_user }}"
          ansible_password: "{{ windows_password }}"
          ansible_port: 5985
          ansible_connection: winrm
          ansible_winrm_server_cert_validation: ignore
        loop:
          - "{{ list1 | flatten(levels=1) }}"

尝试运行上述代码时,我收到“列表对象没有属性”错误。我尝试了不同的展平级别都无济于事。

但是...

如果我简单地将上面的循环替换为:

with_items:
  - "{{ list1 }}"

一切都很完美。我在这里的 with_items > 循环翻译中遗漏了一些东西......

【问题讨论】:

    标签: loops ansible


    【解决方案1】:

    不要在你的列表前加上-

    在这里,你有一个 dicts 列表,所以你不需要扁平化。

    这个剧本:

    - hosts: localhost
      gather_facts: no
    
      vars:
        demo_list:
          - ip: 1.2.3.4
            id: 1
            name: demo1
          - ip: 2.2.3.4
            id: 2
            name: demo2
          - ip: 3.2.3.4
            id: 3
            name: demo3
    
      tasks:
    
        - name: the list
          debug:
            msg: "{{ demo_list }}"
    
        - name: unflattened list
          debug:
            msg: "{{ item.id }} {{ item.ip }} {{ item.name }}"
          loop:
            "{{ demo_list }}"
    
        - name: flattened list == unflattened list in this case
          debug:
            msg: "{{ item.id }} {{ item.ip }} {{ item.name }}"
          loop:
            "{{ demo_list | flatten(levels=1) }}"
    

    给出这个结果:

    PLAY [localhost] ***************************************************************************************
    
    TASK [the list] ****************************************************************************************
    ok: [localhost] => {
        "msg": [
            {
                "id": 1, 
                "ip": "1.2.3.4", 
                "name": "demo1"
            }, 
            {
                "id": 2, 
                "ip": "2.2.3.4", 
                "name": "demo2"
            }, 
            {
                "id": 3, 
                "ip": "3.2.3.4", 
                "name": "demo3"
            }
        ]
    }
    
    TASK [unflattened list] ********************************************************************************
    ok: [localhost] => (item=None) => {
        "msg": "1 1.2.3.4 demo1"
    }
    ok: [localhost] => (item=None) => {
        "msg": "2 2.2.3.4 demo2"
    }
    ok: [localhost] => (item=None) => {
        "msg": "3 3.2.3.4 demo3"
    }
    
    TASK [flattened list == unflattened list in this case] *************************************************
    ok: [localhost] => (item=None) => {
        "msg": "1 1.2.3.4 demo1"
    }
    ok: [localhost] => (item=None) => {
        "msg": "2 2.2.3.4 demo2"
    }
    ok: [localhost] => (item=None) => {
        "msg": "3 3.2.3.4 demo3"
    }
    
    PLAY RECAP *********************************************************************************************
    localhost                  : ok=3    changed=0    unreachable=0    failed=0
    

    【讨论】:

    • 哦,奇怪!我不知道......我认为 - 是循环项目的正确符号!你能解释一下区别/意义吗?
    • 这实际上没有用。产生以下错误:ERROR! Syntax Error while loading YAML. did not find expected key The offending line appears to be: "{{ list1 }}" "{{ list2 }}" ^ here We could be wrong, but this one looks like it might be an issue with missing quotes. Always quote template expression brackets when they start a value. For instance: with_items: - {{ foo }} Should be written as: with_items: - "{{ foo }}"
    • 什么不起作用?在我的示例中,我没有 list2...您可能会写类似 "{{ list1 }}" "{{ list2 }}" 而不是 "{{ list1 }} {{ list2 }}"
    • 嗯...写“{{ list1 }} {{ list2 }}”给出了以下错误:“FAILED!=> {“msg”:“传递给“循环”的数据无效,它需要一个列表,而不是这个:"
    猜你喜欢
    • 1970-01-01
    • 2021-06-11
    • 2021-02-04
    • 2020-11-04
    • 2014-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-31
    相关资源
    最近更新 更多