简体   繁体   English

剧本中的 ansible 循环

[英]ansible loop in playbook

I am playing with nxos module to configure Cisco switch using ansible and had one question related loop handling.我正在使用nxos模块来使用nxos配置 Cisco 交换机,并且有一个与循环处理相关的问题。

https://docs.ansible.com/ansible/latest/modules/list_of_network_modules.html#nxos https://docs.ansible.com/ansible/latest/modules/list_of_network_modules.html#nxos

I have this task where i need to configure channel-group on all interface but adding 1 in interface number so if interface is E1/12 channel-group will be 112我有这个任务,我需要在所有接口上配置通道组,但在接口号中添加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

I have currently this snippet in ansible to doing all interface related task我目前有这个片段可以执行所有与界面相关的任务

- name: default interfaces
      nxos_interface: interface={{ item }} description='Configured by Ansible' mode=layer2
      with_items:
        - Ethernet1/11
        - Ethernet1/12

is there anyway i can some kind of loop in above code to iterate in variables?无论如何我可以在上面的代码中使用某种循环来迭代变量吗?

Are you looking for something like below:您是否正在寻找以下内容:

---
- name: test
  hosts: localhost
  tasks:
    - name:  default interfaces
      debug:
        msg: "1{{ item.split('/')[1] }}"
      with_items:
        - Ethernet1/11
        - Ethernet1/12

Output输出

ok: [localhost] => (item=Ethernet1/11) => {
    "msg": "111"
}
ok: [localhost] => (item=Ethernet1/12) => {
    "msg": "112"
}

The "1" is fixed here这里的“1”是固定的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM