[Python] {{ ds }} isn't interpreted in my airflow dag

Discussão em 'Python' iniciado por Stack, Março 20, 2025.

  1. Stack

    Stack Membro Participativo

    My goal is to dynamically create some SFTPToS3Operator to retrieve files from a server to Amazon S3.

    For instance, ,my filename is xxx_date_hours_minutes_seconds.csv, so I want to fetch filename from the server to use it in the SFTPToS3Operator operator.

    The main problem is that the {{ ds }} is not interpreted

    with DAG(...)
    as dag:
    init_task = DummyOperator(task_id='dag_start')
    alertPerfOperatorsTask = list_files_to_transfer()
    init_task >> alertPerfOperatorsTask


    The goal of list_files_to_transfer is to create a list of task, one task for each file :

    def list_files_to_transfer():
    logger = logging.getLogger('airflow.task')
    yesterday_date = '{{ macros.ds_add(ds, -1) }}'
    logger.info("yesterday_date: " + yesterday_date)
    sftp_hook = SFTPHook(ssh_conn_id='SFTP__gas__PointBreakExporter')
    with sftp_hook.get_conn() as sftp_client:
    remote_path = 'out/IQUAL_DEV/'
    ...
    return alertPerfOperatorsTask (list of SFTPToS3Operator)


    If I look at my log, I get :

    INFO - yesterday_date: `{{ macros.ds_add(ds, -1) }}` instead of yesterday_date: 2025-03-19


    and of course I am using this date to find file but obvioulsy didn't find any file named xxx_{{ macros.ds_add(ds, -1) }}.csv

    I don't understand why {{ macros.ds_add(ds, -1) }} isn't interpreted

    Continue reading...

Compartilhe esta Página