# Issue with loop index number.

**URL:** <https://forum.ansible.com/t/issue-with-loop-index-number/33647>\
**Category:** Ansible Project\
**Created:** [August 20, 2020, 1:24pm UTC](https://forum.ansible.com/t/issue-with-loop-index-number/33647 "2020-08-20T13:24:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![soumya\_dutta](https://avatars.discourse-cdn.com/v4/letter/s/3ab097/32.png) [@soumya\_dutta](https://forum.ansible.com/u/soumya_dutta)\
**Post date:** [August 20, 2020, 1:24pm UTC](https://forum.ansible.com/t/issue-with-loop-index-number/33647/1 "2020-08-20T13:24:54Z")

</div>

Hi,

I have a situation where I have few servers and there are different artifacts or rpms are installed. I am getting all data for all servers in a clubbed list and based on that I am creating files for each server and the respective rpms data are posted to that file.

So suppose server 1 has 5 rpms and server 2 has 2 rpms and file I will create it should look like this.  
File1:  
artifact:  
1:  
artifact\_name:  
artifact\_version:  
2:  
artifact\_name:  
artifact\_version:  
Similarly till 5 and similarly on the other file there will be two entries under artifact as 1 & 2.

But on using loop index for file1 i get 5 numbers but for file 2 instead of starting the number with 1 it starts with 6.

Help on this is really appreciated.

Regards,  
Soumya

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [August 20, 2020, 1:30pm UTC](https://forum.ansible.com/t/issue-with-loop-index-number/33647/2 "2020-08-20T13:30:04Z")

</div>

Can you show us your actual code

---

<div class="post-metadata">

**Author:** ![soumya\_dutta](https://avatars.discourse-cdn.com/v4/letter/s/3ab097/32.png) [@soumya\_dutta](https://forum.ansible.com/u/soumya_dutta)\
**Post date:** [August 20, 2020, 2:12pm UTC](https://forum.ansible.com/t/issue-with-loop-index-number/33647/3 "2020-08-20T14:12:19Z")

</div>

I can show a dummy thing:

So i am using blockinfile module.

blockinfile:  
path: /tmp/item.ci\_name/vars  
block: |2  
{{ ansible\_loop.index}}:  
artifact\_name: {{ item.1.artifact\_id}}  
artifact\_version: {{ item.1.artifact\_version}}  
insertafter: “artifact:”  
with\_subelements:

- “{{ custom.results}}”
- json.result.artifacts

Regards,  
Soumya

---

<div class="post-metadata">

**Author:** ![soumya\_dutta](https://avatars.discourse-cdn.com/v4/letter/s/3ab097/32.png) [@soumya\_dutta](https://forum.ansible.com/u/soumya_dutta)\
**Post date:** [August 22, 2020, 3:29am UTC](https://forum.ansible.com/t/issue-with-loop-index-number/33647/4 "2020-08-22T03:29:31Z")

</div>

Can someone help me with the below query?

Regards,  
Soumya

---

<div class="post-metadata">

**Author:** ![soumya\_dutta](https://avatars.discourse-cdn.com/v4/letter/s/3ab097/32.png) [@soumya\_dutta](https://forum.ansible.com/u/soumya_dutta)\
**Post date:** [August 23, 2020, 3:41pm UTC](https://forum.ansible.com/t/issue-with-loop-index-number/33647/5 "2020-08-23T15:41:05Z")

</div>

Can somebody please help with the below query.

Regards,  
Soumya
