如何在 Ansible 中使用带变量的默认值? [英] How can I use default with variable in Ansible?

查看:37
本文介绍了如何在 Ansible 中使用带变量的默认值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道默认情况下我可以使用简单的硬编码字符串,但我正在尝试这样做:

I know that I can use a simple hardcoded string in default but I am trying to do this:

myvar: "{{ lookup('env','var1') | default("{{var2}}",true) }}"

但是它将它添加为一个字符串而不是评估它.

But it adds that as a string instead of evaluating it.

推荐答案

一旦你用 {{ 打开一个 Jinja2 表达式你就不需要再次打开它(特别是引用),你可以参考到变量的名称:

Once you opened a Jinja2 expression with {{ you don't need to open it again (especially quoted) and you can refer to the variables by their names:

myvar: "{{ lookup('env','var1') | default(var2, true) }}"

这篇关于如何在 Ansible 中使用带变量的默认值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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