Using variable in default filter in Ansible Jinja2

2019-02-20 03:43发布

问题:

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.

For example:

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

returns:

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

How to refer to the app_instance variable?

回答1:

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") }}


标签: ansible