# with\_nested with range()

**URL:** <https://forum.ansible.com/t/with-nested-with-range/23283>\
**Category:** Ansible Project\
**Created:** [December 19, 2016, 1:47pm UTC](https://forum.ansible.com/t/with-nested-with-range/23283 "2016-12-19T13:47:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lukasz\_Gorski](https://avatars.discourse-cdn.com/v4/letter/l/5f9b8f/32.png) [@Lukasz\_Gorski](https://forum.ansible.com/u/Lukasz_Gorski)\
**Post date:** [December 19, 2016, 1:47pm UTC](https://forum.ansible.com/t/with-nested-with-range/23283/1 "2016-12-19T13:47:57Z")

</div>

Hi,

I need to create a nested loop referencing a variable from the outer loop. Basically something along the lines of:

hosts = [{‘name’: ‘first’, ‘limit’: 3}, {‘name’: ‘second’, ‘limit’: 5}]  
for host in hosts:  
for i in range(1, host[‘limit’] + 1):  
print(‘%s-%d’ % (host[‘name’], i))

For all I see it’s not quite possible using with\_nested or with\_subelements (how do I refer to the item coming from the first loop?). I cannot enumerate all the items, they are simple int values, but their numer may be quite large.

Is there something to the built-in loops I am missing which could possibly help me using range() with limit coming from a dictionary from the first loop/data set?

Thanks in advance for any tips.

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [December 19, 2016, 2:20pm UTC](https://forum.ansible.com/t/with-nested-with-range/23283/2 "2016-12-19T14:20:58Z")

</div>

Check out loop\_control  
[https://docs.ansible.com/ansible/playbooks\_loops.html#loop-control](https://docs.ansible.com/ansible/playbooks_loops.html#loop-control)

hosts.yml

---

<div class="post-metadata">

**Author:** ![Lukasz\_Gorski](https://avatars.discourse-cdn.com/v4/letter/l/5f9b8f/32.png) [@Lukasz\_Gorski](https://forum.ansible.com/u/Lukasz_Gorski)\
**Post date:** [December 19, 2016, 3:13pm UTC](https://forum.ansible.com/t/with-nested-with-range/23283/3 "2016-12-19T15:13:11Z")

</div>

Thank you! It gets the job done nicely indeed.

Best regards,
