Can we create unique file name every time airflow dag run and access that file from all tasks? I tried creating global variable (output_filename) and appended timestamp to it. But when ever i access that file_name in tasks,every task generates different file name as it is calculating timestamp in every task. Below is sample code:
table_name = 'Test_ABC'
start_date = datetime.now()
cur_tmpstp = start_date.strftime('%Y_%m_%d')
output_filename = table_name + "_" + cur_tmpstp + ".csv"
S3_landing_path = "s3://abc/"
def clean_up():
if os.path.exists(output_filename):
os.remove(output_filename)
task_1 = BashOperator(
task_id='task_1',
bash_command="aws s3 cp %s %s/ " %(output_filename, S3_landing_path, ),
dag=dag)
task_2_cleanup = PythonOperator(
task_id='task_2_cleanup',
python_callable=clean_up,
dag=dag)
We have more tasks where we have to access output_filename. How can we access output_filename global variable in all tasks?