Ansible template loop with iteritems

965 views
Skip to first unread message

Ciobancai Vlad

unread,
Apr 20, 2018, 7:53:27 AM4/20/18
to Ansible Project
Hi,

I would like to create dynamically some configuration files (the configs are used by tomcat) using a template  based some variables from group_vars

Below is what I have

- group_vars/test.yml

file: 
    - log4j
    - config

log4j:
    log4j.rootCategory: INFO
    log4j.appender.SYSLOG: org.apache.log4j.net.SyslogAppender
    log4j.appender.SYSLOG.syslogHost: testhost

jira:
    jira.soap.url: https://hostname
    jira.login.username: test
    jira.login.password: test14


- roles/test/main.yml

   - name: 'Configure the application properties'
        template:
          src: "template.j2"
          dest: "/tmp/{{ item }}.properties"
          owner: build
          group: build
          backup: yes
        with_items: "{{ file }}"

- roles/test/template/template.j2

{% for key, value in {{ item.iteritems() }} %}
   {{ key }}= {{ value }}
{% endfor %} 


So I would like to iterate every "file" and to create the files
I'm receiving "msg": "AnsibleError: template error while templating string: expected token ':', got '}'. String: ## File managed by ansible ##\n\n{{ item }}\n{{ propertyfiles }}\n\n{% for i in {{ propertyfiles }} %}\n {% for key, value in i.items() %}\n {{ key }}= {{ value }}\n {% endfor %} \n{% endfor %} \n"}

Kai Stian Olstad

unread,
Apr 20, 2018, 9:20:58 AM4/20/18
to ansible...@googlegroups.com
On 20.04.2018 13:53, Ciobancai Vlad wrote:
> {% for key, value in {{ item.iteritems() }} %}
> {{ key }}= {{ value }}
> {% endfor %}

You are already in jinja template mode between {% %} so you can't use {{
}} there.

{% for key, value in item.iteritems() %}


--
Kai Stian Olstad

Vlad

unread,
Apr 20, 2018, 9:29:25 AM4/20/18
to Ansible Project
I'm receiving the folllowing error "msg": "AnsibleUndefinedVariable: 'ansible.utils.unsafe_proxy.AnsibleUnsafeText object' has no attribute 'iteritems'"} 

Kai Stian Olstad

unread,
Apr 20, 2018, 9:41:25 AM4/20/18
to ansible...@googlegroups.com
On 20.04.2018 15:29, Vlad wrote:
> On Friday, 20 April 2018 16:20:58 UTC+3, Kai Stian Olstad wrote:
>>
>> On 20.04.2018 13:53, Ciobancai Vlad wrote:
>> > {% for key, value in {{ item.iteritems() }} %}
>> > {{ key }}= {{ value }}
>> > {% endfor %}
>>
>> You are already in jinja template mode between {% %} so you can't use
>> {{
>> }} there.
>>
>> {% for key, value in item.iteritems() %}
>>
>>
>
>
> I'm receiving the folllowing error "msg": "AnsibleUndefinedVariable:
> 'ansible.utils.unsafe_proxy.AnsibleUnsafeText object' has no attribute
> 'iteritems'"}

I didn't look at what you where trying to do I just saw the syntax
error.

But I see what you are trying to do, I'm not sure but maybe this work

{% for key, value in vars[item].iteritems() %}


--
Kai Stian Olstad

Vlad

unread,
Apr 20, 2018, 9:44:21 AM4/20/18
to Ansible Project
Works, thank you for your help. 

Vlad

unread,
Apr 20, 2018, 10:01:06 AM4/20/18
to Ansible Project
I have another question, after I applied the changes, now every line in the files contains a new empty line. It is possible to be removed ?

{% for key, value in vars[item].iteritems()| sort %} 
{{ key }}= {{ value }}
{% endfor %} 


log4j.appender.MAIN_APPENDER= org.apache.log4j.RollingFileAppender

log4j.appender.MAIN_APPENDER.File= /tmp/log4j.log

log4j.appender.MAIN_APPENDER.MaxBackupIndex= 5

Kai Stian Olstad

unread,
Apr 23, 2018, 2:50:04 AM4/23/18
to ansible...@googlegroups.com
I don't know why since you shouldn't unless you input contain extra
whitespaces.

You can try to use {%- and/or -%} to remove whitespaces, more on the
subject here
http://jinja.pocoo.org/docs/2.10/templates/#whitespace-control


--
Kai Stian Olstad

Vlad

unread,
Apr 23, 2018, 3:17:55 AM4/23/18
to Ansible Project
Thank you Kai for all the support, works OK 
Reply all
Reply to author
Forward
0 new messages