2

Say I have:

rn2: [x,y,z]

and in a task I want to output the current index of the variable it is currently using

- name: Output
  shell: echo "{{ item|int }}"
  loop: "{{rn2}}"

My expected is to output

0
1
2

but it only outputs

0
0
0

My goal is to increment the number as it loops through the list.

user13539846
  • 425
  • 2
  • 5
  • 13

1 Answers1

11

As per documentation, we can enable additional looping options with loop_control. index_var is one of the options which enables us to access the current position in the list.

Example:

- name: Output
  debug:
    var: my_idx
  loop: "{{ rn2 }}"
  loop_control:
    index_var: my_idx
seshadri_c
  • 6,906
  • 2
  • 10
  • 24