I am trying to execute the following task in Airflow:
time_zone = "America/New_York"
t1= BashOperator(
task_id='example_task',
bash_command=('script.sh --arg1 hello '
f'--arg2 {{ execution_date + timedelta(days=1).astimezone(pytz.timezone({time_zone})) }}'),
dag=dag)
The problem I have is with the bash_command. I am trying to execute python code and use a macro inside the bash_command, but the script above yields airflow.exceptions.AirflowException: Bash command failed.
My question is: Is what I am trying to do possible and if so, how? I guess I am just scripting jinja in the wrong way... but I am not sure.
echo {{ execution_date + timedelta(days=1).astimezone(pytz.timezone({time_zone})) }}to see what that macro is actually resolving to.{{ execution_date + timedelta(days=1).astimezone(pytz.timezone({time_zone})) }}runs fine inBashOperator, whereexecution_dateis an airflow macro and the rest is python code?{{{{ execution_date + timedelta(days=1).astimezone(pytz.timezone('{time_zone}')) }}}}, doing yourstringvar.format(time_zone=time_zone), and then jinja should be able to resolve the execution_date from there. You might also want to changetimedeltatomacros.timedeltaif you get atimedelta is undefinederror.