如何将播放中的主机动态设置为Ansible中vars_file中的变量



我有一个剧本,根据调查定义了一组变量,运行任务包括将变量写入文件。在第二个剧本中,我想根据文件中的变量设置主机。它为主机提供了一个未定义的变量。

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

这是我得到的错误:

错误!字段"hosts"的值无效,其中包含未定义的变量。错误为:"switch_group"未定义错误出现在"/tmp/awx_929_h3cfbasn/project/provision_evpn_beta.yml":第106行,第3列,但可能根据确切的语法问题,位于文件的其他位置。违规行似乎是:

  • 名称:"提供交换机";^这里

这就是我的变量文件的样子:

[{"customer_name":"TEST444"},{"data_center":"Richland"},{"evpn_id":"444"}]

为了概括我要做的事情,我需要根据原始调查中以前的输入将主持人设置为不同的组。

为了增加更多的色彩,我的第一个游戏是在数据中心提供一组路由器,第二个游戏是交换机。我把它们分开了,因为很明显,你不想把相同的配置推送到路由器和交换机上。如果有更好的方法来隔离这两组(他们确实有不同的操作系统(,我会洗耳恭听。

您的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进行测试。

所以我能够通过将第二次播放设置为localhosts,然后使用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] }}"

如果我可以委托一个团队而不是一个主机,这对我来说会更好。或者如果我缺少一个不同的策略。

最新更新