title: Value substitution in Ansible template
body: <p>I am working on an Ansible playbook, where in a template I need to substitute a variable which is a list of dictionaries.</p>
  
  <p>The task file is as follows:</p>
  
  <pre><code>vars:
  locations:
    - context: "/rest"
      server: "http://locahost:8080;"
    - context: "/api"
      server: "http://localhost:9090;"
  tasks:
  - name: testing the template
    template:
      src: ./conf.j2
      dest: /tmp/test.conf
    with_items: '{{ locations }}'
  </code></pre>
  
  <p>I need to substitute the <code>locations</code> in the template. So the template is as follows:</p>
  
  <pre><code>{% for location in item %}
       location {{ location['context'] }}
       proxy_pass {{ location['server'] }}
  {% endfor %} 
  </code></pre>
  
  <p>I was expecting an output as follows:</p>
  
  <pre><code>location /rest
  proxy_pass http://localhost:8080
  
  location /api
  proxy_pass htpp://localhost:9090
  </code></pre>
  
  <p>But i am having hard time in getting the substitution correct, can anybody help in pointing out where am I making the mistake. </p>
  
  <p>The error I am getting is</p>
  
  <pre><code>failed: [127.0.0.1] (item={u'context': u'/rest', u'server': 
  u'http://localhost:9090;'}) =&gt; {"failed": true, "item": {"context": 
  "/rest", "server": "http://localhost:8080;"}, "msg": 
  "AnsibleUndefinedVariable: 'context' is undefined"}
  failed: [127.0.0.1] (item={u'context': u'/api', u'server': 
  u'http://locahost:8080;'}) =&gt; {"failed": true, "item": {"context": 
  "/api", "server": "http://locahost:9090;"}, "msg": 
  "AnsibleUndefinedVariable: 'context' is undefined"}
  </code></pre>