'Is it possible to render a variable that has template syntax in jinja2?

I have a variable in my python code like:

template_var = "{{ a_macro('var') }}"

Is it possible to have that template_var rendered in my template? Doing the following prints it as-is:

{{ params.template_var}}


Solution 1:[1]

You didn't share code so I guess a_macro is a user defined macro that you created and what you are doing is something like:

op = BashOperator(
    task_id='my_task',
    bash_command="python script.py --key {{ params.template_var}}",
    params={'template_var': "{{ a_macro('var') }}"}
    )

This is not going to work. You can not pass Jinja macros with params. Templated values needs to be set directly in the templated field. In the above example bash_command is a templated field so you need to do:

op = BashOperator(
    task_id='my_task',
    bash_command="python script.py --key {{ a_macro('var') }}",
    )

The same goes for any other Airflow operator.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Elad Kalif