# is with\_nested the right approach here?

**URL:** <https://forum.ansible.com/t/is-with-nested-the-right-approach-here/23532>\
**Category:** Ansible Project\
**Created:** [January 23, 2017, 1:53pm UTC](https://forum.ansible.com/t/is-with-nested-the-right-approach-here/23532 "2017-01-23T13:53:01Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![raphael.bauduin](https://avatars.discourse-cdn.com/v4/letter/r/8c91f0/32.png) [@raphael.bauduin](https://forum.ansible.com/u/raphael.bauduin)\
**Post date:** [January 23, 2017, 1:53pm UTC](https://forum.ansible.com/t/is-with-nested-the-right-approach-here/23532/1 "2017-01-23T13:53:01Z")

</div>

Hi,

I’m writing a cookbook to deploy a service, of which multiple instances can run at the same time on the same host.  
Each instance requires 3 config files: config\_X.yml, config\_X.env, log\_X.conf, where X=instance number.  
I have a template for each config file.

I want the cookbook to set things up for 3 instances.  
Having read the doc, it seems I could go in this direction:

Define the 3 instances in an array variable called instances (each having an index member), and loop over it:

- include: lib/copy\_config\_files.yml  
with\_nested:
- "{{ instances }} "
- [“config\_X.yml”, “config\_X.env”, “log\_X.conf”]

and in the lib/copy\_config\_files.yml, loop over the config files:

- name: copy mode 1 config  
template: …  
when: mode == 1
- name: copy mode 2 config  
template: …  
when: mode == 2
- name: “Copy configs”  
template: src=templates/{{item[1]}} dest=/etc/{{item[1] | regex\_replace(‘X’,{{item[0].index}})}} owner=root group=root mode=0744

First question: is this the right approach?

Second: what’s the best solution to the conditional template for mode1 and mode2 configs? With this cookbook it is unnecessarily generated multiple times. Is the solution to extract these to a seperate fil included one level higher?

thanks in advance for any tips!

Raphaël

---

<div class="post-metadata">

**Author:** ![raphael.bauduin](https://avatars.discourse-cdn.com/v4/letter/r/8c91f0/32.png) [@raphael.bauduin](https://forum.ansible.com/u/raphael.bauduin)\
**Post date:** [January 24, 2017, 8:40am UTC](https://forum.ansible.com/t/is-with-nested-the-right-approach-here/23532/2 "2017-01-24T08:40:40Z")

</div>

Hello,

I tried this:

- include: lib/copy\_config\_files.yml  
with\_nested:
- "{{ instances }} "
- [“config\_X.yml”, “config\_X.env”, “log\_X.conf”]

with the included cookbook:

- name: “Copy configs”  
template: src=templates/{{item[1]}} dest=/etc/{{item[1] | regex\_replace(‘X’,{{item[0].  
index}})}} owner=root group=root mode=0744

but it seems item is unknown in the include tasks. Any idea what I’m doing wrong here?

Thanks

Raphaël

---

<div class="post-metadata">

**Author:** ![Johannes\_Kastl](https://avatars.discourse-cdn.com/v4/letter/j/bbe5ce/32.png) [@Johannes\_Kastl](https://forum.ansible.com/u/Johannes_Kastl)\
**Post date:** [January 24, 2017, 10:43am UTC](https://forum.ansible.com/t/is-with-nested-the-right-approach-here/23532/3 "2017-01-24T10:43:16Z")

</div>

cookbook is chef, ansible uses playbooks 😉

I think there is an error in your dest, you use double curly braces  
inside of double curly braces:

template:  
&nbsp;&nbsp;src: templates/{{item[1]}}  
&nbsp;&nbsp;dest: /etc/{{item[1] | regex\_replace('X',{{item[0]. \> index}})}}  
&nbsp;&nbsp;owner: root  
&nbsp;&nbsp;group: root  
&nbsp;&nbsp;mode: 0744

We had this recently, change the inner braces to single square ones:

dest: /etc/{{ item[1] | regex\_replace('X', [item[0]. \> index ] ) }}

Untested!

Johannes
