This is an automated email from the ASF dual-hosted git repository.

potiuk pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/airflow.git


The following commit(s) were added to refs/heads/main by this push:
     new cafe98a253 [docs] best-practices add use variable with template 
example. (#27316)
cafe98a253 is described below

commit cafe98a25331fef89272151d272b3000187d0f09
Author: Bob Du <[email protected]>
AuthorDate: Mon Oct 31 12:48:33 2022 +0800

    [docs] best-practices add use variable with template example. (#27316)
---
 docs/apache-airflow/best-practices.rst | 42 ++++++++++++++++++++++++++++++++++
 1 file changed, 42 insertions(+)

diff --git a/docs/apache-airflow/best-practices.rst 
b/docs/apache-airflow/best-practices.rst
index 9c80f10dcf..337815a63b 100644
--- a/docs/apache-airflow/best-practices.rst
+++ b/docs/apache-airflow/best-practices.rst
@@ -213,6 +213,48 @@ or if you need to deserialize a json object from the 
variable :
 
     {{ var.json.<variable_name> }}
 
+Make sure to use variable with template in operator, not in the top level code.
+
+Bad example:
+
+.. code-block:: python
+
+    from airflow.models import Variable
+
+    foo_var = Variable.get("foo")  # DON'T DO THAT
+    bash_use_variable_bad_1 = BashOperator(
+        task_id="bash_use_variable_bad_1", bash_command="echo variable 
foo=${foo_env}", env={"foo_env": foo_var}
+    )
+
+    bash_use_variable_bad_2 = BashOperator(
+        task_id="bash_use_variable_bad_2",
+        bash_command=f"echo variable foo=${Variable.get('foo')}",  # DON'T DO 
THAT
+    )
+
+    bash_use_variable_bad_3 = BashOperator(
+        task_id="bash_use_variable_bad_3",
+        bash_command="echo variable foo=${foo_env}",
+        env={"foo_env": Variable.get("foo")},  # DON'T DO THAT
+    )
+
+
+Good example:
+
+.. code-block:: python
+
+    bash_use_variable_good = BashOperator(
+        task_id="bash_use_variable_good",
+        bash_command="echo variable foo=${foo_env}",
+        env={"foo_env": "{{ var.value.get('foo') }}"},
+    )
+
+.. code-block:: python
+
+  @task
+  def my_task():
+      var = Variable.get("foo")  # this is fine, because func my_task called 
only run task, not scan dags.
+      print(var)
+
 For security purpose, you're recommended to use the :ref:`Secrets 
Backend<secrets_backend_configuration>`
 for any variable that contains sensitive data.
 

Reply via email to