Creating multiple json files with jinja2 loop

105 views
Skip to first unread message

alanp...@gmail.com

unread,
Dec 17, 2017, 10:59:19 PM12/17/17
to Ansible Project
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'] %}
{
"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:

---
- 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

Kai Stian Olstad

unread,
Dec 19, 2017, 12:54:08 AM12/19/17
to ansible...@googlegroups.com
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
> 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.

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

--
Kai Stian Olstad

alanp...@gmail.com

unread,
Dec 19, 2017, 6:16:06 PM12/19/17
to Ansible Project
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

alanp...@gmail.com

unread,
Dec 19, 2017, 6:40:35 PM12/19/17
to Ansible Project
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
Reply all
Reply to author
Forward
0 new messages