title: Ansible/Jinja2 template : for loop runs twice
body: <p>I have a problem with a for loop in my Jinja2 file.
  I use Ansible template as the following :</p>
  <pre><code>- name: &quot;Configure syslog-ng-nginx&quot;
    template:
      src: &quot;nginx.conf.j2&quot;
      dest: /etc/syslog-ng/conf.d/nginx.conf
      owner: root
      group: root
      mode: 0640
      backup: yes
    with_dict: &quot;{{ nginx_http_template }}&quot;
    notify: restart syslog-ng
  </code></pre>
  <p>My dict looks like this :</p>
  <pre><code>nginx_http_template:
    toto.test.fr:
      conf_file_name: toto.test.fr
      conf_file_location: /etc/nginx/sites-enabled/
      servers:
        server1:
          server_name: 'toto.test.fr'
          listen:
            listen_wildcard:
              ip: '*'
              port: 80
          access_log:
            - name: main
              location: /var/log/nginx/access.toto.test.fr.log
          error_log:
            location: /var/log/nginx/error.toto.test.fr.log
            level: warn
          autoindex: false
          root: /var/www/toto/public
    toto2.test.fr:
      conf_file_name: toto2.test.fr
      conf_file_location: /etc/nginx/sites-enabled/
      servers:
        server1:
          server_name: 'toto2.test.fr'
          listen:
            listen_wildcard:
              ip: '*'
              port: 80
          access_log:
            - name: main
              location: /var/log/nginx/access.toto2.test.fr.log
          error_log:
            location: /var/log/nginx/error.toto2.test.fr.log
            level: warn
          autoindex: false
          root: /var/www/toto2/public
  </code></pre>
  <p>And my <code>nginx.conf.j2</code>template looks like this :</p>
  <pre><code>source {{ nginx_syslog_source_name }} {
  {% for server in item.value.servers %}
    {% for access_log in item.value.servers[server].access_log %}
      file(&quot;{{ access_log.location }}&quot; flags(no-parse) program-override(&quot;nginx-access-{{ item.value.servers[server].server_name }}&quot;));
    {% endfor %}
  {% endfor %}
  };
  </code></pre>
  <p>The problem is, whenever I run the task, the file is getting written with the informations of &quot;toto.test.fr&quot;, but then, instead of &quot;appending&quot; the informations of &quot;toto2.test.fr&quot;, it overwrites the &quot;toto.test.fr&quot; and I only get what's in &quot;toto2.test.fr&quot;.</p>
  <p>Let's have a look at my Ansible output :</p>
  <pre><code>TASK [syslog : Configure syslog-ng-nginx] ***************************************************************************************************************************************
  --- before
  +++ after: /home/yanis/.ansible/tmp/ansible-local-251749PRAOO/tmpIR53tK/nginx.conf.j2
  @@ -0,0 +1,3 @@
  +source s_nginx_toto.test {
  +      file(&quot;/var/log/nginx/access.toto.test.fr.log&quot; flags(no-parse) program-override(&quot;nginx-access-toto.test.fr&quot;));
  +  };
  
  changed: [MYHOST] =&gt; (item={'value': {u'conf_file_name': u'toto.test.fr', u'conf_file_location': u'/etc/nginx/sites-enabled/', u'servers': {u'server1': {u'error_lo
  g': {u'location': u'/var/log/nginx/error.toto.test.fr.log', u'level': u'warn'}, u'server_name': u'toto.test.fr', u'autoindex': False, u'access_log': [{u'name': u'main', u'locati
  on': u'/var/log/nginx/access.toto.test.fr.log'}], u'root': u'/var/www/toto/public', u'listen': {u'listen_wildcard': {u'ip': u'*', u'port': 80}}}}}, 'key': u'toto.test.fr'})
  
  --- before: /etc/syslog-ng/conf.d/nginx.conf
  +++ after: /home/yanis/.ansible/tmp/ansible-local-251749PRAOO/tmpRaaSpL/nginx.conf.j2
  @@ -1,3 +1,3 @@
   source s_nginx_toto_test {
  -      file(&quot;/var/log/nginx/access.toto.test.fr.log&quot; flags(no-parse) program-override(&quot;nginx-access-toto.test.fr&quot;));
  +      file(&quot;/var/log/nginx/access.toto2.test.fr.log&quot; flags(no-parse) program-override(&quot;nginx-access-toto2.test.fr&quot;));
     };
  
  changed: [MYHOST] =&gt; (item={'value': {u'conf_file_name': u'toto2.test.fr', u'conf_file_location': u'/etc/nginx/sites-enabled/', u'servers': {u'server1': {u'error_l
  og': {u'location': u'/var/log/nginx/error.toto2.test.fr.log', u'level': u'warn'}, u'server_name': u'toto2.test.fr', u'autoindex': False, u'access_log': [{u'name': u'main', u'loc
  ation': u'/var/log/nginx/access.toto2.test.fr.log'}], u'root': u'/var/www/toto2/public', u'listen': {u'listen_wildcard': {u'ip': u'*', u'port': 80}}}}}, 'key': u'toto2.test.fr'}
  )
  
  </code></pre>
  <p>As you can see, the &quot;toto.test.fr&quot; infos get written, but then, the &quot;toto2.test.fr&quot; also get written and overwrite the &quot;toto.test.fr&quot;.</p>
  <p>Here he's the final file :</p>
  <pre><code>source s_nginx_toto_test {
        file(&quot;/var/log/nginx/access.toto2.test.fr.log&quot; flags(no-parse) program-override(&quot;nginx-access-toto2.test.fr&quot;));
    };
  
  </code></pre>
  <p>What I want is :</p>
  <pre><code>source s_nginx_toto_test {
        file(&quot;/var/log/nginx/access.toto.test.fr.log&quot; flags(no-parse) program-override(&quot;nginx-access-toto.test.fr&quot;));
    };
        file(&quot;/var/log/nginx/access.toto2.test.fr.log&quot; flags(no-parse) program-override(&quot;nginx-access-toto2.test.fr&quot;));
    };
  
  </code></pre>
  <p>Any idea ?</p>