I have X amount of server inside a defined group (For example - web server)
I want to loop the server a define there hostname based on index+1
For example, if the group have 3 server , so server one should be "web-server1" , second , "web-server-2" etc.
The below didnt do the trick - since it is looping the sequence for each server
- hosts: web-server
remote_user: user
become: yes
tasks:
- name: Set a hostname
hostname:
name: web-server-{{ item }}
with\_sequence: count=3
Using a loop is futile here, as you found out. But you can do that with a bit Python:
- name: Set a hostname
hostname:
name: "web-server-{{ groups['web-server'].index(inventory_hostname) }}"