Ansible: iterate over hash in template

Matthew Joseph Martin
1 min readApr 1, 2015

--

How to iterate over a hash inside Ansible Jinja templates

Ansible Jinja gotcha

If you iterate over a hash as you might think, you’ll see this error:

fatal: [host] => {‘msg’: ‘ValueError: too many values to unpack’, ‘failed’: True}

Solution

You need to use the .iteritems() method:

Update: If you’re using Python3 use dict.items() instead of dict.iteritems()(credit to Olaf Alders)

--

--

Matthew Joseph Martin

Founder of blossomfinance.com, entrepreneur, learner, bitcoin fan, student of languages, constitutionalist, geolibertarian, practicing Muslim