You can access these variables with os.environ["ENV VAR NAME"]
(make sure to import os
). For example:
import os
# ... other imports ...
dag = DAG(
dag_id="demo",
default_args=default_args,
schedule_interval="0 0 * * *",
)
def print_env_var():
print(os.environ["AIRFLOW_CTX_DAG_ID"])
print_context = PythonOperator(
task_id="print_env",
python_callable=print_env_var,
dag=dag,
)
However, the common way to access such variables in a task is by providing the task context by setting provide_context=True
in your operator.
For example:
dag = DAG(
dag_id="demo",
default_args=default_args,
schedule_interval="0 0 * * *",
)
def print_context(**context):
print(context)
print_context = PythonOperator(
task_id="print_context",
python_callable=print_context,
provide_context=True, # <====
dag=dag,
)
The context
variable will contain a number of variables containing information about the task context, including the ones in your question:
# {
# 'END_DATE': '2019-01-01',
# 'conf': <module 'airflow.configuration' from '/opt/conda/lib/python3.6/site-packages/airflow/configuration.py'>,
# 'dag': <DAG: context_demo>,
# 'dag_run': None,
# 'ds': '2019-01-01',
# 'ds_nodash': '20190101',
# 'end_date': '2019-01-01',
# 'execution_date': <Pendulum [2019-01-01T00:00:00+00:00]>,
# 'inlets': [],
# 'latest_date': '2019-01-01',
# 'macros': <module 'airflow.macros' from '/opt/conda/lib/python3.6/site-packages/airflow/macros/__init__.py'>,
# 'next_ds': '2019-01-02',
# 'next_ds_nodash': '20190102',
# 'next_execution_date': datetime.datetime(2019, 1, 2, 0, 0, tzinfo=<TimezoneInfo [UTC, GMT, +00:00:00, STD]>),
# 'outlets': [],
# 'params': {},
# 'prev_ds': '2018-12-31',
# 'prev_ds_nodash': '20181231',
# 'prev_execution_date': datetime.datetime(2018, 12, 31, 0, 0, tzinfo=<TimezoneInfo [UTC, GMT, +00:00:00, STD]>),
# 'run_id': None,
# 'tables': None,
# 'task': <Task(PythonOperator): print_exec_date>,
# 'task_instance': <TaskInstance: context_demo.print_exec_date 2019-01-01T00:00:00+00:00 [None]>,
# 'task_instance_key_str': 'context_demo__print_exec_date__20190101',
# 'templates_dict': None,
# 'test_mode': True,
# 'ti': <TaskInstance: context_demo.print_exec_date 2019-01-01T00:00:00+00:00 [None]>,
# 'tomorrow_ds': '2019-01-02',
# 'tomorrow_ds_nodash': '20190102',
# 'ts': '2019-01-01T00:00:00+00:00',
# 'ts_nodash': '20190101T000000',
# 'ts_nodash_with_tz': '20190101T000000+0000',
# 'var': {'json': None, 'value': None},
# 'yesterday_ds': '2018-12-31',
# 'yesterday_ds_nodash': '20181231'
# }
I explain how to handle the task context in a bit more detail in this blog post (see "3. Passing context to tasks").