Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to set hostname using inventory file

Tags:

ansible

I have my hosts in an inventory file as below:

cnamgw01b ansible_ssh_host=172.17.0.26 
cnamgw01a ansible_ssh_host=172.17.1.26
cnamgw02b ansible_ssh_host=172.17.0.23
cnamgw02a ansible_ssh_host=172.17.1.23 
cnamgw03a ansible_ssh_host=172.17.1.13
cnamgw03b ansible_ssh_host=172.17.0.13 

These are new builds and I would like to set the hostname based on the inventory file. I already have a script in place that updated the inventory file as new VM's are turned up and assigns a random hostname. I would like to take this hostname assigned and set it as the hosts hostname. How can I accomplish this? Also note that I also use folders to subdivide the hosts by region

like image 260
user2236794 Avatar asked Mar 08 '26 14:03

user2236794


1 Answers

You can use the ansible module hostname to set hostname. https://docs.ansible.com/ansible/latest/modules/hostname_module.html

- hosts: all
  tasks:
  - name: Set hostname
    hostname:
     name: {{ inventory_hostname }}
like image 175
Smily Avatar answered Mar 10 '26 09:03

Smily