2016-07-28 111 views
1

當使用ansible和迭代主機提供的循環計數器時,可以獲得當前的循環計數器嗎?Ansible - 迭代主機時獲取當前循環計數

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

任何幫助表示讚賞。

回答

3

它實際上不是ansible一個循環,但我想你想:

- 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) }}" 
+0

真棒,工程。 –

+0

@ Mr.Coffee好趕上!固定。 –