# Playbook to set Hostname for servers

**URL:** <https://forum.ansible.com/t/playbook-to-set-hostname-for-servers/12263>\
**Category:** Ansible Project\
**Created:** [January 30, 2013, 2:04am UTC](https://forum.ansible.com/t/playbook-to-set-hostname-for-servers/12263 "2013-01-30T02:04:45Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kavin](https://avatars.discourse-cdn.com/v4/letter/k/ba8739/32.png) [@Kavin](https://forum.ansible.com/u/Kavin)\
**Post date:** [January 30, 2013, 2:04am UTC](https://forum.ansible.com/t/playbook-to-set-hostname-for-servers/12263/1 "2013-01-30T02:04:45Z")

</div>

Hi,

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!!

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [January 30, 2013, 4:11am UTC](https://forum.ansible.com/t/playbook-to-set-hostname-for-servers/12263/2 "2013-01-30T04:11:21Z")

</div>

Possible suggestion:

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! 🙂

---

<div class="post-metadata">

**Author:** ![Kavin](https://avatars.discourse-cdn.com/v4/letter/k/ba8739/32.png) [@Kavin](https://forum.ansible.com/u/Kavin)\
**Post date:** [January 30, 2013, 5:18am UTC](https://forum.ansible.com/t/playbook-to-set-hostname-for-servers/12263/3 "2013-01-30T05:18:44Z")

</div>

Thanks for the reply, Which documentation i should follow to define ($inventory\_hostname) in hostfile for a list of IP’s.

I could not find any reference in this: [http://ansible.cc/docs/patterns.html](http://ansible.cc/docs/patterns.html)

is it something like “ntp\_server: [acme.example.org](http://acme.example.org)” or “ntp\_server=[ntp.atlanta.example.com](http://ntp.atlanta.example.com)” ?

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [January 30, 2013, 5:45am UTC](https://forum.ansible.com/t/playbook-to-set-hostname-for-servers/12263/4 "2013-01-30T05:45:44Z")

</div>

Do you mean to get that value, or to define it?

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.

--Michael

---

<div class="post-metadata">

**Author:** ![Kavin](https://avatars.discourse-cdn.com/v4/letter/k/ba8739/32.png) [@Kavin](https://forum.ansible.com/u/Kavin)\
**Post date:** [January 30, 2013, 7:23pm UTC](https://forum.ansible.com/t/playbook-to-set-hostname-for-servers/12263/5 "2013-01-30T19:23:21Z")

</div>

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.

Sorry i am still learning Ansible.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [January 30, 2013, 7:35pm UTC](https://forum.ansible.com/t/playbook-to-set-hostname-for-servers/12263/6 "2013-01-30T19:35:56Z")

</div>

Sounds like you have decided to SSH in by IP and set the hostname.

If doing so, I would do it like this

[whatever\_group]  
2.2.2.122 hostname=foo.example.com

and then just use $hostname

there is nothing magic about hostname, you could have called it  
'blippy' in this case, but that's an easy way to set a variable in the  
host file.

You can of course also use "host\_vars/\<hostname\>" to keep them in  
seperate files, but it's overkill in this case.

---

<div class="post-metadata">

**Author:** ![Kavin](https://avatars.discourse-cdn.com/v4/letter/k/ba8739/32.png) [@Kavin](https://forum.ansible.com/u/Kavin)\
**Post date:** [January 30, 2013, 7:50pm UTC](https://forum.ansible.com/t/playbook-to-set-hostname-for-servers/12263/7 "2013-01-30T19:50:00Z")

</div>

Thank you very much, This is very helpful.
