# Need help with Ansible loop

**URL:** <https://forum.ansible.com/t/need-help-with-ansible-loop/33795>\
**Category:** Ansible Project\
**Created:** [September 11, 2020, 9:18pm UTC](https://forum.ansible.com/t/need-help-with-ansible-loop/33795 "2020-09-11T21:18:26Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sky7](https://avatars.discourse-cdn.com/v4/letter/s/258eb7/32.png) [@Sky7](https://forum.ansible.com/u/Sky7)\
**Post date:** [September 11, 2020, 9:18pm UTC](https://forum.ansible.com/t/need-help-with-ansible-loop/33795/1 "2020-09-11T21:18:26Z")

</div>

Hi All,

I have following code to call another file find\_elements.yaml from my current playbook.

calling from my current playbook

- include: find\_elements.yaml  
loop:  
-“{{ vardata.L2.0 }}”  
-“{{ vardata.L2.1 }}”  
–“{{ vardata.L2.1 }}”  
loop\_control:  
loop\_var: testing

My question is when I run this code I get expected out where the loop get executed 3 times.  
However I want to put these 3 statements in a loop such as  
-“{{ vardata.L2.[some variable]}}”  
and increment counter until the condition is met which is 3 times if I have 3 elements in list , 10 times if I have 10 elements in list.  
However when I do this this get executed only once and no way I can find to increment the counter and run the loop multiple times.  
Not sure what will be the syntax for the variable inside the loop  
Any body can help.

Thanks  
Vikas.

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [September 12, 2020, 8:28am UTC](https://forum.ansible.com/t/need-help-with-ansible-loop/33795/2 "2020-09-12T08:28:55Z")

</div>

> Hi All,
> 
> I have following code to call another file find\_elements.yaml from my current playbook.
> 
> calling from my current playbook  
> - include: find\_elements.yaml  
> loop:  
> -"{{ vardata.L2.0 }}"  
> -"{{ vardata.L2.1 }}"  
> --"{{ vardata.L2.1 }}"  
> loop\_control:  
> loop\_var: testing
> 
> My question is when I run this code I get expected out where the loop get executed 3 times.  
> However I want to put these 3 statements in a loop such as  
> -"{{ vardata.L2.[some variable]}}"  
> and increment counter until the condition is met which is 3 times if I have 3 elements in list , 10 times if I have 10  
> elements in list.  
> However when I do this this get executed only once and no way I can find to increment the counter and run the loop  
> multiple times.  
> Not sure what will be the syntax for the variable inside the loop  
> Any body can help.

What about sharing the real playbook and explain why you need this loop setup? So far I can not see  
the logic in your example and your description.

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![Sky7](https://avatars.discourse-cdn.com/v4/letter/s/258eb7/32.png) [@Sky7](https://forum.ansible.com/u/Sky7)\
**Post date:** [September 12, 2020, 11:52am UTC](https://forum.ansible.com/t/need-help-with-ansible-loop/33795/3 "2020-09-12T11:52:55Z")

</div>

Sure,

So i have excel file from where I am reading the file and populating the variables files called iterate.yaml which puts it in dictionary of list with each excel row values as a list of elements

Iterate.yaml

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [September 13, 2020, 6:46am UTC](https://forum.ansible.com/t/need-help-with-ansible-loop/33795/4 "2020-09-13T06:46:29Z")

</div>

> Sure,
> 
> So i have excel file from where I am reading the file and populating the variables files called iterate.yaml which puts  
> it in dictionary of list with each excel row values as a list of elements
> 
> Iterate.yaml  
> --  
> data:  
> - Name: person1  
> Age:20  
> description: test1  
> - Name: person2  
> Age:30  
> description: test2  
> - Name: person3  
> Age:40  
> description: test3

It looks to me that your playbook is way too complicated.

Getting to your output below should be as simple as follows (regardless of the number entries in data):

- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: |  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{{ item.Name }}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{{ item.Age }}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{{ item.description }}  
&nbsp;&nbsp;&nbsp;with\_items: "{{ vardata.data }}"

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![Sky7](https://avatars.discourse-cdn.com/v4/letter/s/258eb7/32.png) [@Sky7](https://forum.ansible.com/u/Sky7)\
**Post date:** [September 16, 2020, 2:03am UTC](https://forum.ansible.com/t/need-help-with-ansible-loop/33795/5 "2020-09-16T02:03:36Z")

</div>

Thanks a lot as this fixed the issue.

Regards  
Vikas.
