ZhangZhihui's Blog  

📌 Most Useful Airflow Template Variables

🧩 Task Instance Context

VariableMeaning
ti The TaskInstance object. Commonly used.
task_instance Same as ti — full TaskInstance object.
task The task object (operator instance).
task_instance_key_str Unique identifier for this task instance (DAG ID + task ID + run date).

Example usage:

echo "Running: {{ task_instance_key_str }}"

🗓️ Execution Time / Dates

VariableMeaning
ds Execution date as YYYY-MM-DD.
ds_nodash Execution date as YYYYMMDD.
ts Execution timestamp (ISO8601).
ts_nodash Timestamp without dashes.
logical_date Pendulum datetime of the logical (scheduled) execution date (same as execution_date).
next_ds Next execution date (string).
prev_ds Previous execution date (string).
next_execution_date Next logical execution datetime.
prev_execution_date Previous logical execution datetime.
macros.* All Airflow macro helpers.

Example:

echo "Date: {{ ds }} Next: {{ next_ds }}"

🧳 DAG-related

VariableMeaning
dag The DAG object.
dag_run The DAGRun object (contains run_id, conf, etc.).
run_id The DAG run ID (e.g., manual__2024-01-01T00:00:00+00:00).
conf Runtime config passed via REST UI/trigger (a dict).
params DAG-level params.

Example (pull config values):

echo "Passed value: {{ dag_run.conf['my_key'] }}"

🔄 Data Intervals (Airflow 2.2+)

Airflow newer versions use data intervals:

VariableMeaning
data_interval_start Start of data interval (Pendulum datetime).
data_interval_end End of data interval.

🔧 Macros (helpers)

Airflow provides many helpers under macros.

Common ones:

MacroMeaning
macros.ds_add(ds, n) Add n days to a date.
macros.ds_format(ds, input_fmt, output_fmt) Transform date format.
macros.datetime Python datetime module.
macros.timedelta Python timedelta.
macros.uuid() Random UUID string.

Example:

echo "{{ macros.ds_add(ds, 7) }}"

📚 Quick copy-friendly reference

# Task instance context
{{ ti }}
{{ task_instance }}
{{ task }}
{{ task_instance_key_str }}

# DagRun & config
{{ dag_run }}
{{ run_id }}
{{ conf }}
{{ params }}

# Execution datetime
{{ ds }}
{{ ds_nodash }}
{{ ts }}
{{ ts_nodash }}
{{ logical_date }}

# Data intervals
{{ data_interval_start }}
{{ data_interval_end }}

# Previous/Next execution
{{ prev_ds }}
{{ next_ds }}
{{ prev_execution_date }}
{{ next_execution_date }}

# Macros
{{ macros }}
{{ macros.ds_add(ds, 1) }}
{{ macros.datetime }}
{{ macros.timedelta }}
{{ macros.uuid() }}

 

posted on 2025-12-10 09:59  ZhangZhihuiAAA  阅读(3)  评论(0)    收藏  举报