Hi both

Thank you very much for the help (and humouring me).

The following works perfectly:

{% for server in usenet_servers %}
Server{{ loop.index }}.Level={{ server.level }}
Server{{ loop.index }}.Host={{ server.host }}
Server{{ loop.index }}.Port={{ server.port }}
Server{{ loop.index }}.Username={{ server.username }}
Server{{ loop.index }}.Password={{ server.password }}
Server{{ loop.index }}.JoinGroup={{ server.join_group }}
Server{{ loop.index }}.Connections= {{ server.connections }}

{% endfor %}

Cheers =]

On 22 December 2014 at 14:55, Matt Martz <[email protected]> wrote:

> I believe you need to make 2 changes:
>
> loop.index instead of usenet_servers[server].loop.index
>
> And just use server instead usenet_servers[server] everywhere else. server
> isn't the index, it is the actual hash/dict.
> On Dec 22, 2014 4:08 AM, "ayounggun" <[email protected]> wrote:
>
>> Hi all
>>
>> Can anyone help me with do a loop in a template?
>>
>> bcoca helped me earlier on IRC and I think I'm a bit closer than I was
>> before! =]
>>
>> (1) variables (layout copied from http://www.yaml.org/start.html)
>>
>> usenet_servers:
>>     - server: astraweb
>>       level: 0
>>       host: ssl-eu.astraweb.com
>>       port: 563
>>       username: me
>>       password: mypass
>>       join_group: yes
>>       connections: 2
>>     - server: newsgroupdirect
>>       level: 1
>>       host: news-eu-ssl.newsgroupdirect.com
>>       port: 563
>>       username: me
>>       password: mypass
>>       join_group: yes
>>       connections: 3
>>     - server: newsgroupdirect
>>       level: 1
>>       host: news.tweaknews.eu
>>       port: 563
>>       username: me
>>       password: mypass
>>       join_group: yes
>>       connections: 10
>>
>>
>> (2) task
>> - name: create nzbget conf file
>>   template:
>>     src=nzbget.conf.j2
>>     dest=/etc/nzbget.conf
>>     mode=660
>>     owner=root
>>     group=library
>>
>>
>>
>> (3) loop in template nzbget.conf.j2
>>
>> {% for server in usenet_servers %}
>> Server{{ usenet_servers[server].loop.index }}.Level={{ usenet_servers[
>> server].level }}
>> Server{{ usenet_servers[server].loop.index }}.Host={{ usenet_servers[
>> server].host }}
>> Server{{ usenet_servers[server].loop.index }}.Port={{ usenet_servers[
>> server].port }}
>> Server{{ usenet_servers[server].loop.index }}.Username={{ usenet_servers[
>> server].username }}
>> Server{{ usenet_servers[server].loop.index }}.Password={{ usenet_servers[
>> server].password }}
>> Server{{ usenet_servers[server].loop.index }}.JoinGroup={{ usenet_servers
>> [server].join_group }}
>> Server{{ usenet_servers[server].loop.index }}.Connections= {{
>> usenet_servers[server].connections }}
>>
>>
>> {% endfor %}
>>
>>
>> (4) desired output from loop
>>
>> Server1.Level=0
>> Server1.Host=ssl-eu.astraweb.com
>> Server1.Port=563
>> Server1.Username=me
>> Server1.Password=mypass
>> Server1.JoinGroup=yes
>> Server1.Connections=2
>>
>> Server2.Level=1
>> Server2.Host=news-eu-ssl.newsgroupdirect.com
>> Server2.Port=563
>> Server2.Username=me
>> Server2.Password=mypass
>> Server2.JoinGroup=yes
>> Server2.Connections=3
>>
>> Server3.Level=1
>> Server3.Host=news.tweaknews.eu
>> Server3.Port=563
>> Server3.Username=me
>> Server3.Password=mypass
>> Server3.JoinGroup=yes
>> Server3.Connections=10
>>
>>
>> (5) error
>> TASK: [nzbget | create nzbget conf file]
>> **************************************
>> <archpi> ESTABLISH CONNECTION FOR USER: root
>> <archpi> EXEC ['sshpass', '-d12', 'ssh', '-C', '-tt', '-vvv', '-o',
>> 'ControlMaster=auto', '-o', 'ControlPersist=60s', '-o',
>> 'ControlPath=/home/alex/.ansible/cp/ansible-ssh-%h-%p-%r', '-o',
>> 'Port=22', '-o', 'GSSAPIAuthentication=no', '-o',
>> 'PubkeyAuthentication=no', '-o', 'User=root', '-o', 'ConnectTimeout=10',
>> 'archpi', "/bin/sh -c 'mkdir -p
>> $HOME/.ansible/tmp/ansible-tmp-1419240488.89-153756188720618 && echo
>> $HOME/.ansible/tmp/ansible-tmp-1419240488.89-153756188720618'"]
>> fatal: [archpi] => {'msg': "AnsibleUndefinedVariable: One or more
>> undefined variables: list object has no element {'username': 'me', 'level':
>> 0, 'server': 'astraweb', 'connections': 2, 'host': 'ssl-eu.astraweb.com',
>> 'join_group': True, 'password': 'mypass', 'port': 563}", 'failed': True}
>> fatal: [archpi] => {'msg': "AnsibleUndefinedVariable: One or more
>> undefined variables: list object has no element {'username': 'me', 'level':
>> 0, 'server': 'astraweb', 'connections': 2, 'host': 'ssl-eu.astraweb.com',
>> 'join_group': True, 'password': 'mypass', 'port': 563}", 'failed': True}
>>
>>
>> Thanks
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Ansible Project" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to [email protected].
>> To post to this group, send email to [email protected].
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/ansible-project/fab45645-6282-4da9-8125-e94a0b1b053c%40googlegroups.com
>> <https://groups.google.com/d/msgid/ansible-project/fab45645-6282-4da9-8125-e94a0b1b053c%40googlegroups.com?utm_medium=email&utm_source=footer>
>> .
>> For more options, visit https://groups.google.com/d/optout.
>>
>  --
> You received this message because you are subscribed to a topic in the
> Google Groups "Ansible Project" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/ansible-project/O0JSh_vDSwU/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
> [email protected].
> To post to this group, send email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/CAD8N0v85wMTXxn5%3DDULMB8R91pi0-7BWdb7VEW04kjjp0xz43g%40mail.gmail.com
> <https://groups.google.com/d/msgid/ansible-project/CAD8N0v85wMTXxn5%3DDULMB8R91pi0-7BWdb7VEW04kjjp0xz43g%40mail.gmail.com?utm_medium=email&utm_source=footer>
> .
>
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/CAB5pHaSWK9ci5PuOQK%2BCFs%3DWmmu%2BFdo9Jid%3D7riTGLmCf4Uv7g%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to