# Inventory hostname as variable ?

**URL:** <https://forum.ansible.com/t/inventory-hostname-as-variable/11927>\
**Category:** Ansible Project\
**Created:** [September 25, 2012, 2:47pm UTC](https://forum.ansible.com/t/inventory-hostname-as-variable/11927 "2012-09-25T14:47:59Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Julien\_Tognazzi](https://avatars.discourse-cdn.com/v4/letter/j/b38774/32.png) [@Julien\_Tognazzi](https://forum.ansible.com/u/Julien_Tognazzi)\
**Post date:** [September 25, 2012, 2:47pm UTC](https://forum.ansible.com/t/inventory-hostname-as-variable/11927/1 "2012-09-25T14:47:59Z")

</div>

Hi,

I try to search the doc and the list to see if it is possible to use the inventory hostname in playbooks (the one specified in /etc/ansible/hosts).

Because in case the name in the inventory doesn’t match the remote hostname (/etc/hosts, cname, etc.), the setup module isn’t reporting any ansible variable with this info.

Did I miss something ?

Julien.

---

<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:** [September 25, 2012, 3:19pm UTC](https://forum.ansible.com/t/inventory-hostname-as-variable/11927/2 "2012-09-25T15:19:31Z")

</div>

There's a "magic" variable called $inventory\_hostname that matches the  
name given in the inventory file, that I think is what you want.

---

<div class="post-metadata">

**Author:** ![Derek\_Carter1](https://avatars.discourse-cdn.com/v4/letter/d/5fc32e/32.png) [@Derek\_Carter1](https://forum.ansible.com/u/Derek_Carter1)\
**Post date:** [September 25, 2012, 3:21pm UTC](https://forum.ansible.com/t/inventory-hostname-as-variable/11927/3 "2012-09-25T15:21:30Z")

</div>

I'm not sure where it is on the docs but the two varibles you get  
automatically are

$inventory\_hostname

and

$inventory\_hostname\_short

The latter being everything up to the first '.' in $inventory\_hostname

---

<div class="post-metadata">

**Author:** ![Julien\_Tognazzi](https://avatars.discourse-cdn.com/v4/letter/j/b38774/32.png) [@Julien\_Tognazzi](https://forum.ansible.com/u/Julien_Tognazzi)\
**Post date:** [September 27, 2012, 7:45am UTC](https://forum.ansible.com/t/inventory-hostname-as-variable/11927/4 "2012-09-27T07:45:58Z")

</div>

Great !  
Exactly what I was looking for.  
And, it is mentioned in the docs in the following section.  
[http://ansible.cc/docs/playbooks2.html?highlight=inventory\_hostname#contents](http://ansible.cc/docs/playbooks2.html?highlight=inventory_hostname#contents)

So all is good.

Thank you.
