Airflow. Run second task only if the first task's result is true

Viewed 719

I have two tasks in an Airflow DAG like below.

def check_condition(**kwargs):
    # do something
    return True # or return False
 
task1 = PythonOperator(
    task_id='condition_task',
    python_callable=check_condition,
    provide_context=True,
    dag=dag
)

task2 = DummyOperator(
    task_id='following_task',
    dag=dag
)

What should I do to make
task 2 invoked only when the return value of task 1 is True?

1 Answers

You just need to do :

task1 >> task2

Operators have trigger_rule argument which sets the condition when to run. The default value is all_success thus no need to specifically mention it. More information about trigger rules can be found here

The issue here is what do you expect the status of task1 to be when you do return False. Do you expect task1 to fail or success? If you expect it to fail then you need to replace return False with raise Exception() If you expect it to be success then you will need task2 to pull the return value from task1 (via Xcom) and then handle the value. For example:

def following(**kwargs):
    ti = kwargs['ti']
    pulled_value = ti.xcom_pull(task_ids='task1')
    if bool(pulled_value) = True:
        do_something
    else
        do_something_else

task2 = PythonOperator(
    task_id='following_task',
    python_callable=following,
    provide_context=True,
    dag=dag
)
Related