title: Set empty variables for jinja2 template values
body: <p>I have a Jinja2 template file that is being used by Ansible to render the actual configuration file. My use case is to use the same templated file and run it manually (using a wrapper python script), use the system environment variables and populate the template file. For example, the following is my template</p>
  
  <pre><code>{%- if groups['abc'] is defined -%}
    {#- inventory compatible with smoketest using obsolete cluster cms/sat -#}
    {%- set groupname='abc' -%}
  {%- else -%}
    {%- if groups['sat'] is defined -%}
      {#- classic definition in fixed inventory 4.x release -#}
      {%- set groupname='sat' -%}
    {%- else -%}
      {#- first pass definition from 4.x releases -#}
      {%- set groupname='hosts' -%}
    {%- endif -%}
  {%- endif -%}
  </code></pre>
  
  <p>And, I render the template using the code below:</p>
  
  <pre><code>#!/usr/bin/python
  import jinja2
  import os
  
  print os.environ
  templateLoader = jinja2.FileSystemLoader(searchpath="./")
  templateEnv = jinja2.Environment(loader=templateLoader)
  TEMPLATE_FILE = "test.j2"
  template = templateEnv.get_template(TEMPLATE_FILE)
  outputText = template.render(env=os.environ)  # this is where to put args to the template renderer
  
  print(outputText)
  </code></pre>
  
  <p>But, I get the following error to begin with:</p>
  
  <pre><code>Traceback (most recent call last):
    File "test.py", line 10, in &lt;module&gt;
      outputText = template.render(env=os.environ)  # this is where to put args to the template renderer
    File "/Users/karthikjayaraman/Library/Python/2.7/lib/python/site-packages/jinja2/environment.py", line 1008, in render
      return self.environment.handle_exception(exc_info, True)
    File "/Users/karthikjayaraman/Library/Python/2.7/lib/python/site-packages/jinja2/environment.py", line 780, in handle_exception
      reraise(exc_type, exc_value, tb)
    File "./test.j2", line 9, in top-level template code
      {%- if groups['mdc'] is defined -%}
    File "/Users/karthikjayaraman/Library/Python/2.7/lib/python/site-packages/jinja2/environment.py", line 411, in getitem
      return obj[argument]
  jinja2.exceptions.UndefinedError: 'groups' is undefined
  </code></pre>
  
  <p>In this case, how do I set the groups to an empty value (but define it) in the environment variable so that I do not run into the undefined error. I tried the following settings in shell (before running the python script) but still I get this error. </p>
  
  <pre><code>export groups=[]
  export groups={}
  export groups=()
  declare -a groups
  </code></pre>
  
  <p>EDIT 1: One interesting thing I found is that when I set export groups={}, I do not see that variable in print os.environ. </p>