简体   繁体   English

气流模板问题

[英]Airflow templating issue

I have a simple Airflow dag:我有一个简单的 Airflow dag:

from datetime import timedelta


from airflow import DAG
from airflow.operators.bash_operator import BashOperator
from airflow.utils.dates import days_ago

default_args = {
    'owner': 'airflow',
    'depends_on_past': False,
    'start_date': days_ago(2),
    'email': ['airflow@example.com'],
    'email_on_failure': False,
    'email_on_retry': False,
    'retries': 1,
    'retry_delay': timedelta(minutes=5),
}
dag = DAG(
    'tutorial',
    default_args=default_args,
    description='A simple tutorial DAG',
    schedule_interval=timedelta(days=1),
)

templated_command = """
echo "{{ ds }}"
echo "{{ macros.ds_add(ds, 7)}}"
echo "Param: {{ params.my_param }}"
"""

t3 = BashOperator(
    task_id='templated',
    depends_on_past=False,
    bash_command=templated_command,
    params={'my_param': templated_command},
    dag=dag,
)

I would like to be able to pass a template to params .我希望能够将模板传递给params However, this doesn't seem to work the way I am doing it, since the rendered template outputs:但是,这似乎不像我这样做的方式,因为渲染的模板输出:

echo "2020-09-17"
echo "2020-09-24"
echo "Param: 
echo "{{ ds }}"
echo "{{ macros.ds_add(ds, 7)}}"
echo "Param: {{ params.my_param }}"
"

It is effectively rendering the bash_command but not the params .它有效地呈现bash_command而不是params Is there a way to solve this and render the params properly?有没有办法解决这个问题并正确渲染params

You are passing whole template as value to my_param in Params, which is a string, for your actual template it does renders every ( ds , macros.ds_add(ds, 7) and even param.my_param ) right您将整个模板作为值传递给 Params 中的my_param ,这是一个字符串,对于您的实际模板,它确实呈现每个( ds , macros.ds_add(ds, 7)甚至param.my_param )正确

whats happening is, you get echo "{{ ds }}" echo "{{ macros.ds_add(ds, 7)}}" echo "Param: {{ params.my_param }}" " displayed again as string because it is the value of my_param发生的事情是,你得到echo "{{ ds }}" echo "{{ macros.ds_add(ds, 7)}}" echo "Param: {{ params.my_param }}" "再次显示为字符串,因为它是my_param

I dont know why you want to pass whole temple as string to my_param ?我不知道你为什么要将整个寺庙作为字符串传递给my_param

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM