# Creating multiple json files with jinja2 loop

**URL:** <https://forum.ansible.com/t/creating-multiple-json-files-with-jinja2-loop/26316>\
**Category:** Ansible Project\
**Created:** [December 18, 2017, 3:59am UTC](https://forum.ansible.com/t/creating-multiple-json-files-with-jinja2-loop/26316 "2017-12-18T03:59:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![alanpassel7](https://avatars.discourse-cdn.com/v4/letter/a/848f3c/32.png) [@alanpassel7](https://forum.ansible.com/u/alanpassel7)\
**Post date:** [December 18, 2017, 3:59am UTC](https://forum.ansible.com/t/creating-multiple-json-files-with-jinja2-loop/26316/1 "2017-12-18T03:59:19Z")

</div>

Hi,

I’m trying to create a batch of json files, that will pull values from an inventory group.

Ideally, what I would like to have happen is 10 different files are created (the inventory group has 10 nodes in it), using both the position of the node in the inventory list and the fqdn of the node as the values that are populated into these json files.

At this point, what I’m not sure how to do it create 10 separate files (I can create one big one), which uses the aforementioned values.

My template (this is what generates the one big file:

```auto
{% for node in groups['yarn-nms'] %}
{
    "instanceId": "{{ loop.index }}",
    "name": "yarn-nm",
    "cluster": "stg",
    "image": "{{ image }}",
    "instance": {
      "cpu": 24,
      "mem": 80000
    },
    "constraints": [["hostname", "CLUSTER", "{{ node }}"]]
}
{% endfor %}

```

I tried stuffing that loop into the task that creates the json files:

`

```auto
---
- name: Make directory for json blobs
  file:
    path: ~/blobs
    state: directory

- name: Create json blobs
  template:
    src: pin-nm.json.j2
    dest: ~/blobs/pin-nm{% for node in groups['yarn-nms'] %}{{ loop.index }}{% endfor %}.json

```

`

but instead of giving me 10 different files, it gave me one file named pin-nm12345678910.json. And not surprisingly, the template didn’t inherit any values.

Can anyone offer any suggestions on how to create the 10 separate files, with the values passed through to the templates? Or even just how to create a loop that creates the 10 different files - I’m pretty sure I could figure it out from there.

Thanks,

AP

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [December 19, 2017, 5:53am UTC](https://forum.ansible.com/t/creating-multiple-json-files-with-jinja2-loop/26316/2 "2017-12-19T05:53:51Z")

</div>

> Hi,
> 
> I'm trying to create a batch of json files, that will pull values from an  
> inventory group.
> 
> Ideally, what I would like to have happen is 10 different files are created  
> (the inventory group has 10 nodes in it), using both the position of the  
> node in the inventory list and the fqdn of the node as the values that are  
> populated into these json files.
> 
> At this point, what I'm not sure how to do it create 10 separate files (I  
> can create one big one), which uses the aforementioned values.
> 
> My template (this is what generates the one big file:
> 
> {% for node in groups['yarn-nms'] %}  
> {  
> &nbsp;&nbsp;&nbsp;&nbsp;"instanceId": "{{ loop.index }}",  
> &nbsp;&nbsp;&nbsp;&nbsp;"name": "yarn-nm",  
> &nbsp;&nbsp;&nbsp;&nbsp;"cluster": "stg",  
> &nbsp;&nbsp;&nbsp;&nbsp;"image": "{{ image }}",  
> &nbsp;&nbsp;&nbsp;&nbsp;"instance": {  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"cpu": 24,  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"mem": 80000  
> &nbsp;&nbsp;&nbsp;&nbsp;},  
> &nbsp;&nbsp;&nbsp;&nbsp;"constraints": [["hostname", "CLUSTER", "{{ node }}"]]  
> }  
> {% endfor %}
> 
> I tried stuffing that loop into the task that creates the json files:

One task, one file. If you need 10 files you need to loop the task and the json in the template.

> ---  
> - name: Make directory for json blobs  
> &nbsp;&nbsp;file:  
> &nbsp;&nbsp;&nbsp;&nbsp;path: ~/blobs  
> &nbsp;&nbsp;&nbsp;&nbsp;state: directory
> 
> - name: Create json blobs  
> &nbsp;&nbsp;template:  
> &nbsp;&nbsp;&nbsp;&nbsp;src: pin-nm.json.j2  
> &nbsp;&nbsp;&nbsp;&nbsp;dest: ~/blobs/pin-nm{% for node in groups['yarn-nms'] %}{{  
> loop.index }}{% endfor %}.json
> 
> but instead of giving me 10 different files, it gave me one file named  
> pin-nm12345678910.json. And not surprisingly, the template didn't inherit  
> any values.

Ansible is just going to create one file as you have seen, and the template loop for dest is fully expanded.

> Can anyone offer any suggestions on how to create the 10 separate files,  
> with the values passed through to the templates? Or even just how to create  
> a loop that creates the 10 different files - I'm pretty sure I could figure  
> it out from there.

Remove the loop in the template file and create the loop on the task, you can use with\_indexed\_items[1] for this

[1] [http://docs.ansible.com/ansible/latest/playbooks\_loops.html#looping-over-a-list-with-an-index](http://docs.ansible.com/ansible/latest/playbooks_loops.html#looping-over-a-list-with-an-index)

---

<div class="post-metadata">

**Author:** ![alanpassel7](https://avatars.discourse-cdn.com/v4/letter/a/848f3c/32.png) [@alanpassel7](https://forum.ansible.com/u/alanpassel7)\
**Post date:** [December 19, 2017, 11:16pm UTC](https://forum.ansible.com/t/creating-multiple-json-files-with-jinja2-loop/26316/3 "2017-12-19T23:16:06Z")

</div>

Hi Kai, and thank you so much for the help.

So using the method you recommended, I’m now able to get my 10 files, using the following task:

`

- name: Create json blobs  
template:  
src: pin-nm.json.j2  
dest: ~/blobs/pin-nm{{ item.index }}.json  
with\_indexed\_items:
- “{{ groups[‘external\_yarn\_bare\_metal\_nodes’] }}”

`

Frustratingly, though, that gives me ten files named thusly:

pin-nm\<built-in method index of list object at 0x7fa45b58dc20\>.json

Any idea how to pull out the index number, or perhaps an idea of where I can find either the documentation for the functions available to me using with\_indexed\_items, or the source code? I hate to have to ask questions like these, but I’m just not making the leap, here.

Thank you,

ap

---

<div class="post-metadata">

**Author:** ![alanpassel7](https://avatars.discourse-cdn.com/v4/letter/a/848f3c/32.png) [@alanpassel7](https://forum.ansible.com/u/alanpassel7)\
**Post date:** [December 19, 2017, 11:40pm UTC](https://forum.ansible.com/t/creating-multiple-json-files-with-jinja2-loop/26316/4 "2017-12-19T23:40:35Z")

</div>

Nevermind, I figured it out - turns out item.0 and item.1 weren’t what I thought they were. Thanks very much, Kai!

Thank you,

ap
