Accessing the 'ds' variable in airflow

I assume you want to call one of the default variables built-in AirFlow ds - the execution date as YYYY-MM-DD

To call just ds, you can do:

EXEC_DATE = '{{ ds }}'

To call what you wanted - macros.ds_add:

EXEC_DATE = '{{ macros.ds_add(ds, 1) }}'

And load it this way:

T1 = BashOperator(\
        task_id='test_ds',
        bash_command='echo ' + EXEC_DATE
        dag=DAG)

In case you want to format it (like I had to), you can do:

EXEC_DATE = '{{ macros.ds_format(macros.ds_add(ds, 1), "%Y-%m-%d", "%Y%m%d") }}'

Short answer, ds and macros variables can only be accessed through template as they only exists during execution and not during python code parsing (when the dag is loaded by airflow).

The question is really similar to this one : execution_date in airflow: need to access as a variable and I try to explain the difference between the 2 steps and how to have the execution date in a variable in the last answer : https://stackoverflow.com/a/45725005/3756307

Tags:

Airflow