# iterate counter within loop

**URL:** <https://forum.ansible.com/t/iterate-counter-within-loop/33754>\
**Category:** Ansible Project\
**Created:** [September 7, 2020, 11:36am UTC](https://forum.ansible.com/t/iterate-counter-within-loop/33754 "2020-09-07T11:36:51Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![David\_Bush1](https://avatars.discourse-cdn.com/v4/letter/d/bc8723/32.png) [@David\_Bush1](https://forum.ansible.com/u/David_Bush1)\
**Post date:** [September 7, 2020, 11:36am UTC](https://forum.ansible.com/t/iterate-counter-within-loop/33754/1 "2020-09-07T11:36:51Z")

</div>

Hi All,

I am after some advise please as I am struggling to think of a method to do this.

I have a task in a role that needs to create a file on each target host in a host-group that should look like this:

server.0=\<host1\_ip\>:1234  
server.1=\<host2\_ip\>:1234  
server.2=\<host3\_ip\>:1234

I can get the host IPs to work using `groups[<group_name>]`.  
I am stuck on how to iterate the server.x counter in each line.

I have tried using something like this:

- name: create dynamic config file  
lineinfile:  
path: \<file\_path\>  
line: server.{{ item.hostIndex }}={{ item.hostIp }}:2888:3888;2181  
with\_items:
- { hostIndex: “{{ [play\_hosts.index(inventory\_hostname)] }}”, hostIp: “{{ groups[\<group\_name\>] }}” }

but of course it only uses the index from the host running the play ☹

What I would like to do is set a counter for say 0, then iterate that on each loop for hosts in `groups[<group_name>]`.

I could not see a way to do this within a task - is this possible and if so what would be the method to use.

Thanks in advance…

---

<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 8, 2020, 3:15pm UTC](https://forum.ansible.com/t/iterate-counter-within-loop/33754/2 "2020-09-08T15:15:21Z")

</div>

> Hi All,
> 
> I am after some advise please as I am struggling to think of a method to do this.
> 
> I have a task in a role that needs to create a file on each target host in a host-group that should look like this:
> 
> server.0=\<host1\_ip\>:1234  
> server.1=\<host2\_ip\>:1234  
> server.2=\<host3\_ip\>:1234
> 
> I can get the host IPs to work using `groups[\<group_name\>]`.  
> I am stuck on how to iterate the server.x counter in each line.
> 
> I have tried using something like this:
> 
> - name: create dynamic config file  
> lineinfile:  
> path: \<file\_path\>  
> line: server.{{ item.hostIndex }}={{ item.hostIp }}:2888:3888;2181  
> with\_items:  
> - { hostIndex: "{{ [play\_hosts.index(inventory\_hostname)] }}", hostIp: "{{ groups[\<group\_name\>] }}" }
> 
> but of course it only uses the index from the host running the play ☹
> 
> What I would like to do is set a counter for say 0, then iterate that on each loop for hosts in `groups[\<group_name\>]`.
> 
> I could not see a way to do this within a task - is this possible and if so what would be the method to use.
> 
> Thanks in advance...

This look like a list of nodes in cluster, is that correct?

You can create a list as follows (replace all with your group name):

{% for host in groups['all'] %}  
server.{{ loop.index }}={{ hostvars[host]['ansible\_default\_ipv4']['address'] }}:1234  
{% endfor %}"

lineinfile is not appropriate (what happens when a node is removed ?), use blockinfile  
or template.

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

---

<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 8, 2020, 3:17pm UTC](https://forum.ansible.com/t/iterate-counter-within-loop/33754/3 "2020-09-08T15:17:20Z")

</div>

> > Hi All,
> > 
> > I am after some advise please as I am struggling to think of a method to do this.
> > 
> > I have a task in a role that needs to create a file on each target host in a host-group that should look like this:
> > 
> > server.0=\<host1\_ip\>:1234  
> > server.1=\<host2\_ip\>:1234  
> > server.2=\<host3\_ip\>:1234
> > 
> > I can get the host IPs to work using `groups[\<group_name\>]`.  
> > I am stuck on how to iterate the server.x counter in each line.
> > 
> > I have tried using something like this:
> > 
> > - name: create dynamic config file  
> > lineinfile:  
> > path: \<file\_path\>  
> > line: server.{{ item.hostIndex }}={{ item.hostIp }}:2888:3888;2181  
> > with\_items:  
> > - { hostIndex: "{{ [play\_hosts.index(inventory\_hostname)] }}", hostIp: "{{ groups[\<group\_name\>] }}" }
> > 
> > but of course it only uses the index from the host running the play ☹
> > 
> > What I would like to do is set a counter for say 0, then iterate that on each loop for hosts in `groups[\<group_name\>]`.
> > 
> > I could not see a way to do this within a task - is this possible and if so what would be the method to use.
> > 
> > Thanks in advance...
> 
> This look like a list of nodes in cluster, is that correct?
> 
> You can create a list as follows (replace all with your group name):
> 
> {% for host in groups['all'] %}  
> server.{{ loop.index }}={{ hostvars[host]['ansible\_default\_ipv4']['address'] }}:1234  
> {% endfor %}"
> 
> lineinfile is not appropriate (what happens when a node is removed ?), use blockinfile  
> or template.

Little correction:

Use {{ loop.index - 1 }} instead of {{ loop.index }}

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