Iterate Hash in template.

118 views
Skip to first unread message

David Blackburn

unread,
Jan 25, 2016, 11:23:02 PM1/25/16
to Ansible Project
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

David Blackburn

unread,
Jan 26, 2016, 12:50:29 AM1/26/16
to Ansible Project
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 %}

Brian Coca

unread,
Jan 26, 2016, 9:57:54 AM1/26/16
to Ansible Project
its correct way to reference most vars


--
Brian Coca

David Blackburn

unread,
Jan 26, 2016, 11:22:14 AM1/26/16
to Ansible Project
Thanks for clarifying this Brian.

Dave
Reply all
Reply to author
Forward
0 new messages