是否可以合并 anisible group_vars,如果可以的话你能帮我解决一下吗
Is it possible to merge anisible group_vars, if possible can you hlep me how to solve
实际上我有 group_vars,我需要合并那些变量,你能帮我吗。
group_vars/eu/main.yaml
mgmt_routes:
Tok:
- ip: 172.22.203.253
netmask: 255.255.255.255
group_vars/all/main.yaml
mgmt_routes:
all:
- ip: 172.18.0.70
netmask: 255.255.255.255
- ip: 172.18.3.50
netmask: 255.255.255.255
如何整合这些变量以及如何在 playbook 中调用合并的变量
让我们简化数据
$ cat group_vars/all/main.yml
mgmt_routes:
all: ALL ROUTES
$ cat group_vars/eu/main.yml
mgmt_routes:
Tok: TOK ROUTES
有了库存文件
[eu]
test_01
[us]
test_02
以下剧目
- hosts: test_01
tasks:
- debug:
var: mgmt_routes
- hosts: test_02
tasks:
- debug:
var: mgmt_routes
- set_fact:
all_routes_list: "{{ hostvars|
json_query('*.mgmt_routes')|
unique }}"
- set_fact:
all_routes_dict: "{{ all_routes_dict|
default({})|
combine(item) }}"
loop: "{{ all_routes_list }}"
- debug:
var: all_routes_dict
给出(删节)
ok: [test_01] => {
"mgmt_routes": {
"Tok": "TOK ROUTES"
}
}
ok: [test_02] => {
"mgmt_routes": {
"all": "ALL ROUTES"
}
}
ok: [test_02] => {
"all_routes_dict": {
"Tok": "TOK ROUTES",
"all": "ALL ROUTES"
}
}
这是您要找的吗?
感谢您回复我的 post,但实际上我必须处理 group_vars 而不是主机变量。
实际上我有 group_vars,我需要合并那些变量,你能帮我吗。
group_vars/eu/main.yaml
mgmt_routes:
Tok:
- ip: 172.22.203.253
netmask: 255.255.255.255
group_vars/all/main.yaml
mgmt_routes:
all:
- ip: 172.18.0.70
netmask: 255.255.255.255
- ip: 172.18.3.50
netmask: 255.255.255.255
如何整合这些变量以及如何在 playbook 中调用合并的变量
让我们简化数据
$ cat group_vars/all/main.yml
mgmt_routes:
all: ALL ROUTES
$ cat group_vars/eu/main.yml
mgmt_routes:
Tok: TOK ROUTES
有了库存文件
[eu]
test_01
[us]
test_02
以下剧目
- hosts: test_01
tasks:
- debug:
var: mgmt_routes
- hosts: test_02
tasks:
- debug:
var: mgmt_routes
- set_fact:
all_routes_list: "{{ hostvars|
json_query('*.mgmt_routes')|
unique }}"
- set_fact:
all_routes_dict: "{{ all_routes_dict|
default({})|
combine(item) }}"
loop: "{{ all_routes_list }}"
- debug:
var: all_routes_dict
给出(删节)
ok: [test_01] => {
"mgmt_routes": {
"Tok": "TOK ROUTES"
}
}
ok: [test_02] => {
"mgmt_routes": {
"all": "ALL ROUTES"
}
}
ok: [test_02] => {
"all_routes_dict": {
"Tok": "TOK ROUTES",
"all": "ALL ROUTES"
}
}
这是您要找的吗?
感谢您回复我的 post,但实际上我必须处理 group_vars 而不是主机变量。