我有一个像这样的vars文件(vars/apps.yml
):
apps:
app1:
some_var: hello
greeting:
dev: "{{apps[app_name].some_var}} world!"
prod: "{{apps[app_name].some_var}} planet earth!"
app2:
some_var: hi
greeting:
dev: "{{apps[app_name].some_var}} world!"
prod: "{{apps[app_name].some_var}} planet mars!"
当我使用var app_name=app1
执行我的Playbook时,出现recursive loop detected
错误。
如何在保持vars.yml
DRY的同时解决此问题?
答案 0 :(得分:0)
您可以尝试将app.some_var
和app.greeting
变量定义到不同的树,请参阅下文:
hosts
:
[test_group]
rhel-green app_name=app1
rhel-blue app_name=app2
变量文件group_vars/all/test_role.yml
:
apps:
app1:
some_var: hello
app2:
some_var: hi
app_greeting:
app1:
greeting:
dev: "{{apps[app_name].some_var}} world!"
prod: "{{apps[app_name].some_var}} planet earth!"
app2:
greeting:
dev: "{{apps[app_name].some_var}} world!"
prod: "{{apps[app_name].some_var}} planet mars!"
playbook角色test_role/tasks/main.yml
:
---
# tasks file for test_role
- debug:
var: app_greeting[app_name]
输出:
[root@ansible ILIAS]# ansible-playbook -i hosts recursive_vars.yml
PLAY [test_group] ***************************************************************************************************************************************************************************************************
TASK [Gathering Facts] **********************************************************************************************************************************************************************************************
ok: [rhel-blue]
ok: [rhel-green]
TASK [test_role : debug] ********************************************************************************************************************************************************************************************
ok: [rhel-green] => {
"app_greeting[app_name]": {
"greeting": {
"dev": "hello world!",
"prod": "hello planet earth!"
}
}
}
ok: [rhel-blue] => {
"app_greeting[app_name]": {
"greeting": {
"dev": "hi world!",
"prod": "hi planet mars!"
}
}
}
PLAY RECAP **********************************************************************************************************************************************************************************************************
rhel-blue : ok=2 changed=0 unreachable=0 failed=0
rhel-green : ok=2 changed=0 unreachable=0 failed=0
[root@ansible ILIAS]#