# Define hostname for group based on index+1

**URL:** <https://forum.ansible.com/t/define-hostname-for-group-based-on-index-1/36535>\
**Category:** Ansible Project\
**Created:** [April 15, 2022, 7:18am UTC](https://forum.ansible.com/t/define-hostname-for-group-based-on-index-1/36535 "2022-04-15T07:18:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dudu.c](https://avatars.discourse-cdn.com/v4/letter/d/278dde/32.png) [@dudu.c](https://forum.ansible.com/u/dudu.c)\
**Post date:** [April 15, 2022, 7:18am UTC](https://forum.ansible.com/t/define-hostname-for-group-based-on-index-1/36535/1 "2022-04-15T07:18:30Z")

</div>

Hi,

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

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [April 15, 2022, 8:58am UTC](https://forum.ansible.com/t/define-hostname-for-group-based-on-index-1/36535/2 "2022-04-15T08:58:11Z")

</div>

> Hi,
> 
> 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  
&nbsp;&nbsp;&nbsp;hostname:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name: "web-server-{{ groups['web-server'].index(inventory\_hostname) }}"

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![dudu.c](https://avatars.discourse-cdn.com/v4/letter/d/278dde/32.png) [@dudu.c](https://forum.ansible.com/u/dudu.c)\
**Post date:** [April 15, 2022, 10:45am UTC](https://forum.ansible.com/t/define-hostname-for-group-based-on-index-1/36535/3 "2022-04-15T10:45:43Z")

</div>

First of all thank you - it works.

But, can i spare the following definition ? **groups[‘web-server’]**  
Basically in the begging of the playbook I’m defining the group under hosts

- hosts: web-server

ב-יום שישי, 15 באפריל 2022 בשעה 11:58:31 UTC+3, [ra...@linuxia.de](mailto:ra...@linuxia.de) כתב/ה:

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [April 15, 2022, 10:55am UTC](https://forum.ansible.com/t/define-hostname-for-group-based-on-index-1/36535/4 "2022-04-15T10:55:51Z")

</div>

> First of all thank you - it works.
> 
> But, can i spare the following definition ? \*groups['web-server']\*  
> Basically in the begging of the playbook I'm defining the group under hosts
> 
> - hosts: web-server

You could try ansible\_play\_hosts instead.

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke
