0
0
AirflowHow-ToBeginner · 3 min read

How to Use Variables in DAG in Airflow: Simple Guide

In Airflow, you can use Variable.get() to access variables inside a DAG. Define variables in the Airflow UI or CLI, then retrieve them in your DAG code to make your workflows dynamic and configurable.
📐

Syntax

Use Variable.get("variable_name", default_var=None) to fetch a variable's value inside your DAG. The variable_name is the key you set in Airflow's variables. The optional default_var is returned if the variable is not found.

This allows your DAG to use external values without hardcoding them.

python
from airflow.models import Variable

my_var = Variable.get("my_variable", default_var="default_value")
💻

Example

This example shows how to use an Airflow variable inside a DAG to pass a parameter to a Python task.

python
from airflow import DAG
from airflow.operators.python import PythonOperator
from airflow.models import Variable
from datetime import datetime

def print_variable(**kwargs):
    my_var = Variable.get("my_variable", default_var="No Value Set")
    print(f"The variable value is: {my_var}")

default_args = {
    'start_date': datetime(2024, 1, 1),
}

dag = DAG(
    'variable_example_dag',
    default_args=default_args,
    schedule_interval='@once',
    catchup=False
)

print_task = PythonOperator(
    task_id='print_variable_task',
    python_callable=print_variable,
    dag=dag
)
Output
The variable value is: No Value Set
⚠️

Common Pitfalls

  • Not setting the variable in Airflow UI or CLI before running the DAG causes Variable.get() to return None or raise an error if no default is provided.
  • Using variables inside DAG definition code that runs at parse time can cause errors if variables are missing. Use variables inside task functions instead.
  • For sensitive data, use Airflow Connections or Secrets Backend instead of Variables.
python
from airflow.models import Variable

# Wrong: Using variable at DAG parse time (can cause errors if variable missing)
my_var = Variable.get("missing_var")

# Right: Use inside task function

def task_func():
    my_var = Variable.get("missing_var", default_var="default")
    print(my_var)
📊

Quick Reference

Airflow Variable Usage Cheat Sheet:

  • Variable.get("key"): Get variable value by key.
  • Variable.get("key", default_var="value"): Get variable or default if missing.
  • Set variables via Airflow UI under Admin > Variables or CLI airflow variables set key value.
  • Use variables inside task functions, not at DAG parse time.
  • For secrets, prefer Connections or Secrets Backend.

Key Takeaways

Use Variable.get() to access Airflow variables inside task functions for dynamic DAGs.
Always set variables in Airflow UI or CLI before running DAGs to avoid errors.
Avoid using variables at DAG parse time to prevent failures during DAG loading.
Use default_var parameter to provide fallback values when variables are missing.
For sensitive information, use Airflow Connections or Secrets Backend instead of Variables.