剧本中的ansible循环

问题描述 投票:1回答:1

我正在使用nxos模块使用ansible配置Cisco交换机,并且有一个问题涉及循环处理。

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

我有这个任务,我需要在所有接口上配置channel-group,但在接口号中添加1,所以如果接口是E1/12 channel-group将是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

我目前这个片段在ansible中做了所有与接口相关的任务

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

无论如何我可以在上面的代码中进行某种循环来迭代变量吗?

python loops ansible cisco
1个回答
1
投票

你在找下面的东西:

---
- 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”

© www.soinside.com 2019 - 2024. All rights reserved.