在 Ansible Jinja2 模板中使用默认过滤器中的变量

问题描述:

在 Ansible Jinja2 模板中,我试图设置一个默认"值,其中也有一个变量,但它打印出文字而不是插入它.

Inside a an Ansible Jinja2 template I'm trying to set a "default" value which also has a variable in it but it's printing out the literal rather than interpolating it.

例如:

homedir = {{ hostvars[inventory_hostname]['instances'][app_instance]['homedir'] | default("/home/{{ app_instance }}/airflow") }}

返回:

airflow_home = /home/{{ app_instance }}/airflow

如何引用app_instance变量?

Jinja2 表达式内部使用 Jinja2 语法.您应该将字符串连接到一个变量值:

Inside Jinja2 expression use Jinja2 syntax. You should concatenate strings to a variable value:

homedir = {{ hostvars[inventory_hostname]['instances'][app_instance]['homedir'] | default("/home/" + app_instance + "/airflow") }}