I have an inventory file with two hosts defined as below
[testservers]
xx.xx.xx.106 ansible_ssh_user=johndoe
xx.xx.xx.138 ansible_ssh_user=johndoe
xx.xx.xx.141 ansible_ssh_user=johndoe
I want to use the sequence number of defined hosts inside the tasks.
Like for xx.xx.xx.106
I should get the sequence 1, for xx.xx.xx.141
I should get the sequence value as 3 since its the third entry.
How do I get this sequence number of host entry defined in inventory file without defining additional variables?
You can use standard Python's index method.
Replace strings with variables below:
debug:
msg: "{{ groups['testservers'].index('xx.xx.xx.141') + 1 }}"
(+1
because you explicitly asked for a sequence from 1 to 3, but index
starts with 0
)