# combining with\_nested: + with\_together: (incl. loop count limit)

**URL:** <https://forum.ansible.com/t/combining-with-nested-with-together-incl-loop-count-limit/17558>\
**Category:** Ansible Project\
**Created:** [February 18, 2015, 10:15pm UTC](https://forum.ansible.com/t/combining-with-nested-with-together-incl-loop-count-limit/17558 "2015-02-18T22:15:11Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nusenu1](https://avatars.discourse-cdn.com/v4/letter/n/db5fbb/32.png) [@Nusenu1](https://forum.ansible.com/u/Nusenu1)\
**Post date:** [February 18, 2015, 10:15pm UTC](https://forum.ansible.com/t/combining-with-nested-with-together-incl-loop-count-limit/17558/1 "2015-02-18T22:15:11Z")

</div>

Hi,

(this email was previously send to the incorrect list [1])

I've got 3 sets of data:

foo: [A, B, C, D, ...]  
bar: [1, 2]  
tra: [a, b]

foo should be combined with bar and tra in a nested loop, but bar and  
tra should loop together.

something like (not actually working):

- debug: msg="{{ item[0] }} {{ item[1][0] }} {{ item[1][1] }}  
&nbsp;&nbsp;with\_netsted:  
&nbsp;&nbsp;&nbsp;- foo  
&nbsp;&nbsp;&nbsp;- item  
&nbsp;&nbsp;&nbsp;with\_together:  
&nbsp;&nbsp;&nbsp;&nbsp;- bar  
&nbsp;&nbsp;&nbsp;&nbsp;- tra

Expected output:

A1a  
A2b  
B1a  
B2b  
C1a  
C2b  
...

How would I actually combine with\_nested: and with\_together?

In addition to that I will need a loop termination once it reached a  
certain amount of loop executions.  
On a simple loop I would use 'with\_indexed\_items:' and check the index  
against a given max value. How would that loop count limit be  
implemented in the loop described above?

thanks!  
Nusenu

[1] [https://groups.google.com/d/msg/ansible-devel/aLrH\_SC8HyY/FZPs7GanHysJ](https://groups.google.com/d/msg/ansible-devel/aLrH_SC8HyY/FZPs7GanHysJ)

---

<div class="post-metadata">

**Author:** ![Nusenu1](https://avatars.discourse-cdn.com/v4/letter/n/db5fbb/32.png) [@Nusenu1](https://forum.ansible.com/u/Nusenu1)\
**Post date:** [February 19, 2015, 6:25pm UTC](https://forum.ansible.com/t/combining-with-nested-with-together-incl-loop-count-limit/17558/2 "2015-02-19T18:25:50Z")

</div>

I got it working. I combined 'bar' and 'tra' in a dictionary and used  
that in with\_nested

Now I'm left with the question: How can I limit the amount of loops in  
a with\_nested loop?

---

<div class="post-metadata">

**Author:** ![Nusenu1](https://avatars.discourse-cdn.com/v4/letter/n/db5fbb/32.png) [@Nusenu1](https://forum.ansible.com/u/Nusenu1)\
**Post date:** [March 16, 2015, 8:29pm UTC](https://forum.ansible.com/t/combining-with-nested-with-together-incl-loop-count-limit/17558/3 "2015-03-16T20:29:35Z")

</div>

> How can I limit the amount of loops in a with\_nested loop?

I still didn't find a solution for this. Maybe someone can suggest  
alternative approaches if the use case is clear:

I've got two use cases where I would need an index counter within a  
with\_nested loop (or any other solution that gets the job done):

1) limit number of service instances based on available machine memory

basically something like:  
create process instances  
with\_nested:  
- set1  
- set2  
when: machine\_memory / loopIndex+1 \> minimalmemoryrequired

but there is no "loopIndex" that I could use..

2) assign unique TCP port to a service to prevent duplicate bindings

here is the ansible role where this would be useful for me:  
[https://github.com/nusenu/relayor/blob/master/tasks/configure.yml](https://github.com/nusenu/relayor/blob/master/tasks/configure.yml)

thanks,  
Nusenu
