Ansible: iterate over a list of dictionaries - loop vs. with_items

南楼画角 提交于 2020-07-09 05:13:01

问题


I'm getting different results when using loop vs with_items when trying to iterate over a list of dictionaries.

I've tried using loop|dict2items (the structure isn't a dictionary, & it tells me as much. heh) and loop with the flatten filter.

Here is the list of dictionaries:

    "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"
        }
    ]
}

Here is the task in the playbook:

 - 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) }}"

When attempting to run the above code, I get the "list object has no attribute" error. I've tried different flatten levels to no avail.

HOWEVER...

If I simply replace the loop above with:

with_items:
  - "{{ list1 }}"

Everything works perfectly. I'm missing something in the with_items > loop translation here...


回答1:


Don't put a - before your list.

And here, you have a list of dicts, so you don't need to flatten neither.

This playbook:

- 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) }}"

gives this result:

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


来源:https://stackoverflow.com/questions/55425311/ansible-iterate-over-a-list-of-dictionaries-loop-vs-with-items

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!