On 18.12.2017 04:59, [email protected] 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/66cbc0f791b4ad353672174e5ee39fd5%40olstad.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to