If you need to use xcoms in a BashOperator
and the desire is to pass the arguments to a python script from the xcoms, then I would suggest adding some argparse
arguments to the python script then using named arguments and Jinja templating the bash_command
. So something like this:
# Assuming you already xcom pushed the variable as "my_xcom_variable"
my_task = BashOperator(
task_id='my_task',
bash_command='python3 /opt/airflow/dags/programs/my_task.py --arg1={{ ti.xcom_pull(key="my_xcom_variable") }}',
)
Then if you are unfamiliar with argparse
you can add it at the end of the python script like so:
# ^^^ The rest of your program is up here ^^^
# I have no idea what your python script is,
# just assuming your main program is a function called main_program()
# add as many arguments as you want and name them whatever you want
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--arg1')
args = parser.parse_args()
main_program(args_from_the_outside=args.arg1)