# Templates & looping on json data structure

**URL:** <https://forum.ansible.com/t/templates-looping-on-json-data-structure/13760>\
**Category:** Ansible Project\
**Created:** [October 24, 2013, 9:19am UTC](https://forum.ansible.com/t/templates-looping-on-json-data-structure/13760 "2013-10-24T09:19:53Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Smain\_Kahlouch](https://avatars.discourse-cdn.com/v4/letter/s/ee59a6/32.png) [@Smain\_Kahlouch](https://forum.ansible.com/u/Smain_Kahlouch)\
**Post date:** [October 24, 2013, 9:19am UTC](https://forum.ansible.com/t/templates-looping-on-json-data-structure/13760/1 "2013-10-24T09:19:53Z")

</div>

Hello,

I have a data structure gathered from a postgres db (external inventory) with a list of json data structures as follows :

[  
{‘name’: conf1, ‘myvar1’: 1, ‘myvar2’: blabla},  
{‘name’: conf2, ‘myvar1’: 44, ‘myvar2’: hello}  
]

And i would like to use a kind of loop to create a file like :

[conf1]  
param1 = 1  
param2 = blabla

[conf2]  
param1 = 44  
param2 = hello

I’ve read in the ansible  
"  
ansible allows Jinja2 loops and conditionals in templates, but in playbooks, we do not use them. Ansible templates are pure machine-parseable YAML. This is an rather important feature as it means it is possible to code-generate pieces of files, or to have other ecosystem tools read Ansible files. Not everyone will need this but it can unlock possibilities.

"

Is there a way to perform that kind of loops please ?

Regards,  
Smana

---

<div class="post-metadata">

**Author:** ![Smain\_Kahlouch](https://avatars.discourse-cdn.com/v4/letter/s/ee59a6/32.png) [@Smain\_Kahlouch](https://forum.ansible.com/u/Smain_Kahlouch)\
**Post date:** [October 24, 2013, 1:58pm UTC](https://forum.ansible.com/t/templates-looping-on-json-data-structure/13760/2 "2013-10-24T13:58:31Z")

</div>

my current script generates this output :

{  
“databases”: {  
“hosts”: ,  
“vars”: {}  
},  
“webservers”: {  
“hosts”: [  
“10.18.2.1”,  
“10.18.2.2”,  
],  
“vars”: {}  
},  
“Site”: {  
“children”: [  
“databases”,  
“webservers”  
],  
“vars”: [  
{  
“id”: 4912,  
“path”: “test1.mp3”,  
}

{  
“id”: 4913,  
“path”: “test2.mp3”,  
}

]  
}  
}

But i got this error when executing ansible :

/etc/ansible/services/mysite 0 # ansible all -i ./inventory.py

Traceback (most recent call last):  
File “/usr/bin/ansible”, line 157, in   
(runner, results) = cli.run(options, args)  
File “/usr/bin/ansible”, line 79, in run  
inventory\_manager = inventory.Inventory(options.inventory)  
File “/usr/lib/python2.7/dist-packages/ansible/inventory/ **init**.py”, line 101, in **init**  
self.parser = InventoryScript(filename=host\_list)  
File “/usr/lib/python2.7/dist-packages/ansible/inventory/script.py”, line 47, in **init**  
self.groups = self.\_parse(stderr)  
File “/usr/lib/python2.7/dist-packages/ansible/inventory/script.py”, line 92, in \_parse  
for k, v in data[‘vars’].iteritems():  
AttributeError: ‘list’ object has no attribute ‘iteritems’

I need to iterate on a list of several variables.  
Do you think that’s possible ?

Regards,  
Smana

---

<div class="post-metadata">

**Author:** ![Smain\_Kahlouch](https://avatars.discourse-cdn.com/v4/letter/s/ee59a6/32.png) [@Smain\_Kahlouch](https://forum.ansible.com/u/Smain_Kahlouch)\
**Post date:** [October 25, 2013, 11:11am UTC](https://forum.ansible.com/t/templates-looping-on-json-data-structure/13760/3 "2013-10-25T11:11:30Z")

</div>

Any idea please ?

Regards,  
Smana

---

<div class="post-metadata">

**Author:** ![Kesten\_Broughton2](https://avatars.discourse-cdn.com/v4/letter/k/ecc23a/32.png) [@Kesten\_Broughton2](https://forum.ansible.com/u/Kesten_Broughton2)\
**Post date:** [October 25, 2013, 5:03pm UTC](https://forum.ansible.com/t/templates-looping-on-json-data-structure/13760/4 "2013-10-25T17:03:54Z")

</div>

Smain,

Although full jinja2 templating is not available in tasks/playbooks, other features like pipes/filters are.Try piping your datastructure with to\_json like

- name: do a thing  
copy: src=“{{item}}” dest=/foo  
with\_items: {{output | to\_json}}

I used something like this as well, where \_requested\_hosts was a dict passed in to a task like  
[main.yml]

- include: { playbook.yml, \_requested\_hosts: {{some\_dict\_defined\_in\_vars\_main.yml}} }

[playbook.yml]

- debug: msg=“{{\_requested\_hosts | to\_json | capture(requested\_hosts)}}”

The problem is that passing structures between playbook files seems to serialize it, and this deserializes it.

kesten

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [October 25, 2013, 11:49pm UTC](https://forum.ansible.com/t/templates-looping-on-json-data-structure/13760/5 "2013-10-25T23:49:02Z")

</div>

I don’t endorse the above suggestion by Kesten, and I’m pretty sure it’s not going to work 🙂

You have a list data structure so iteritems isn’t a method on a list.

If you just want to generate the config file I would do this as a template module:

(WARNING: untested general idea)

{% for entry in foo %}  
{% for (k,v) in entry.iteritems() %}  
{% if k == ‘name’ %}  
[{{ v }}]  
{% endif %}  
{% endfor %}

{% for (k,v) in entry.iteritems() %}  
{% if k != ‘name’ %}  
{{ k }}={{ v }}  
{% endif %}  
{% endfor %}

{% endfor %}

- template: src=foo.j2 dest=wherever.out

Note that I’m answering your first question and not your second – your variable structures are different in each.

---

<div class="post-metadata">

**Author:** ![Smain\_Kahlouch](https://avatars.discourse-cdn.com/v4/letter/s/ee59a6/32.png) [@Smain\_Kahlouch](https://forum.ansible.com/u/Smain_Kahlouch)\
**Post date:** [October 29, 2013, 9:06am UTC](https://forum.ansible.com/t/templates-looping-on-json-data-structure/13760/6 "2013-10-29T09:06:29Z")

</div>

Hello all,

For information i managed to do what i wanted.  
i just changed my list into a dictionnary :

mylist = [{‘name’: conf1, ‘myvar1’: 1, ‘myvar2’: blabla}, {‘name’: conf2, ‘myvar1’: 44, ‘myvar2’: hello}]  
mydict = { mylist }

then i used this dictionnary in my playbook as follows :

- name: foo configuration  
template: src=foo.cfg dest=/etc/foo/{{ item[‘name’] }}.sh  
with\_items: $mydict

the “item” variable can also be use inside the template.

Thanks for your help.

Regards

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [October 30, 2013, 1:50am UTC](https://forum.ansible.com/t/templates-looping-on-json-data-structure/13760/7 "2013-10-30T01:50:44Z")

</div>

No need for the legacy variable here, BTW, just do this:

with\_items: mydict
