我想要做的是检查 ios_facts gather_network_resources l2_interfaces 返回的每个接口,并仅为在“访问”下定义了某些 VLAN 的接口运行命令。例如,如果 vlan 为 2,我想在该接口上运行命令。
这是我从调试中看到的,显示了从我正在使用的 Cisco 测试交换机返回的数据结构:
"result": {
"ansible_facts": {
"ansible_network_resources": {
"l2_interfaces": [
{
"name": "GigabitEthernet0/0"
},
{
"access": {
"vlan": 2
},
"name": "GigabitEthernet1/0/1"
},
{
"name": "GigabitEthernet1/1/1",
"trunk": {
"allowed_vlans": [
"3",
"4",
"5"
],
"native_vlan": 7
}
},
{
"access": {
"vlan": 4
},
"name": "GigabitEthernet1/0/23"
}
]
}
}
}
作为 ansible 的新手,我很难弄清楚如何遍历这个并在为 VLAN 2 而不是其他 VLAN 配置的接口上运行所需的命令。到目前为止,这是我的剧本:
---
- name: Test IOS Facts
hosts: switch1
tasks:
- name: IOS Facts
ios_facts:
gather_network_resources: l2_interfaces
register: result
- debug: var=result
- name: Test Loop to Find Desired Ports
ios_config:
lines:
- description VLAN 2
parents: interface {{ item.value.name }}
register: result
loop: "{{ ansible_network_resources | dict2items }}"
when:
- item.value.access is defined
- item.value.access | length > 0
- item.value.access.vlan[0] is 2
- debug: var=result
我得到的回报是 "fatal: [switch1]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'list object' has no attribute 'name'\nThe error appears to be in <path>: line x column y, but may\nbe elswhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n - debug: var=result\n ^h here\n\nThere appears to be both 'k=v' shorthand syntax and YAML in this task. Only one syntax may be used.\n}
。
我很茫然,如果有人可以帮助或指导这个新手,将不胜感激!