4

Here I am pushing XCOM value:

task_get_username_bash = BashOperator(
                task_id='execute_bash',
                bash_command='whoami',
                xcom_push=True)

So in XCOMs it stores like {'return_value' : '$USER'} (in my case $USER = 'airflow').

Then I want to pull this return_value from XCOM:

task_insert_new_row = PostgresOperator(
                task_id='insert_new_row',
                trigger_rule=TriggerRule.ALL_DONE,
                sql='''INSERT INTO table_name VALUES
                (%s, %s, %s);''',
                parameters=(uuid.uuid4().int % 123456789,
                            "{{ ti.xcom_pull(task_ids='execute_bash', key='return_value') }}",
                            datetime.now()))

But PostgresOperator interpret Macros reference as str. How to pull XCOM in PostgresOperator?

Vladyslav
  • 103
  • 1
  • 8

1 Answers1

5

Problem solved:

task_insert_new_row = PostgresOperator(
                task_id='insert_new_row',
                trigger_rule=TriggerRule.ALL_DONE,
                sql='''INSERT INTO table_name VALUES
                (%s, '{{ ti.xcom_pull(task_ids='execute_bash', key='return_value') }}', %s);''',
                parameters=(uuid.uuid4().int % 123456789, datetime.now()))
Vladyslav
  • 103
  • 1
  • 8
  • What if I have a `sql=file.sql` parameter and I can't modify it, I can only pass some parameters that came from variables inside DAG, and one of them have the `ti.xcom_pull(...)` content? – eddy85br Jun 05 '20 at 04:32
  • @Vladyslav do you know how to push return value by postgres to other operator. Imagine instead of bash script, you have sql operator. If you know please help me with that in https://stackoverflow.com/questions/67614989/how-to-pass-the-result-from-first-query-to-the-second-one-with-postgresoperator – pm1359 May 20 '21 at 09:15