iterate counter within loop

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 :frowning:

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…

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 :frowning:

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
         Racke

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 :frowning:

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
         Racke