这可能是因为 Airflow 版本在 1.10.6 及早期版本中的某些缺陷导致的。一种解决方法是将使用情况更广泛的 render_template 函数替换 render_template_as_native_obj 函数。
示例代码:
from airflow.models import DAG from airflow.operators.bash_operator import BashOperator from datetime import datetime
dag = DAG( 'example_dag', start_date=datetime.now(), schedule_interval=None )
BashOperator( task_id='print_template_output', bash_command='echo "{{ ds }}"', xcom_push=True, dag=dag )
BashOperator( task_id='print_rendered_template_output', bash_command='echo {{ ti.xcom_pull(task_ids="print_template_output") }}', dag=dag )
任务 "print_template_output" 将模板输出传递到 "print_rendered_template_output" 任务,而后者将使用 {{ ti.xcom_pull(task_ids =“ print_template_output”)}} 渲染它并将其输出。
请注意,该解决方法需要在模板中使用 jinja2 语法。