Search code examples
bashairflowtemplating

Airflow XCom send variables inside templating


I've been trying to send a the contents of a variable to Xcom in a bash script. However, I am failing at it.

test_bash = """
export test_val='123'
{{ ti.xcom_push(key='1',value=test_val) }}
echo $test_val
"""

bash_tash = BashOperator(
    task_id='test',
    bash_command=test_bash,
    retries=3,
    dag=dag)

In the code snipped above. When I try to pull it. I'm not able to send anything to Xcom. I have tried sending text in single quotations and it works fine.

Is there a way to send variables from bash scripts to xcom?


Solution

  • You can do it differently using a BashOperator to push your value:

    test_bash = """
    export test_val='123'
    echo $test_val
    """
    
    bash_task = BashOperator(
        task_id='test',
        bash_command=test_bash,
        xcom_push=True
        retries=3,
        dag=dag)
    

    task instance > XCom

    And then you pull the value into another task.