Is there any way to get config parameters in Bash operator, when we run dag manually with config from airflow UI (like we have for Python kwargs and args using context)?

1

There are 1 answers

0
Bas Harenslak On

Yes, this can be done using templating. The Airflow docs show a few examples of templating + BashOperator: https://airflow.apache.org/docs/apache-airflow/stable/howto/operator/bash.html#templating. Since Bash isn't Python code where you can pass through Python variables, you have to define "placeholders" in the bash_command argument for Python variables/statements to evaluate at runtime. For example:

BashOperator(
    task_id="example",
    bash_command='echo "{{ params.foo }}"',
)

The curly braces {{ }} define a template and params is an object that holds user-defined variables, that you can set when triggering a DAG. For example, the following configuration when triggering a DAG and the code above, will make the BashOperator run echo "bar".

enter image description here