KristiLuna
KristiLuna

Reputation: 1903

Airflow 2.0.2 - Jinja2 top-level template error

I'm seeing the below error happen:

Traceback (most recent call last): File "/usr/local/lib/python3.8/site-packages/airflow/models/taskinstance.py", line 1138, in _run_raw_task self._prepare_and_execute_task_with_callbacks(context, task) File "/usr/local/lib/python3.8/site-packages/airflow/models/taskinstance.py", line 1275, in _prepare_and_execute_task_with_callbacks self.render_templates(context=context) File "/usr/local/lib/python3.8/site-packages/airflow/models/taskinstance.py", line 1779, in render_templates self.task.render_template_fields(context) File "/usr/local/lib/python3.8/site-packages/airflow/models/baseoperator.py", line 892, in render_template_fields self._do_render_template_fields(self, self.template_fields, context, jinja_env, set()) File "/usr/local/lib/python3.8/site-packages/airflow/models/baseoperator.py", line 905, in _do_render_template_fields rendered_content = self.render_template(content, context, jinja_env, seen_oids) File "/usr/local/lib/python3.8/site-packages/airflow/models/baseoperator.py", line 942, in render_template return jinja_env.from_string(content).render(**context) File "/usr/local/lib/python3.8/site-packages/jinja2/environment.py", line 1090, in render self.environment.handle_exception() File "/usr/local/lib/python3.8/site-packages/jinja2/environment.py", line 832, in handle_exception reraise(*rewrite_traceback_stack(source=source)) File "/usr/local/lib/python3.8/site-packages/jinja2/_compat.py", line 28, in reraise raise value.with_traceback(tb)

File "", line 1, in top-level template code jinja2.exceptions.UndefinedError: 'target_table' is undefined

when trying to run this task in Airflow:

TABLE_NAME = 'sales.il_sales'

t4 = SnowflakeOperator(
    task_id='my_test_query.sql',
    sql='sql/my_test_query.sql',
    params={
        'target_table': TABLE_NAME,
    },
    retries=0,
    pool='airflow')

this is strange because target_table is 'sales.il_sales'

Upvotes: 0

Views: 3985

Answers (1)

kaxil
kaxil

Reputation: 18844

In your sql/my_test_query.sql file if you are using {{ target_table }} it won't work. You need to use {{ params.target_table }}.

Example:

SELECT * from {{ params.target_table }}

Check the example in docs here: https://airflow.apache.org/docs/apache-airflow/stable/tutorial.html#templating-with-jinja

Upvotes: 1

Related Questions