Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Airflow dag and task decorator in 2.0: how to pass config params to task?

Tags:

airflow

I'm struggling to understand how to read DAG config parameters inside a task using Airflow 2.0 dag and task decorators.

Consider this simple DAG definition file:

from airflow.decorators import dag, task
from airflow.utils.dates import days_ago

@dag()
def lovely_dag():
   
    @task(start_date=days_ago(1))
    def task1():
       return 1

    something = task1()

my_dag = lovely_dag()

I can trigger the dag using the UI or the console and pass to it some (key,value) config, for example:

airflow dags trigger --conf '{"hello":"there"}' lovely_dag

How can I access {"hello":"there"} inside the task1 function?

My use case is I want to pass 2 parameters to dag and want task1 to see them.

like image 341
oprog Avatar asked Dec 14 '22 07:12

oprog


1 Answers

You can access the context as follows:

from airflow.operators.python import task, get_current_context

@task
def my_task():
    context = get_current_context()
    dag_run = context["dag_run"]
    dagrun_conf = dag_run.conf

where dagrun_conf will be the variable containing the DAG config parameters

Source: http://airflow.apache.org/docs/apache-airflow/2.0.0/concepts.html#accessing-current-context

like image 121
kaxil Avatar answered Jun 15 '23 16:06

kaxil