how to print the value of a variable in airflow

Viewed 274

I'm trying to print the content of a variable computed in airflow dag ,therefore I used an echo in a bash operator, but It doesn't work
I tried with a predefined variable but, I got the same output
here is an example :

with DAG(
    dag_id="tmp",
) as dag:
test="test"
test_dag = BashOperator(
    task_id="test_dag",
    bash_command='echo $test',
)

the output is always empty :

Output:
INFO -

2 Answers

You can pass the variable in a f-string. You can even set the task_id based on your variable. See:

    with DAG(
        dag_id="tmp",
    ) as dag:
        test="test"
        test_dag = BashOperator(
            task_id=f"t1_{test}",
            bash_command=f"echo {test}",
        )

You can also use the env parameter like so:

with DAG(dag_id="tmp", start_date=datetime(2022, 1, 1), schedule_interval=None) as dag:
    test_dag = BashOperator(
        task_id="t1",
        bash_command="echo $test",
        env={"test": "something"},
    )

enter image description here

Related