# {{ inventory\_hostname  }} variable

**URL:** <https://forum.ansible.com/t/inventory-hostname-variable/19853>\
**Category:** Ansible Project\
**Created:** [November 25, 2015, 4:45pm UTC](https://forum.ansible.com/t/inventory-hostname-variable/19853 "2015-11-25T16:45:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![richard\_kappler](https://avatars.discourse-cdn.com/v4/letter/r/977dab/32.png) [@richard\_kappler](https://forum.ansible.com/u/richard_kappler)\
**Post date:** [November 25, 2015, 4:45pm UTC](https://forum.ansible.com/t/inventory-hostname-variable/19853/1 "2015-11-25T16:45:25Z")

</div>

New to Ansible here, and have a question on using an inventory\_hostname variable.

I have a playbook that deploys an add-on to numerous servers, using an inventory list of IP addresses setup as so:

[servers01]  
xxx.xxx.xxx.xxx  
yyy.yyy.yyy.yyy

[servers02]  
zzz.zzz.zzz.zzz

and so on.

One of the plays edits a configuration file with:

- name: edit inputs.conf for server name  
replace:  
dest=/path/to/inputs/conf  
regexp=‘PLACEHOLDER’  
replace=“{{ inventory\_hostname }}”  
backup=no

This playbook has been tested in our local test environment and works well, but it has now been decided to use a text hostname instead of an IP address. I have updated the inventory file as follows:

[servers01]  
hostname1 xxx.xxx.xxx.xxx  
hostname2 yyy.yyy.yyy.yyy

[servers02]  
hostname3 zzz.zzz.zzz.zzz

and so on.

My question is, does inventory\_hostname pull out hostname1 or still pull out the IP address?

regards, Richard

---

<div class="post-metadata">

**Author:** ![Roger\_Sherman1](https://avatars.discourse-cdn.com/v4/letter/r/c68b51/32.png) [@Roger\_Sherman1](https://forum.ansible.com/u/Roger_Sherman1)\
**Post date:** [November 25, 2015, 7:28pm UTC](https://forum.ansible.com/t/inventory-hostname-variable/19853/2 "2015-11-25T19:28:46Z")

</div>

Hi Richard,

I’m sure there’s more than one way to skin a cat, here, but I set up my hosts file thusly, and it does the job:

```auto
[rails]
rails0 ansible_ssh_host=xxx.xxx.xxx.xxx
rails1 ansible_ssh_host=xxx.xxx.xxx.xxx
rails2 ansible_ssh_host=xxx.xxx.xxx.xxx

```

I believe when doing this, you’re just aliasing the hostname for the ip number.

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [November 25, 2015, 7:31pm UTC](https://forum.ansible.com/t/inventory-hostname-variable/19853/3 "2015-11-25T19:31:19Z")

</div>

inventory\_hostname should always reflect the 'hostname alias' provided  
by the inventory, if you ALWAYS want the IPs use ansible\_ssh\_host  
(ansbile\_host in 2.0 and above).

---

<div class="post-metadata">

**Author:** ![richard\_kappler](https://avatars.discourse-cdn.com/v4/letter/r/977dab/32.png) [@richard\_kappler](https://forum.ansible.com/u/richard_kappler)\
**Post date:** [December 1, 2015, 6:36pm UTC](https://forum.ansible.com/t/inventory-hostname-variable/19853/4 "2015-12-01T18:36:52Z")

</div>

no, indeed it worked as I had hoped, and as Roger and Brian spoke of. By using

[servers]  
name ansible\_ssh\_host=xx.xx.xx.xx

it worked just fine. Ran into some other problems, but that’s a different post

regards, Richard
