导出可供 Operator 使用的动态环境变量¶
在 airflow_local_settings.py 中定义的 get_airflow_context_vars 返回的键值对会被注入到默认的 Airflow 上下文环境变量中,这些变量在运行任务时可作为环境变量使用。请注意,键和值都必须是字符串。
dag_id、task_id、execution_date、dag_run_id、dag_owner、dag_email 是保留键。
例如,在您的 airflow_local_settings.py 文件中
def get_airflow_context_vars(context) -> dict[str, str]:
"""
:param context: The context for the task_instance of interest.
"""
# more env vars
return {"airflow_cluster": "main"}
有关如何配置本地设置的详细信息,请参阅配置本地设置。