anjum
anjum

Reputation: 43

How to use Default variables in airflow

How can we use Default variables in airflow which are showing in below link https://airflow.apache.org/code.html#default-variables

I have used this in my code as shown below:

def decide_which_task():

    if {{ dag_run.task_id }}  is  "Move_file":    
            return "move_file"    
        else:    
            return "push_to_db"

But i am getting error in log file as

NameError: global name 'dag_run' is not defined

Upvotes: 4

Views: 7118

Answers (1)

tobi6
tobi6

Reputation: 8239

The annotation you are trying to use is the annotation for Jinja templating within strings.

To use the same variables in a task, you need to:

  • use the parameter provide_context=True in the PythonOperator
  • change the function signature to def decide_which_task(**context):
  • then access the variable out of the context, e.g. mytask = context['task_id']

Code:

def decide_which_task(**context):

if context['task_id']  is  "Move_file":    
        return "move_file"    
    else:    
        return "push_to_db"

Upvotes: 3

Related Questions