apache/airflow

Allow usage of Jinja templating inside task `param`

Open

#31 801 ouverte le 8 juin 2023

Voir sur GitHub
 (11 commentaires) (2 réactions) (0 assignés)Python (16 781 forks)batch import
good first issuekind:feature

Métriques du dépôt

Stars
 (44 809 stars)
Métriques de merge PR
 (Merge moyen 7j 18h) (834 PRs mergées en 30 j)

Description

Description

Currently, it is not possible to use Jinja templates inside the params field of a task.

It is not possible to do the following :

bo = BashOperator(
   task_id="bash_task",
   bash_command = "do-something.sh {{params.path}}",
   params = {"path": "/path/to/thing/{{ ds }}/thing.csv"}
)

Use case/motivation

We have a case using the PostgresOperator :

PostgresOperator(
      task_id="do_postgres_operation",
      sql="my_sql_script.sql",
      params={
            "schema": "my_schema",
            "bucket": "my_bucket"
      }
)

Say my_sql_script.sql is a script that let you copy data from S3 bucket to Postgres. It's pretty common and we would like to reuse it here and there. But in our case, we get the s3_file_name from the xcom of the previous task, so to get that filename we need to use {{ ti.xcom_pull(task_ids='my_previous_task') }} and we would like to use it in params that way :

      ...
      params={
            "schema": "my_schema",
            "bucket": "my_bucket",
            "s3_file_name": "{{ ti.xcom_pull(task_ids='my_previous_task') }}"
      }
      ...

But it's not possible, we thus need to hard-code {{ ti.xcom_pull(task_ids='my_previous_task') }} inside my_sql_script.sql script.

But later in the code, we don't want s3_file_name to come from xcom but rather from a python variable, and it's impossible because it's hard coded in the sql template. We have to create an other sql file with the exact same code except for s3_file_name.

Related issues

This issue is related to the problem : https://github.com/apache/airflow/issues/7910 It has been marked as resolved but the 2 PRs associated are closed

Are you willing to submit a PR?

  • Yes I am willing to submit a PR!

Code of Conduct

Guide contributeur