剧本中的ansible循环
ansible loop in playbook
我正在使用 nxos
模块来使用 ansible 配置 Cisco 交换机,并且有一个与循环处理相关的问题。
https://docs.ansible.com/ansible/latest/modules/list_of_network_modules.html#nxos
我有这个任务,我需要在所有接口上配置通道组,但在接口编号中添加 1
因此,如果接口是 E1/12
,通道组将是 112
interface Ethernet1/11
channel-group 111 mode active
interface Ethernet1/12
channel-group 112 mode active
interface Ethernet1/13
channel-group 113 mode active
interface Ethernet1/14
channel-group 114 mode active
interface Ethernet1/15
channel-group 115 mode active
我目前有这个片段可以执行所有与界面相关的任务
- name: default interfaces
nxos_interface: interface={{ item }} description='Configured by Ansible' mode=layer2
with_items:
- Ethernet1/11
- Ethernet1/12
无论如何我可以在上面的代码中使用某种循环来迭代变量吗?
您是否在寻找以下内容:
---
- name: test
hosts: localhost
tasks:
- name: default interfaces
debug:
msg: "1{{ item.split('/')[1] }}"
with_items:
- Ethernet1/11
- Ethernet1/12
输出
ok: [localhost] => (item=Ethernet1/11) => {
"msg": "111"
}
ok: [localhost] => (item=Ethernet1/12) => {
"msg": "112"
}
此处固定为“1”
我正在使用 nxos
模块来使用 ansible 配置 Cisco 交换机,并且有一个与循环处理相关的问题。
https://docs.ansible.com/ansible/latest/modules/list_of_network_modules.html#nxos
我有这个任务,我需要在所有接口上配置通道组,但在接口编号中添加 1
因此,如果接口是 E1/12
,通道组将是 112
interface Ethernet1/11
channel-group 111 mode active
interface Ethernet1/12
channel-group 112 mode active
interface Ethernet1/13
channel-group 113 mode active
interface Ethernet1/14
channel-group 114 mode active
interface Ethernet1/15
channel-group 115 mode active
我目前有这个片段可以执行所有与界面相关的任务
- name: default interfaces
nxos_interface: interface={{ item }} description='Configured by Ansible' mode=layer2
with_items:
- Ethernet1/11
- Ethernet1/12
无论如何我可以在上面的代码中使用某种循环来迭代变量吗?
您是否在寻找以下内容:
---
- name: test
hosts: localhost
tasks:
- name: default interfaces
debug:
msg: "1{{ item.split('/')[1] }}"
with_items:
- Ethernet1/11
- Ethernet1/12
输出
ok: [localhost] => (item=Ethernet1/11) => {
"msg": "111"
}
ok: [localhost] => (item=Ethernet1/12) => {
"msg": "112"
}
此处固定为“1”