title: Ansible: How to create nested dictionary using Jinja2
body: <p>Here is the output.</p>
  <pre><code>&quot;result.containers&quot;:[
  {
    &quot;Image&quot;:&quot;ca.docker/webproxy:1.0.0&quot;,
    &quot;Names&quot;:[
       &quot;/customer1&quot;
    ]
  },
  {
    &quot;Image&quot;:&quot;docker.local/egacustomer:1.0.1&quot;,
    &quot;Names&quot;:[
       &quot;/registrator&quot;
    ]
  }
  ]
  </code></pre>
  <p>I'm trying to get the following output using jinja2</p>
  <pre><code>&quot;containerlist&quot;=&gt;{
              &quot;webproxy&quot;: {
                  &quot;name&quot;: &quot;customer1&quot;
             }, 
              &quot;egacustomer&quot;: {
                  &quot;name&quot;: &quot;registrator&quot;
             }
           }
  </code></pre>
  <p>Here is my jinja2 code.</p>
  <pre><code>- set_fact:
        containerlist: |
         {
         {% for item in result.containers %}
         {{ item.Image.split('/')[-1].split(':')[0] | replace('\n', '') }}
              name : {{ item.Names[0][1:] | replace('\n', '') }}
         {% endfor %}
         }
  </code></pre>
  <p>I get the below output.</p>
  <pre><code> &quot;containerlist&quot;: &quot;{\nwebproxy\n     name : customer1\negacustome\n     name : registrator\n}&quot;
  </code></pre>
  <p>Could someone please help me get the desired output. Any help would be greatly appreciated</p>