eladkal opened a new issue #8688:
URL: https://github.com/apache/airflow/issues/8688


   
   **What happened**:
   
   Defined variable as:
   ![Screen Shot 2020-05-03 at 11 45 
51](https://user-images.githubusercontent.com/45845474/80909798-b81d5500-8d33-11ea-93ee-2859997bf0ef.png)
   
   Trying to access the variable via:
   `{{ var.json.csba-catalog.sandbox }}`
   
   Gives:
   ```
   Traceback (most recent call last):
     File "/usr/local/lib/python3.6/site-packages/airflow/models/__init__.py", 
line 1426, in _run_raw_task
       self.render_templates()
     File "/usr/local/lib/python3.6/site-packages/airflow/models/__init__.py", 
line 1790, in render_templates
       rendered_content = rt(attr, content, jinja_context)
     File "/usr/local/lib/python3.6/site-packages/airflow/models/__init__.py", 
line 2538, in render_template
       return self.render_template_from_field(attr, content, context, jinja_env)
     File "/usr/local/lib/python3.6/site-packages/airflow/models/__init__.py", 
line 2514, in render_template_from_field
       result = jinja_env.from_string(content).render(**context)
     File "/usr/local/lib/python3.6/site-packages/jinja2/asyncsupport.py", line 
76, in render
       return original_render(self, *args, **kwargs)
     File "/usr/local/lib/python3.6/site-packages/jinja2/environment.py", line 
1008, in render
       return self.environment.handle_exception(exc_info, True)
     File "/usr/local/lib/python3.6/site-packages/jinja2/environment.py", line 
780, in handle_exception
       reraise(exc_type, exc_value, tb)
     File "/usr/local/lib/python3.6/site-packages/jinja2/_compat.py", line 37, 
in reraise
       raise value.with_traceback(tb)
     File "<template>", line 1, in top-level template code
     File "/usr/local/lib/python3.6/site-packages/jinja2/environment.py", line 
430, in getattr
       return getattr(obj, attribute)
     File "/usr/local/lib/python3.6/site-packages/airflow/models/__init__.py", 
line 1728, in __getattr__
       self.var = Variable.get(item, deserialize_json=True)
     File "/usr/local/lib/python3.6/site-packages/airflow/utils/db.py", line 
73, in wrapper
       return func(*args, **kwargs)
     File "/usr/local/lib/python3.6/site-packages/airflow/models/__init__.py", 
line 4368, in get
       raise KeyError('Variable {} does not exist'.format(key))
   KeyError: 'Variable csba does not exist'
   ```
   Trying to access with:
   `catalog_variable = Variable.get("csba-catalog", deserialize_json=True)` 
works.
   
   **How to reproduce it:**
   ```
   from airflow.operators.bash_operator import BashOperator
   catalog_variable = Variable.get("csba-catalog", deserialize_json=True)
   catalog= catalog_variable["sandbox"]
   
   default_args = {
       'owner': 'airflow',
       'depends_on_past': False,
       'start_date': datetime(2019, 6, 25),
   }
   
   with DAG(
       dag_id="reproduce_bug",
       default_args=default_args,
       schedule_interval=None,
       max_active_runs=1,
       catchup=False
   ) as dag:
   
       t1 = BashOperator(
           task_id='jinja',
           bash_command='echo {{ var.json.csba-catalog.sandbox }}',
           dag=dag,
       )
       t2 = BashOperator(
           task_id='variable_get',
           bash_command=f'echo {catalog}',
           dag=dag,
       )
   ```
   
   Gives:
   **task_id='jinja':**
   ![Screen Shot 2020-05-03 at 11 54 
54](https://user-images.githubusercontent.com/45845474/80909969-1bf44d80-8d35-11ea-8f4f-b684923c4a99.png)
    **task_id='variable_get':**
   ![Screen Shot 2020-05-03 at 11 55 
06](https://user-images.githubusercontent.com/45845474/80909974-21ea2e80-8d35-11ea-900a-fae6bcc2409f.png)
   
   
   **more info:**
   If I remove the hyphen:
   ![Screen Shot 2020-05-03 at 11 59 
10](https://user-images.githubusercontent.com/45845474/80910050-a0df6700-8d35-11ea-9265-4af918979818.png)
   Both works.
   
   
   **Required fix**
   It should work in both cases. 


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to