Ansible - 在模板中呈现嵌套变量 [英] Ansible - Rendering nested variables in a template

查看:27
本文介绍了Ansible - 在模板中呈现嵌套变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 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

为简单起见,我的角色的 tasks/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 密码.但是,目前我得到 fatal: [1.1.1.1]: FAILED!=>{"failed": 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆