How to set hostname with running number

20 views
Skip to first unread message

dudu.c...@gmail.com

unread,
Nov 22, 2021, 3:00:35 AM11/22/21
to Ansible Project
Hi, 
In my inventory file i have 3 server, I need to set the hostname including prefix1-3

web1
web2
web3

Inventory fie:

[web]
192.168.0.47
192.168.0.48
192.168.0.49

Playbook

- hosts:
    - web
  become: true
  any_errors_fatal: true
  tasks:


    - name: update hostname
      hostname:
        name : "web ????"





Stefan Hornburg (Racke)

unread,
Nov 22, 2021, 3:19:04 AM11/22/21
to ansible...@googlegroups.com
On 22/11/2021 09:00, dudu.c...@gmail.com wrote:
> Hi,
> In my inventory file i have 3 server, I need to set the hostname including prefix1-3
>
> web1
> web2
> web3
>
> *Inventory fie:*
>
> [web]
> 192.168.0.47
> 192.168.0.48
> 192.168.0.49
>

Add the hostname to the inventory file:

web1 ansible_host=192.168.0.47

> *Playbook*
>
> - hosts:
>     - web
>   become: true
>   any_errors_fatal: true
>   tasks:
>
>
>     - name: update hostname
>       hostname:
>         name : "web ????"
>

Now you can use the inventory_hostname variable in your task:

- name: update hostname
hostname:
name: "{{ inventory_hostname }}"

Please note that might not survive reboots, e.g on Ubuntu I'm doing:

- name: Enable preserving hostname in Cloud configuration
lineinfile:
path: /etc/cloud/cloud.cfg
line: 'preserve_hostname: true'
regexp: '^preserve_hostname:'
tags: 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/2dd18239-c65d-4717-9d90-af4c2aba1a68n%40googlegroups.com <https://groups.google.com/d/msgid/ansible-project/2dd18239-c65d-4717-9d90-af4c2aba1a68n%40googlegroups.com?utm_medium=email&utm_source=footer>.


--
Ecommerce and Linux consulting + Perl and web application programming.
Debian and Sympa administration.


OpenPGP_signature
Reply all
Reply to author
Forward
0 new messages