当尝试从 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。
一个最小的示例手册
---
- 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'