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?
So if you want to define a default value for a variable you should set it in role/defaults/main.
Jinja2 filter is something we use to transform data held in variables. We apply filters by placing pipe symbol | after the variable followed by name of the filter. Filters can change the look and format of the source data, or even generate new data derived from the input values.
Ansible uses Jinja2 templating to enable dynamic expressions and access to variables and facts.
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") }}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With