What to do when Airflow BashOperator fails with TemplateNotFound error

This article is a part of my "100 data engineering tutorials in 100 days" challenge. (61/100)

Airflow has an annoying error that causes a failure when we try to use the BashOperator like this:

1
2
3
4
operator = BashOperator(
    task_id='operator_id',
    bash_command='some_bash_script.sh',
    dag=dag)

What is wrong? For some reason, it tries to apply the template variables to the “bash_command” parameter even if we don’t use them. Consequently, the template loader fails with the TemplateNotFound error because it cannot find any template variables in the given string.

Of course, in this case, it is the correct behavior because we don’t use any template variables. Unfortunately, it is not the behavior we want.

To make Airflow ignore the missing template and just use the given string as the file name (as it should do anyway), we have to trick the template parsing code by adding a single space at the end of the parameter:

1
2
3
4
operator = BashOperator(
    task_id='operator_id',
    bash_command='some_bash_script.sh ',
    dag=dag)

Subscribe to the newsletter and join the free email course.


Remember to share on social media!
If you like this text, please share it on Facebook/Twitter/LinkedIn/Reddit or other social media.

If you want to contact me, send me a message on LinkedIn or Twitter.

Would you like to have a call and talk? Please schedule a meeting using this link.


Bartosz Mikulski
Bartosz Mikulski * MLOps Engineer / data engineer * conference speaker * co-founder of Software Craft Poznan & Poznan Scala User Group

Subscribe to the newsletter and get access to my free email course on building trustworthy data pipelines.