Mr.Coffee
Mr.Coffee

Reputation: 3876

Ansible - Get current loop count when iterating over hosts

It this possible to get current for loop counter when using ansible and iterating over hosts provided?

- name: Debug me
  hosts: [1.2.3.4, 1.3.4.5]
  user: root
  tasks:
      - debug: msg="{{ inventory_hostname }}"
      - debug: msg="{{ forloop.counter }}" # ??

Any help appreciated.

Upvotes: 2

Views: 2709

Answers (1)

Konstantin Suvorov
Konstantin Suvorov

Reputation: 68289

It is not actually a loop in ansible, but I think you want:

- name: Debug me
  hosts: [1.2.3.4, 1.3.4.5]
  user: root
  tasks:
      - debug: msg="{{ inventory_hostname }}"
      - debug: msg="{{ play_hosts.index(inventory_hostname) }}"

Upvotes: 4

Related Questions