ansible - 在 Anible 中使用计数器控制循环
问题描述
我正在尝试从不属于端口通道的交换机收集接口列表。我正在尝试使用计数器控制列表的位置,以便我可以指定键以便我可以看到值,但我没有成功。
但是当我使用计数器时,我得到列表对象没有属性。
- name: Check for port-channel config in the Ethernet
nxos_command:
commands: sh run int {{ item }} | include channel-group
register: output
with_items: "{{ ethernet }}"
- name: Trim down conf output
set_fact:
int_config: "{{ output['results'] }}"
- debug:
msg:
- "The counter is {{ counter | int }}"
- "Found interface with channgel-group, interface {{ item }}"
name: show the results
loop: "{{ int_config[counter] }}"
loop_control:
index_var: counter
解决方案
这是你要找的吗?
- debug:
msg: "Counter: {{ item|int }} Interface: {{ int_config[item] }}"
loop: "{{ range(0, int_config|length)|list }}"
此任务与以下数据
int_config:
- abc
- def
- ghi
给
"msg": "Counter: 0 Interface: abc"
"msg": "Counter: 1 Interface: def"
"msg": "Counter: 2 Interface: ghi"
推荐阅读
- c - 如何获取打开文件的结构文件?
- excel - Excel INDIRECT 函数 - 动态链接不起作用
- postgresql - To_Date() 扫描每个分区上的索引。我该如何优化它还是可以(因为它不会慢很多)或切换到 pg11 解决它?
- swift - tableView中不同产品的多个INApp购买请求
- sql - 在同一列中查找 Datediff
- amazon-web-services - 是否可以向 AWS IoT Analytics 中的管道添加多个通道输入?
- apache-spark - 动态生成用于合并的 spark Sql selectExpr 语句
- java - 如何在使用 java 8 或更高版本编译的 java 7 上运行 Jar
- javascript - 如何访问 li 文本以使用 javascript 创建新列表
- php - 无论如何我可以在单击按钮时插入一条 SQL 语句