Ansible 使用Anible中的计数器控制循环
我试图从一个交换机收集一个接口列表,这些接口不是端口通道的一部分。我试图使用计数器控制列表的位置,这样我就可以指定键,这样我就可以看到值,但我没有成功地这样做 但当我使用计数器时,我得到的列表对象没有属性Ansible 使用Anible中的计数器控制循环,ansible,Ansible,我试图从一个交换机收集一个接口列表,这些接口不是端口通道的一部分。我试图使用计数器控制列表的位置,这样我就可以指定键,这样我就可以看到值,但我没有成功地这样做 但当我使用计数器时,我得到的列表对象没有属性 - name: Check for port-channel config in the Ethernet nxos_command: commands: sh run int {{ item }} | include channel-group register: outpu
- 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
给予
这就是你要找的吗
- debug:
msg: "Counter: {{ item|int }} Interface: {{ int_config[item] }}"
loop: "{{ range(0, int_config|length)|list }}"
此任务包含以下数据
int_config:
- abc
- def
- ghi
给予
我明白了我想做什么,见下文
- name: Check for port-channel config in the Ethernet
nxos_command:
commands: sh run int {{ item }} | include channel-group
register: output
with_items: "{{ ethernet }}"
- debug:
var=output
- name: Trim down conf output
set_fact:
int_config: "{{ output['results'] }}"
- name: Create list of int that will be removed
set_fact:
cleaned_ethernet: "{{ cleaned_ethernet | reject('search', item['item']) | list }}"
loop: "{{ int_config }}"
when: item['stdout'][0] != ''
- debug:
var=cleaned_ethernet
- name: Configure each Ethernet port not in a Po with MTU of 9216
nxos_interface:
name: "{{ item }}"
mtu: 9216
with_items: "{{ cleaned_ethernet }}"
我明白了我想做什么,见下文
- name: Check for port-channel config in the Ethernet
nxos_command:
commands: sh run int {{ item }} | include channel-group
register: output
with_items: "{{ ethernet }}"
- debug:
var=output
- name: Trim down conf output
set_fact:
int_config: "{{ output['results'] }}"
- name: Create list of int that will be removed
set_fact:
cleaned_ethernet: "{{ cleaned_ethernet | reject('search', item['item']) | list }}"
loop: "{{ int_config }}"
when: item['stdout'][0] != ''
- debug:
var=cleaned_ethernet
- name: Configure each Ethernet port not in a Po with MTU of 9216
nxos_interface:
name: "{{ item }}"
mtu: 9216
with_items: "{{ cleaned_ethernet }}"