1

Outside of an operator, I need to call a SubdagOperator and pass it an operator's return value, using xcom. I've seen tons of solutions (Airflow - How to pass xcom variable into Python function, How to retrieve a value from Airflow XCom pushed via SSHExecuteOperator, etc).

They all basically say 'variable_name': "{{ ti.xcom_pull(task_ids='some_task_id') }}"

But my Jinja template keeps getting rendered as a string, and not returning the actual variable. Any ideas why?

Here is my current code in the main dag:

PARENT_DAG_NAME = 'my_main_dag'
CHILD_DAG_NAME = 'run_featurization_dag'

run_featurization_task = SubDagOperator(
    task_id=CHILD_DAG_NAME,
    subdag=run_featurization_sub_dag(PARENT_DAG_NAME, CHILD_DAG_NAME, default_args, cur_date, "'{{ ti.xcom_pull(task_ids='get_num_accounts', dag_id='" + PARENT_DAG_NAME + "') }}'" ),  
    default_args=default_args,
    dag=main_dag
)
Yusuf Khaled
  • 53
  • 1
  • 5

2 Answers2

1

Too many quotes? Try this one

"{{ ti.xcom_pull(task_ids='get_num_accounts', dag_id='" + PARENT_DAG_NAME + "') }}"
SergiyKolesnikov
  • 7,369
  • 2
  • 26
  • 47
0

Jinja templating works only for certain parameters, not all.

You can use Jinja templating with every parameter that is marked as “templated” in the documentation. Template substitution occurs just before the pre_execute function of your operator is called.

https://airflow.apache.org/concepts.html#jinja-templating

So I'm afraid you can't pass a variable this way.

theotheo
  • 2,664
  • 1
  • 23
  • 21