Airflow Templating
Templating lets you place small placeholders inside task settings. Airflow replaces each placeholder with a real value at run time, such as the date of the run. One DAG file then works for every day without any code changes.
The Form Letter Picture
A form letter says "Dear {name}, your order {number} ships today." A mail program swaps in each customer's name and number. Airflow templating works the same way. You write the letter once, and Airflow fills in the blanks for every run.
Template: "sales_{{ ds }}.csv"
│
Run on 2024-01-15 ──► "sales_2024-01-15.csv"
Run on 2024-01-16 ──► "sales_2024-01-16.csv"
Jinja Basics
Airflow uses a template engine called Jinja. Double curly braces mark a placeholder.
{{ ds }} # date of the run: 2024-01-15
{{ ds_nodash }} # same date without dashes: 20240115
{{ logical_date }} # full date and time object
{{ data_interval_start }} # start of the period the run covers
{{ data_interval_end }} # end of the period the run covers
{{ dag.dag_id }} # name of the DAG
{{ task.task_id }} # name of the task
{{ run_id }} # unique id of the run
A Simple Example
from airflow.operators.bash import BashOperator
export_sales = BashOperator(
task_id="export_sales",
bash_command="echo Exporting sales for {{ ds }} to /data/sales_{{ ds_nodash }}.csv",
)
A run for 15 January 2024 prints: Exporting sales for 2024-01-15 to /data/sales_20240115.csv.
Templated Fields
Airflow renders templates only in fields that the operator marks as templated. For BashOperator, bash_command and env are templated. For PythonOperator, op_args and op_kwargs are templated. The operator documentation lists these fields under template_fields.
| Operator | Common Templated Fields |
|---|---|
| BashOperator | bash_command, env |
| PythonOperator | op_args, op_kwargs, templates_dict |
| SQL operators | sql, parameters |
| EmailOperator | subject, html_content, to |
Templates in SQL
load_daily = SQLExecuteQueryOperator(
task_id="load_daily",
conn_id="warehouse",
sql="""
INSERT INTO daily_sales
SELECT * FROM raw_sales
WHERE sale_date = '{{ ds }}'
""",
)
Each run loads only the rows for its own date. Rerunning an old date reloads the same rows, which keeps results repeatable.
Passing Values Through Python
def show_date(run_date, **context):
print(f"Run date is {run_date}")
task = PythonOperator(
task_id="show_date",
python_callable=show_date,
op_kwargs={"run_date": "{{ ds }}"},
)
Variables, Params, and Macros
| Template | Result |
|---|---|
{{ var.value.env }} | Airflow variable named env |
{{ var.json.settings.region }} | Field inside a JSON variable |
{{ params.limit }} | Value from the DAG params |
{{ macros.ds_add(ds, 7) }} | Date seven days after the run date |
{{ ti.xcom_pull(task_ids='a') }} | XCom value from task a |
Check the Result in the UI
Open a task instance and select Rendered Template. The page shows the final text after Airflow filled every placeholder. Use it to confirm dates and paths before you trust a run.
Templating Rules to Remember
- Never template a field that the operator does not list. The braces stay as plain text.
- Use templates inside the task fields, not at the top level of the DAG file.
- Prefer data interval values for time-based work, since
dsequals the start of the interval. - Add
template_searchpathto the DAG to load long SQL from separate files.
Key Takeaways
- Placeholders in double braces get replaced at run time.
- Only templated fields render, so check the operator documentation.
- Templates make one DAG reusable for every date.
- The Rendered Template page shows the final values.
