[Python] How can I pass Jinja2 templated parameter to a task using the Taskflow API?

Discussão em 'Python' iniciado por Stack, Fevereiro 8, 2025.

  1. Stack

    Stack Membro Participativo

    I have a task that is not correctly rendering one of its parameters if I pass it as "{{ var.json.my_api_key.api_key }}":

    @task
    def generate_signature_headers(api_key="{{ var.json.knmi_api_key.api_key }}",
    hmac_secret_key="{{ var.json.knmi_api_key.hmac_secret_key }}".encode('utf-8')):

    hmac_digest_transformed = ...some code..

    return {
    "Date": now_utc,
    "Authorization": f'Signature keyId="{api_key}",algorithm="hmac-sha512",'
    f'signature="{hmac_digest_transformed}" ',
    }


    the api_key parameter is returned as the string "{{ var.json.knmi_api_key.api_key }}" without being rendered, but hmac_secret_key works as expected.
    Everything works if I pass the api_key parameter as Variable.get('knmi_api_key', deserialize_json=True)['api_key'] but doing so, it is going to create a connection to the metastore database every time the dag is parsed.

    How can I pass the parameter with the Jinja2 templating engine?

    Continue reading...

Compartilhe esta Página