我对ansible很新,并且在我的剧本中遇到了变量继承/循环问题。该剧本用于为一些思科叶子配置vxlan vrfs和vxlan vlans。
以下是变量:
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”,它将切换到第二组变量。 抱歉格式化,但任何帮助将不胜感激!
答案 0 :(得分:0)
你可以创建像这样的东西
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
的第一项然后使用第二项执行所有三项任务,这样您就可以在名为.yml
的{{1}}文件中执行任务,然后包含它
do_stuff.yml
这将允许您为一组配置执行一组任务。为了使它更容易理解,如果使用- include: do_stuff.yml
with_items:
- '{{ interface }}'
方法展开ansibles循环,将执行的任务将是:
do_stuff.yml
如果您不执行do_stuff.yml,只需在任务中添加- 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]
,则展开的循环将如下所示
with_items