如何在 Ansible 循环中迭代不同长度的嵌套列表?

问题描述 投票:0回答:1

当尝试从 ansible 调试输出迭代此数据时,我只能迭代索引的第一个元素(为零 0)。我想迭代所有元素。 下面是来自 ansible 的输出示例,输出下方是我正在处理的 ansible 任务。

{
    "ansible_loop_var": "item",
    "item": {
        "key": "SwitchName",
        "value": [
            {
                "key": "1",
                "value": {
                    "cdp": {
                        "address": "192.168.16.225",
                        "deviceId": "482ef8200",
                        "portId": "Port C9300",
                        "sourcePort": "1"
                    },
                    "lldp": {
                        "managementAddress": "192.168.16.225",
                        "portId": "2",
                        "sourcePort": "1",
                        "systemName": "**LAB-Remote-1**"
                    }
                }
            },
            {
                "key": "4",
                "value": {
                    "cdp": {
                        "address": "10.22.89.16",
                        "deviceId": "12efa487400",
                        "portId": "Port 1",
                        "sourcePort": "4"
                    },
                    "lldp": {
                        "managementAddress": "10.22.89.16",
                        "portId": "1",
                        "sourcePort": "4",
                        "systemName": "C9300"
                    }
                }
            },
            {
                "key": "5",
                "value": {
                    "cdp": {
                        "address": "172.16.254.56",
                        "deviceId": "f43fc84af710",
                        "portId": "Port 1",
                        "sourcePort": "5"
                    },
                    "lldp": {
                        "managementAddress": "172.16.254.56",
                        "portId": "1",
                        "sourcePort": "5"
                    }
                }
            },
            {
                "key": "6",
                "value": {
                    "cdp": {
                        "address": "10.254.255.216",
                        "deviceId": "4835187cff00",
                        "portId": "Port 50",
                        "sourcePort": "6"
                    },
                    "lldp": {
                        "managementAddress": "10.254.255.216",
                        "portId": "50",
                        "sourcePort": "6"
                    }
                }
            }
        ]
    }
}

这是我正在使用的代码,在我的任务示例中,我正在打印列表的 0 索引,并且它返回数据正常,但我想打印列表的所有元素以及列表大小会有所不同。

- name: print out lldp port data
  debug:
    var: item.value.0.value.lldp.systemName
  loop: "{{cdp_lldp_by_switch | dict2items}}"

此任务将打印出 LAB-Remote-1,因为它在列表中为 0。

python json loops dictionary ansible
1个回答
0
投票

一个最小的示例手册

---
- hosts: localhost
  become: false
  gather_facts: false

  vars:

    items:
      key: SwitchName
      value: [
            {
                "key": "1",
                "value": {
                    "cdp": {
                        "address": "192.168.16.225",
                        "deviceId": "482ef8200",
                        "portId": "Port C9300",
                        "sourcePort": "1"
                    },
                    "lldp": {
                        "managementAddress": "192.168.16.225",
                        "portId": "2",
                        "sourcePort": "1",
                        "systemName": "**LAB-Remote-1**"
                    }
                }
            },
            {
                "key": "4",
                "value": {
                    "cdp": {
                        "address": "10.22.89.16",
                        "deviceId": "12efa487400",
                        "portId": "Port 1",
                        "sourcePort": "4"
                    },
                    "lldp": {
                        "managementAddress": "10.22.89.16",
                        "portId": "1",
                        "sourcePort": "4",
                        "systemName": "C9300"
                    }
                }
            },
            {
                "key": "5",
                "value": {
                    "cdp": {
                        "address": "172.16.254.56",
                        "deviceId": "f43fc84af710",
                        "portId": "Port 1",
                        "sourcePort": "5"
                    },
                    "lldp": {
                        "managementAddress": "172.16.254.56",
                        "portId": "1",
                        "sourcePort": "5"
                    }
                }
            },
            {
                "key": "6",
                "value": {
                    "cdp": {
                        "address": "10.254.255.216",
                        "deviceId": "4835187cff00",
                        "portId": "Port 50",
                        "sourcePort": "6"
                    },
                    "lldp": {
                        "managementAddress": "10.254.255.216",
                        "portId": "50",
                        "sourcePort": "6"
                    }
                }
            }
        ]

  tasks:

    - debug:
        msg: "{{ item.value }}"
      loop: "{{ items.value }}"
      loop_control:
        label: "{{ item.key }}"

将产生

的输出
TASK [debug] **************************
ok: [localhost] => (item=1) =>
  msg:
    cdp:
      address: 192.168.16.225
      deviceId: 482ef8200
      portId: Port C9300
      sourcePort: '1'
    lldp:
      managementAddress: 192.168.16.225
      portId: '2'
      sourcePort: '1'
      systemName: '**LAB-Remote-1**'
ok: [localhost] => (item=4) =>
  msg:
    cdp:
      address: 10.22.89.16
      deviceId: 12efa487400
      portId: Port 1
      sourcePort: '4'
    lldp:
      managementAddress: 10.22.89.16
      portId: '1'
      sourcePort: '4'
      systemName: C9300
ok: [localhost] => (item=5) =>
  msg:
    cdp:
      address: 172.16.254.56
      deviceId: f43fc84af710
      portId: Port 1
      sourcePort: '5'
    lldp:
      managementAddress: 172.16.254.56
      portId: '1'
      sourcePort: '5'
ok: [localhost] => (item=6) =>
  msg:
    cdp:
      address: 10.254.255.216
      deviceId: 4835187cff00
      portId: Port 50
      sourcePort: '6'
    lldp:
      managementAddress: 10.254.255.216
      portId: '50'
      sourcePort: '6'
© www.soinside.com 2019 - 2024. All rights reserved.