我有一个剧本,它定义了一组基于调查的变量,任务被运行,包括将变量写入文件。在第二部戏中,我想根据文件中的一个变量来设置主机。它为主机提供了一个未定义的变量。
---
- name: "Provision Routers"
connection: network_cli
gather_facts: false
hosts: '{{ DataCenter }}'
vars:
customer_name: "{{ CustomerName }}"
data_center: "{{ DataCenter }}"
evpn_id: "{{ EVPN_ID }}"
layer_3: "{{ LAYER_3 }}"
ip_address: "{{ BVI_IP }}"
prefix_mask: "{{ MASK }}"
tasks:
- name: "Config Router EVPN"
iosxr_config:
lines:
- evi {{evpn_id}}
- control-word-disable
- advertise-mac
parents: evpn
------------------I'm leaving a bunch of tasks out to--------------------------
------------------Make this easier to read-------------------------------------
- name: Make vars persistent
local_action:
module: copy
content:
- customer_name: "{{ customer_name }}"
- data_center: "{{ data_center }}"
- evpn_id: "{{ evpn_id }}"
dest: /etc/ansible/group_vars/evpn_vars.yml
mode: 0666
# 2nd play---------------------------------------------------------------------
- name: "Provision Switches"
hosts: '{{ switch_group }}'
vars_files:
- /etc/ansible/group_vars/evpn_vars.yml
tasks:
- name: Set switch_group to Commerce
set_fact:
switch_group: Commerce_Switches
when: data_center == 'Commerce'
- name: Set switch_group to Gumlog
set_fact:
switch_group: Gumlog_Switches
when: data_center == 'Gumlog'
- name: Set switch_group to Richland
set_fact:
switch_group: Richland_Switches
when: data_center == 'Richland'这是我得到的错误:
错误!字段“host”有一个无效的值,其中包含一个未定义的变量。错误是:“switch_group”未定义--错误出现在第3列的第106行,但可能在文件的其他地方,具体取决于语法问题。冒犯的界限似乎是:
这就是我的变量文件的样子:
{customer_name:"TEST444"},{"data_center":"Richland"},{"evpn_id":"444"}
为了概括我想要做的事情,我需要根据先前在原始调查中的输入,将主机设置成一个不同的组。
为了增加一些颜色,我的第一个播放是在一个数据中心提供一组路由器,第二个播放是交换机。我把它们分开了,因为很明显,您不想将相同的配置推送到路由器上。如果有更好的方法来隔离这两组人(他们有不同的操作系统),我会全神贯注。
发布于 2021-03-19 15:13:03
你的switch_group是假的。
[{"customer_name": "TEST444"}, {"data_center": "Richland"}, {"evpn_id": "444"}]它应该是有效的JSON。看见
$ ansible-inventory -i "8.8.4.4," --list all
{
"_meta": {
"hostvars": {}
},
"all": {
"children": [
"ungrouped"
]
},
"ungrouped": {
"hosts": [
"8.8.4.4"
]
}
}因此,通过ansible-inventory --list switch_group进行测试。
发布于 2021-03-22 19:43:54
因此,我能够通过将第二次播放设置为本地主机,然后使用delegate_to来更改主机来实现这一工作。但它并不像我想的那样优雅。
- name: Make vars persistent
local_action:
module: copy
content:
- customer_name: "{ customer_name }}"
- data_center: "{{ data_center }}"
- evpn_id: "{{ evpn_id }}"
dest: /etc/ansible/group_vars/evpn_vars.yml
mode: 0666
- name: "Provision Switches"
hosts: localhost
vars_files:
- /etc/ansible/group_vars/evpn_vars.yml
tasks:
- name: Set switch_group to Commerce
set_fact:
switch_group: Commerce_Switches
when: data_center == 'Commerce'
- name: Set switch_group to Gumlog
set_fact:
switch_group: Gumlog_Switches
when: data_center == 'Gumlog'
- name: Set switch_group to Richland
set_fact:
switch_group: Richland_Switches
when: data_center == 'Richland'
- name: Set port to layer 2
nxos_interface:
name: Ethernet1/2
description: 'Configured by Ansible'
mode: layer2
admin_state: up
delegate_to: "{{ groups[switch_group][0] }}"
- name: Test nxos
nxos_l2_interface:
name: Ethernet1/2
mode: trunk
trunk_vlans: 5-10
delegate_to: "{{ groups[switch_group][0] }}"如果我可以delegate_to一个组而不是一个主机,它会对我工作得更好。或者,如果有一个不同的策略,所有这些我都错过了。
https://stackoverflow.com/questions/66698598
复制相似问题