I am thinking on what would be the best way to set hostname on servers (trying to provision test machines) and if there are already a sample playbook or what would be the best approach,
Write playbook, to use sequence and then name it using that or maybe set it in ansible hostfile and use that variable to set the hostname ? Any recommendations would be really great!!
You can use $inventory_hostname for whatever is in inventory and
$inventory_hostname_short will include everything but will chop off
the domain name part.
Do not confuse with $ansible_hostname which is the fact derived from
talking to the system!
You can use $ansible_hostname which is the hostname fact.
If you have the IP in the inventory file, the $inventory_hostname is
actually going to be the IP, but you are right in that you can set
arbitrary variables on those lines, or in a "host_vars/<name of host>"
file too, in a directory alongside your inventory file.
Thanks, Sorry for the confusion, Will try to clarify it, So basically all the test servers have hostname as localhost now, I want to basically change the hostname to say “u1”
So from what i understand “$inventory_hostname” is a magic variable, but i dont know how to set this variable. So if “$inventory_hostname” is just going to be the IP from /etc/ansible/hosts file. Might not help me in this problem, but if i can have key=value , for the IP, So i can just use IP as the key and value can be whatever hostname i need to set.
I guess easiest thing would be set variable along with IP and use that to set the hostname? Or is there a recommended way to solve this.