Ansible 变量循环
Ansible Variable Looping
我是 ansible 的新手,我的剧本中的变量 inheritance/looping 有问题。该剧本用于为某些 Cisco 叶子配置 vxlan vrfs 和 vxlan vlan。
变量如下:
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" 处完成,它将切换到第二组变量。
抱歉格式问题,如有任何帮助,我们将不胜感激!
你可以创建这样的东西
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
方法展开 ansibles 循环,将执行的任务将是:
- 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]
我是 ansible 的新手,我的剧本中的变量 inheritance/looping 有问题。该剧本用于为某些 Cisco 叶子配置 vxlan vrfs 和 vxlan vlan。
变量如下:
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" 处完成,它将切换到第二组变量。 抱歉格式问题,如有任何帮助,我们将不胜感激!
你可以创建这样的东西
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
方法展开 ansibles 循环,将执行的任务将是:
- 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]