Define hostname for group based on index+1

28 views
Skip to first unread message

dudu.c...@gmail.com

unread,
Apr 15, 2022, 3:18:30 AM4/15/22
to Ansible Project
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

Stefan Hornburg (Racke)

unread,
Apr 15, 2022, 4:58:31 AM4/15/22
to ansible...@googlegroups.com
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) }}"

Regards
Racke

>
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com <mailto:ansible-proje...@googlegroups.com>.
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/4d7a9bb7-6403-4ac9-b77f-4212fafc5680n%40googlegroups.com <https://groups.google.com/d/msgid/ansible-project/4d7a9bb7-6403-4ac9-b77f-4212fafc5680n%40googlegroups.com?utm_medium=email&utm_source=footer>.


--
Automation expert - Ansible and friends
Linux administrator & Debian maintainer
Perl Dancer & conference hopper

OpenPGP_signature

dudu.c...@gmail.com

unread,
Apr 15, 2022, 6:45:43 AM4/15/22
to Ansible Project
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 כתב/ה:

Stefan Hornburg (Racke)

unread,
Apr 15, 2022, 6:56:12 AM4/15/22
to ansible...@googlegroups.com
On 15/04/2022 12:45, dudu.c...@gmail.com wrote:
> 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
Racke

>
> ב-יום שישי, 15 באפריל 2022 בשעה 11:58:31 UTC+3, ra...@linuxia.de כתב/ה:
>
> On 15/04/2022 09:18, dudu.c...@gmail.com wrote:
> > 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
> hostname:
> name: "web-server-{{ groups['web-server'].index(inventory_hostname) }}"
>
> Regards
> Racke
>
> >
> > --
> > You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> > To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com <mailto:ansible-proje...@googlegroups.com>.
> > To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/4d7a9bb7-6403-4ac9-b77f-4212fafc5680n%40googlegroups.com <https://groups.google.com/d/msgid/ansible-project/4d7a9bb7-6403-4ac9-b77f-4212fafc5680n%40googlegroups.com> <https://groups.google.com/d/msgid/ansible-project/4d7a9bb7-6403-4ac9-b77f-4212fafc5680n%40googlegroups.com?utm_medium=email&utm_source=footer <https://groups.google.com/d/msgid/ansible-project/4d7a9bb7-6403-4ac9-b77f-4212fafc5680n%40googlegroups.com?utm_medium=email&utm_source=footer>>.
>
>
> --
> Automation expert - Ansible and friends
> Linux administrator & Debian maintainer
> Perl Dancer & conference hopper
>
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com <mailto:ansible-proje...@googlegroups.com>.
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/fe43e1e3-ea11-4626-8855-2039a1b786a4n%40googlegroups.com <https://groups.google.com/d/msgid/ansible-project/fe43e1e3-ea11-4626-8855-2039a1b786a4n%40googlegroups.com?utm_medium=email&utm_source=footer>.
OpenPGP_signature
Reply all
Reply to author
Forward
0 new messages