我对ansible非常陌生,并且在我的剧本中出现了变量继承/循环问题。该剧本用于为某些Cisco leafs配置vxlan vrfs和vxlan。
以下是变量:
username: admin
password: password
transport: cli
vrfmember: DMZ_TOOLS
vlan: 1000 ##starts here
vlanname: DMZ_TOOLS_01
vnsegment: 1000000
mcastgroup: 225.1.1.1
description: DMZ Tools Network 1
ipaddress: 10.1.1.1/26
vlan: 1001 ##switches here
vlanname: DMZ_TOOLS_02
vnsegment: 1000001
mcastgroup: 225.1.1.2
description: DMZ Tools Network 2
ipaddress: 10.1.1.2/26这是剧本:
---
- name: meme
hosts: all
connection: local
vars_files:
- /root/ansible/examples/playbooks/vars_shit.yml
tasks:
- name: vlan config (vrf edition)
nxos_command:
commands:
- conf t
- vlan {{ vlan }}
- name {{ vlanname }}
- vn-segment {{ vnsegment }}
provider: "{{ cli }}"
- name: vrf config
nxos_command:
commands:
- conf t
- vrf context {{ vlanname }}
- vni {{ vnsegment }}
- rd auto
- address-family ipv4 unicast
- route-target both auto
- route-target both auto evpn
provider: "{{ cli }}"
- name: int vlan
nxos_command:
commands:
- conf t
- interface {{ vlan }}
- description {{ description }}
- no shutdown
- vrf member {{ vrfmember }}
- no ip redirects
- ip forward
- no ipv6 redirects
provider: "{{ cli }}"
- name: int nve1
nxos_command:
commands:
- conf t
- member vni {{ vnsegment }}
provider: "{{ cli }}"
- name: router shit stoof
nxos_command:
commands:
- conf t
- router bgp 65490
- vrf {{ vrfmember }}
- address-family ipv4 unicast
- advertise 12vpn evpn
provider: "{{ cli }}"
- name: vlan config
nxos_command:
commands:
- conf t
- vlan {{ vlan }}
- name {{ vlanname }}
- vn-segment {{ vnsegment }}
provider: "{{ cli }}"
- name: interface config
nxos_command:
commands:
- conf t
- interface nve1
- member vni {{ vnsegment }}
- suppress-arp
- mcast-group {{ mcastgroup }}
provider: "{{ cli }}"
- name: evpn config
nxos_command:
commands:
- conf t
- evpn
- vni {{ vnsegment }} l2
- rd auto
- route-target import auto
- route-target export auto
provider: "{{ cli }}"
- name: int vlan config
nxos_command:
commands:
- conf t
- interface vlan {{ vlan }}
- description {{ description }}
- no shutdown
- mtu 9216
- vrf member {{ vrfmember }}
- no ip redirects
- ip address {{ ipaddress }}
- no ipv6 redirects
- fabric forwarding mode anycast-gateway
provider: "{{ cli }}"
我正在尝试传递第一组cisco变量,一旦它在"ipaddress“完成,它将切换到第二组变量。很抱歉格式化,但任何帮助将不胜感激!
发布于 2016-11-07 14:18:22
你可以创建这样的东西
username: admin
password: password
transport: cli
interface:
- vrfmember: DMZ_TOOLS
vlan: 1000 ##starts here
vlanname: DMZ_TOOLS_01
vnsegment: 1000000
mcastgroup: 225.1.1.1
description: DMZ Tools Network 1
ipaddress: 10.1.1.1/26
- vlan: 1001 ##switches here
vlanname: DMZ_TOOLS_02
vnsegment: 1000001
mcastgroup: 225.1.1.2
description: DMZ Tools Network 2
ipaddress: 10.1.1.2/26然后在上面循环一些类似的东西
with_items:
- '{{ interface }}'访问任务中的变量。
{{ item.vnsegment }}您可能希望使用interface的第一项执行所有三项任务,然后使用第二项执行任务,这样您就可以将任务放在名为do_stuff.yml的.yml文件中,然后将其包含在
- include: do_stuff.yml
with_items:
- '{{ interface }}'这将允许您为一组配置执行一组任务。为了更容易理解,如果使用do_stuff.yml方法展开ansible循环,将执行的任务如下:
- name: vlan config (vrf edition) // with interface[0]
- name: vrf config // with interface[0]
- name: int vlan // with interface[0]
- name: int nve1 // with interface[0]
- name: router shit stoof // with interface[0]
- name: vlan config // with interface[0]
- name: interface config // with interface[0]
- name: evpn config // with interface[0]
- name: int vlan config // with interface[0]
- name: vlan config (vrf edition) // with interface[1]
- name: vrf config // with interface[1]
- name: int vlan // with interface[1]
- name: int nve1 // with interface[1]
- name: router shit stoof // with interface[1]
- name: vlan config // with interface[1]
- name: interface config // with interface[1]
- name: evpn config // with interface[1]
- name: int vlan config // with interface[1]如果您不执行do_stuff.yml,而只是在任务中添加一个with_items,则展开循环如下所示
- name: vlan config (vrf edition) // with interface[0]
- name: vlan config (vrf edition) // with interface[1]
- name: vrf config // with interface[0]
- name: vrf config // with interface[1]https://stackoverflow.com/questions/40467187
复制相似问题