Skip to content

Commit 2591294

Browse files
ashbkaxil
authored andcommitted
Add note about using dag_run.conf in BashOperator (apache#9143)
(cherry picked from commit 4d8599e) (cherry picked from commit 74d6d1d)
1 parent 4aea266 commit 2591294

File tree

3 files changed

+65
-3
lines changed

3 files changed

+65
-3
lines changed

airflow/example_dags/example_trigger_target_dag.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def run_this_func(ds, **kwargs):
6666
# You can also access the DagRun object in templates
6767
bash_task = BashOperator(
6868
task_id="bash_task",
69-
bash_command='echo "Here is the message: '
70-
'{{ dag_run.conf["message"] if dag_run else "" }}" ',
69+
bash_command='echo "Here is the message: $message"',
70+
env={'message': '{{ dag_run.conf["message"] if dag_run else "" }}'},
7171
dag=dag,
7272
)

airflow/operators/bash_operator.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333

3434

3535
class BashOperator(BaseOperator):
36-
"""
36+
r"""
3737
Execute a Bash script, command or set of commands.
3838
3939
.. seealso::
@@ -53,6 +53,37 @@ class BashOperator(BaseOperator):
5353
:type env: dict
5454
:param output_encoding: Output encoding of bash command
5555
:type output_encoding: str
56+
57+
.. warning::
58+
59+
Care should be taken with "user" input or when using Jinja templates in the
60+
``bash_command``, as this bash operator does not perform any escaping or
61+
sanitization of the command.
62+
63+
This applies mostly to using "dag_run" conf, as that can be submitted via
64+
users in the Web UI. Most of the default template variables are not at
65+
risk.
66+
67+
For example, do **not** do this:
68+
69+
.. code-block:: python
70+
71+
bash_task = BashOperator(
72+
task_id="bash_task",
73+
bash_command='echo "Here is the message: \'{{ dag_run.conf["message"] if dag_run else "" }}\'"',
74+
)
75+
76+
Instead, you should pass this via the ``env`` kwarg and use double-quotes
77+
inside the bash_command, as below:
78+
79+
.. code-block:: python
80+
81+
bash_task = BashOperator(
82+
task_id="bash_task",
83+
bash_command='echo "here is the message: \'$message\'"',
84+
env={'message': '{{ dag_run.conf["message"] if dag_run else "" }}'},
85+
)
86+
5687
"""
5788
template_fields = ('bash_command', 'env')
5889
template_ext = ('.sh', '.bash',)

docs/howto/operator/bash.rst

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,37 @@ You can use :ref:`Jinja templates <jinja-templating>` to parameterize the
4141
:start-after: [START howto_operator_bash_template]
4242
:end-before: [END howto_operator_bash_template]
4343

44+
45+
.. warning::
46+
47+
Care should be taken with "user" input or when using Jinja templates in the
48+
``bash_command``, as this bash operator does not perform any escaping or
49+
sanitization of the command.
50+
51+
This applies mostly to using "dag_run" conf, as that can be submitted via
52+
users in the Web UI. Most of the default template variables are not at
53+
risk.
54+
55+
For example, do **not** do this:
56+
57+
.. code-block:: python
58+
59+
bash_task = BashOperator(
60+
task_id="bash_task",
61+
bash_command='echo "Here is the message: \'{{ dag_run.conf["message"] if dag_run else "" }}\'"',
62+
)
63+
64+
Instead, you should pass this via the ``env`` kwarg and use double-quotes
65+
inside the bash_command, as below:
66+
67+
.. code-block:: python
68+
69+
bash_task = BashOperator(
70+
task_id="bash_task",
71+
bash_command='echo "here is the message: \'$message\'"',
72+
env={'message': '{{ dag_run.conf["message"] if dag_run else "" }}'},
73+
)
74+
4475
Troubleshooting
4576
---------------
4677

0 commit comments

Comments
 (0)