# Secondary loop

**URL:** <https://forum.ansible.com/t/secondary-loop/12540>\
**Category:** Ansible Project\
**Created:** [March 20, 2013, 11:02am UTC](https://forum.ansible.com/t/secondary-loop/12540 "2013-03-20T11:02:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ben\_Hood](https://avatars.discourse-cdn.com/v4/letter/b/8c91f0/32.png) [@Ben\_Hood](https://forum.ansible.com/u/Ben_Hood)\
**Post date:** [March 20, 2013, 11:02am UTC](https://forum.ansible.com/t/secondary-loop/12540/1 "2013-03-20T11:02:22Z")

</div>

Hi,

I was wondering if there is an idiomatic way to have a secondary loop in a with\_items stanza.

I am trying to template the following directory structure:

/opt/my\_app/instance-1/foo.cfg  
/opt/my\_app/instance-1/bar.cfg  
/opt/my\_app/instance-1/baz.cfg

/opt/my\_app/instance-2/foo.cfg  
/opt/my\_app/instance-2/bar.cfg  
/opt/my\_app/instance-3/baz.cfg

…………

/opt/my\_app/instance-n/foo.cfg  
/opt/my\_app/instance-n/bar.cfg  
/opt/my\_app/instance-n/baz.cfg

So in my playbook to create the {foo.cfg,bar.cfg,baz.cfg} trio, I’ve got this fragment:

action: template src=templates/$item.j2 dest=/opt/my\_app/instance\_1/$item  
with\_items:

- foo.cfg
- bar.cfg
- baz.cfg

Is there a neat way that I could create an outer loop of 1…n that I could bind my instance\_$i subdirectory to?

Cheers,

Ben

---

<div class="post-metadata">

**Author:** ![Ben\_Hood](https://avatars.discourse-cdn.com/v4/letter/b/8c91f0/32.png) [@Ben\_Hood](https://forum.ansible.com/u/Ben_Hood)\
**Post date:** [March 20, 2013, 11:13am UTC](https://forum.ansible.com/t/secondary-loop/12540/2 "2013-03-20T11:13:14Z")

</div>

Sorry about the noise on the list, I’ve found that there is actually a solution for this:

[https://github.com/ansible/ansible/blob/devel/examples/playbooks/loop\_nested.yml](https://github.com/ansible/ansible/blob/devel/examples/playbooks/loop_nested.yml)

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [March 20, 2013, 12:37pm UTC](https://forum.ansible.com/t/secondary-loop/12540/3 "2013-03-20T12:37:44Z")

</div>

Yep, with\_nested is new as of last week!

Let me know if you have any questions about it, we typically document  
things in the official docs close to release time, so that will be in  
there shortly!
