# Nested loop with second loop depending on first item

**URL:** <https://forum.ansible.com/t/nested-loop-with-second-loop-depending-on-first-item/14124>\
**Category:** Ansible Project\
**Created:** [December 11, 2013, 8:53am UTC](https://forum.ansible.com/t/nested-loop-with-second-loop-depending-on-first-item/14124 "2013-12-11T08:53:14Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Guillaume\_Subiron](https://avatars.discourse-cdn.com/v4/letter/g/c77e96/32.png) [@Guillaume\_Subiron](https://forum.ansible.com/u/Guillaume_Subiron)\
**Post date:** [December 11, 2013, 8:53am UTC](https://forum.ansible.com/t/nested-loop-with-second-loop-depending-on-first-item/14124/1 "2013-12-11T08:53:14Z")

</div>

Hi,

I'm trying to do a special kind of nesting loop, using the item of the  
first loop in the second loop:

- name: Prepare OSDs  
&nbsp;&nbsp;shell: ceph-deploy osd prepare {{ item[0] }}:{{ item[1] }}  
&nbsp;&nbsp;with\_nested:  
&nbsp;&nbsp;&nbsp;&nbsp;- groups['ceph-ODSs']  
&nbsp;&nbsp;&nbsp;&nbsp;- hostvars[item[0]]['disks']

I'm not sure if this is possible, but I can't find any workaround.

---

<div class="post-metadata">

**Author:** ![Serge\_van\_Ginderacht](https://avatars.discourse-cdn.com/v4/letter/s/b782af/32.png) [@Serge\_van\_Ginderacht](https://forum.ansible.com/u/Serge_van_Ginderacht)\
**Post date:** [December 11, 2013, 10:18am UTC](https://forum.ansible.com/t/nested-loop-with-second-loop-depending-on-first-item/14124/2 "2013-12-11T10:18:28Z")

</div>

Have a look at the with\_subelements lookup plugin.

---

<div class="post-metadata">

**Author:** ![Guillaume\_Subiron](https://avatars.discourse-cdn.com/v4/letter/g/c77e96/32.png) [@Guillaume\_Subiron](https://forum.ansible.com/u/Guillaume_Subiron)\
**Post date:** [December 11, 2013, 10:40am UTC](https://forum.ansible.com/t/nested-loop-with-second-loop-depending-on-first-item/14124/3 "2013-12-11T10:40:08Z")

</div>

Le 13/12/11 11:18, Serge van Ginderachter claviotta :

> Have a look at the with\_subelements lookup plugin.

I know very well with\_subelements, but in this case I'm not trying to  
access a subelement in the first element, I'm trying to use the first  
element to access an entry in an other dictionary.

I don't think with\_subelements is well suited to this case. Do you  
have an example ?

---

<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:** [December 11, 2013, 1:05pm UTC](https://forum.ansible.com/t/nested-loop-with-second-loop-depending-on-first-item/14124/4 "2013-12-11T13:05:32Z")

</div>

Before we dive into a technical solution let me understand your use case and what you are modelling a bit better.

So groups[‘ceph-ODSs’] would be all machines in the ceph-ODSs group.

I’d probably just define a variable like “disks” on the group, but I’m unclear why that wouldn’t work in your case.

I could probably understand more if I could see how “disks” differs between hosts.

---

<div class="post-metadata">

**Author:** ![Guillaume\_Subiron](https://avatars.discourse-cdn.com/v4/letter/g/c77e96/32.png) [@Guillaume\_Subiron](https://forum.ansible.com/u/Guillaume_Subiron)\
**Post date:** [December 11, 2013, 1:21pm UTC](https://forum.ansible.com/t/nested-loop-with-second-loop-depending-on-first-item/14124/5 "2013-12-11T13:21:27Z")

</div>

Le 13/12/11 08:05, Michael DeHaan claviotta :

> Before we dive into a technical solution let me understand your use case  
> and what you are modelling a bit better.
> 
> So groups['ceph-ODSs'] would be all machines in the ceph-ODSs group.

That's right.

> I'd probably just define a variable like "disks" on the group, but I'm  
> unclear why that wouldn't work in your case.
> 
> I could probably understand more if I could see how "disks" differs between  
> hosts.

It's simple, my Ceph OSD (storage nodes) are all différents. Some  
contains 2 hard drives (sdb, sdc), some contains 10 (sdb, sdc, sdd…).  
"disks" is a list of hard drives, which is different from one host to  
another. ex:

&nbsp;&nbsp;inventory

osd0  
osd1

[ceph-OSDs]  
osd0  
osd1

&nbsp;&nbsp;host\_vars/osd0

disks:  
&nbsp;&nbsp;- sdb

&nbsp;&nbsp;host\_vars/osd1

disks:  
&nbsp;&nbsp;- sdb  
&nbsp;&nbsp;- sdc

In my nested loop, I need to loop over the Ceph Storage nodes and  
their hard drive. The hard drive list is an host variable (accessible  
by hostvars[osd0]['disks'], for instance).

With the example above, I want my playbook to do :

ceph\_deploy osd prepare osd0:sda  
ceph\_deploy osd prepare osd1:sda  
ceph\_deploy osd prepare osd1:sdb

---

<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:** [December 11, 2013, 1:24pm UTC](https://forum.ansible.com/t/nested-loop-with-second-loop-depending-on-first-item/14124/6 "2013-12-11T13:24:40Z")

</div>

I think you probably want this:

- shell: echo {{ item.0 }}-{{item.1 }}  
with\_together:
- groups[‘ceph-OSDs’]
- disks

this will print for the first host

osd0-sdb  
osd1-sdb

and for the second host

osd0-sdb  
osd0-sdc  
osd1-sdb  
osd1-sdc

Let me know if that works for you and if I’m missing something we’ll figure it out.

Thanks!

---

<div class="post-metadata">

**Author:** ![Guillaume\_Subiron](https://avatars.discourse-cdn.com/v4/letter/g/c77e96/32.png) [@Guillaume\_Subiron](https://forum.ansible.com/u/Guillaume_Subiron)\
**Post date:** [December 11, 2013, 1:53pm UTC](https://forum.ansible.com/t/nested-loop-with-second-loop-depending-on-first-item/14124/7 "2013-12-11T13:53:10Z")

</div>

Hum, this is not what I'm looking for, because my action is not  
executed on the ceph-OSDs, but on another host (a centralized admin  
node).  
In this playbook, I'm not doing anything on the ceph-OSDs.

What I need to do (only on my admin node) is :

- shell: echo {{ item.0 }}-{{ item.1 }}  
&nbsp;&nbsp;with\_nested:  
&nbsp;&nbsp;&nbsp;&nbsp;- groups['ceph-OSDs']  
&nbsp;&nbsp;&nbsp;&nbsp;- the disks of the current item in the "groups['ceph-OSDs']" loop

I want it to print (on the admin host) :

osd0-sdb  
osd1-sdb  
osd1-sdc

Do you understand the problem ? I don't see any workaround. This is a  
matter of syntax.

Le 13/12/11 08:24, Michael DeHaan claviotta :

---

<div class="post-metadata">

**Author:** ![Guillaume\_Subiron](https://avatars.discourse-cdn.com/v4/letter/g/c77e96/32.png) [@Guillaume\_Subiron](https://forum.ansible.com/u/Guillaume_Subiron)\
**Post date:** [December 11, 2013, 2:24pm UTC](https://forum.ansible.com/t/nested-loop-with-second-loop-depending-on-first-item/14124/8 "2013-12-11T14:24:37Z")

</div>

After reading nested.py and realizing it would never do what I wanted,  
I found a workaround.

On each ceph-ODSs :

- delegate\_to: "{{ ceph-admin }}"  
&nbsp;&nbsp;shell: echo {{ inventory\_hostname }}-{{ item }}  
&nbsp;&nbsp;with\_items: disks

Anyway, thank you very much for your help 🙂

Le 13/12/11 14:53, Guillaume Subiron claviotta :

---

<div class="post-metadata">

**Author:** ![Q11](https://avatars.discourse-cdn.com/v4/letter/q/46a35a/32.png) [@Q11](https://forum.ansible.com/u/Q11)\
**Post date:** [June 9, 2017, 2:57pm UTC](https://forum.ansible.com/t/nested-loop-with-second-loop-depending-on-first-item/14124/9 "2017-06-09T14:57:35Z")

</div>

Guillaume,

I have exactly the same problem with setting up OSD via ADM host and I was pulling my hair for 2 days.  
I don’t know how to thank you:)

---

<div class="post-metadata">

**Author:** ![saisumanth443](https://avatars.discourse-cdn.com/v4/letter/s/898d66/32.png) [@saisumanth443](https://forum.ansible.com/u/saisumanth443)\
**Post date:** [November 14, 2018, 8:05am UTC](https://forum.ansible.com/t/nested-loop-with-second-loop-depending-on-first-item/14124/10 "2018-11-14T08:05:31Z")

</div>

Hi I have seen ur resolutions in this page i.

I am also having the same issue with nested loops .  
my scenario is nothing but th\_\_e matrix multiplication of n\*n\_\_  
Can some one please suggest how to implement it using ansible loops along with adding conidition of count ++ and count –

---

<div class="post-metadata">

**Author:** ![j.r.hawkesworth](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@j.r.hawkesworth](https://forum.ansible.com/u/j.r.hawkesworth)\
**Post date:** [November 18, 2018, 10:29am UTC](https://forum.ansible.com/t/nested-loop-with-second-loop-depending-on-first-item/14124/11 "2018-11-18T10:29:34Z")

</div>

Hmm, would the cartesian lookup help you?

[https://docs.ansible.com/ansible/latest/plugins/lookup/cartesian.html](https://docs.ansible.com/ansible/latest/plugins/lookup/cartesian.html)

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [November 21, 2018, 6:47pm UTC](https://forum.ansible.com/t/nested-loop-with-second-loop-depending-on-first-item/14124/12 "2018-11-21T18:47:33Z")

</div>

actually use the product filter instead of cartesian lookup
