Ansible-在模板中渲染嵌套变量 [英] Ansible - Rendering nested variables in a template
问题描述
我正在尝试使用ansible中的模板将一些嵌套变量呈现到文件中.
I'm trying to render some nested variables to a file using templating in ansible.
主治剧本如下:
---
- hosts: tag_Cluster_restore
vars:
pg_password:
qa: blah
staging: blahblah
production: blahblahblah
roles:
- role: psql_helper
为简单起见,我角色的task/main.yml看起来像这样:
For simplicity, my role's tasks/main.yml looks like this:
---
- debug: msg="Password is {{ pg_password.[env] }}"
我想在命令行上这样指定 env
变量:
And I would like to specify the env
variable on the command line like this:
ansible-playbook playbook.yml -e "env=qa"
...并让其呈现"qa"密码.但是,此刻我得到致命的代码:[1.1.1.1]:失败!=>{失败":true,"msg":"ERROR!模板字符串时模板错误:预期名称或数字"}
...and have it render the qa password. However, at the moment I get fatal: [1.1.1.1]: FAILED! => {"failed": true, "msg": "ERROR! template error while templating string: expected name or number"}
很明显,我在某处语法错误,或者我指定的内容不正确.有人可以帮忙吗?
Obviously I have the syntax wrong somewhere, Or I am specifying something incorrectly. Can anyone help?
请注意,我不想遍历 pg_password
变量-我在Google上搜索过的大多数内容都指出了如何执行此操作,我想向下钻取"到我的嵌套变量需要,如果实际上可行的话...
Note that I do not want to loop over the pg_password
variable - most stuff I've googled points out how to do this, I would like to "drill down" to the nested variable that I need, if this is in fact possible...
谢谢
推荐答案
您的调试任务在调用时不应具有.
.
Your debug task should not have the .
on invocation:
- debug: msg="Password is {{ pg_password[env] }}"
这只是意味着您从字典 pg_password
This simply means you are getting the key env
from the dictionary pg_password
这篇关于Ansible-在模板中渲染嵌套变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!