I managed to achieve this by using "hostvar" in my template is this the 
correct way to be referencing hashes in templates?

Thanks
Dave

{% for servers in hostvars[inventory_hostname][SITE]['ntp'] %}
server {{servers}}
{% endfor %}

On Monday, 25 January 2016 20:23:02 UTC-8, David Blackburn wrote:
>
> I am trying to iterate over a hash of hash of a list, but I do not seem to 
> be having much luck, I have setup a group variable which I select using a 
> variable, which appears to be working correctly as per the debug, but once 
> in the template I don't seem to be able to iterate over it no matter what 
> permutations I use.
>
> Any advice on this would be great.
> Thanks
> Dave
>
> *cat group_vars/all*
> boston:
>     nameservers: ['10.0.0.1','10.0.0.2']
>     ntp: ['pool1.ntp.org','pool2.ntp.org']
> losangeles:
>     nameservers: ['10.0.1.1','10.0.1.2']
>     ntp: ['pool1.ntp.org','pool2.ntp.org']
>
>
> *cat main.yml*
> ---
> -   name: test template
>     hosts: all
>     vars:
>       *  SITE: 'boston'*
>     tasks:
>     - debug: var={{SITE}}
>
>     - name: Template Test
>       template: src=ntp.conf.j2 dest=/tmp/ntp.conf
>
> *cat ntp.conf.j2*
> driftfile /var/lib/ntp/drift
> restrict default kod nomodify notrap nopeer noquery
> restrict -6 default kod nomodify notrap nopeer noquery
> restrict 127.0.0.1
> restrict -6 ::1
>
> {% for key in SITE[ntp][0] %}
>
> {{key}}
>
> {% endfor %}
>
> includefile /etc/ntp/crypto/pw
> keys /etc/ntp/keys
>
> *ansible-playbook -i hosts main.yml   --connection=local*
>
> PLAY [test template] 
> **********************************************************
>
> GATHERING FACTS 
> ***************************************************************
> ok: [localhost]
>
> TASK: [debug var={{SITE}}] 
> ****************************************************
> ok: [localhost] => {
>     "var": {
>         "boston": {
>             "nameservers": [
>                 "10.0.0.1",
>                 "10.0.0.2"
>             ],
>             "ntp": [
>                 "pool1.ntp.org",
>                 "pool2.ntp.org"
>             ]
>         }
>     }
> }
>
> TASK: [Template Test] 
> *********************************************************
> fatal: [localhost] => {'msg': 'AnsibleUndefinedVariable: One or more 
> undefined variables: unicode object has no element Undefined', 'failed': 
> True}
> fatal: [localhost] => {'msg': 'AnsibleUndefinedVariable: One or more 
> undefined variables: unicode object has no element Undefined', 'failed': 
> True}
>
> FATAL: all hosts have already failed -- aborting
>
> PLAY RECAP 
> ********************************************************************
>            to retry, use: --limit @/Users/dave/main.retry
>
> localhost                  : ok=2    changed=0    unreachable=1    failed=0
>

-- 
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/75f6524d-f343-4ebc-b139-a514b7871c8b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to