ansible - 循环通过 ansible cisco.ios.ios_facts gather_network_resources: l2_interfaces 返回值?
问题描述
我想要做的是检查 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}
。
我很茫然,如果有人可以帮助或指导这个新手,将不胜感激!
解决方案
推荐阅读
- excel - 一个单元格等于 IF 语句中存在的多个数字,excel
- string - 禁用特定列的自动增量
- c# - 将结构映射注册表添加到 Microsoft.Azure.Functions.Extensions.DependencyInjection
- google-apps-script - 每 5 秒一次打开一长串 URL 的脚本,然后打开下一个 URL
- html - 如何在 Worker 中捕获 DataCloneError?
- laravel - Laravel 验证日期字段之前:another_date_field 加上一些句点
- python - Django ImageField 上传在本地工作,但在生产中不工作
- python - 如何从 PNG 为 Tensorflow 2 中的每个像素分类创建 One-hot 编码矩阵
- sql - 为什么我的脚本在某个情况下返回多行?
- powershell - ThreadJob:不能从 BeginProcessing、... EndProcessing 方法的覆盖范围之外调用 WriteObject 和 WriteError?