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




On Tuesday, December 19, 2017 at 4:54:08 PM UTC+11, Kai Stian Olstad wrote:
>
> On 18.12.2017 04:59, [email protected] <javascript:> wrote: 
> > 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: 
>
> 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 
>

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/de2cc835-1957-486f-8dbc-9206ffb8387c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to