# Hostname to ip mapping

**URL:** <https://forum.ansible.com/t/hostname-to-ip-mapping/12008>\
**Category:** Ansible Project\
**Created:** [October 21, 2012, 2:37pm UTC](https://forum.ansible.com/t/hostname-to-ip-mapping/12008 "2012-10-21T14:37:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Janis\_Gengeris](https://avatars.discourse-cdn.com/v4/letter/j/9fc29f/32.png) [@Janis\_Gengeris](https://forum.ansible.com/u/Janis_Gengeris)\
**Post date:** [October 21, 2012, 2:37pm UTC](https://forum.ansible.com/t/hostname-to-ip-mapping/12008/1 "2012-10-21T14:37:24Z")

</div>

I want to create similar mapping as in /etc/hosts , but local to use in Ansible, so that all the hostnames are independent of DNS or /etc/hosts and can be stored directly inside VCS.

The motivation for this, usage of hostnames inside playbooks and cli, but using IPs(only if defined) for connections.

Currently I’m using kludgy inventory group for each host like this:

[host1]  
192.168.0.1

[host2]  
192.168.0.2:4400 host\_var=hello

[host3]  
192.168.0.3 other\_var=world

[host\_group:children]  
host1  
host2  
host3

I wonder, it there a better way to do this?

It would be nice to be able to do this:

[host\_group]

host1=192.168.0.1  
host2=192.168.0.2:4400 host\_var=hello  
host3=192.168.0.3 other\_var=world

If IP is assigned to host, then it’s made available through ‘inventory\_ip’, similar to ‘inventory\_hostname’. And Ansible should use the defined IP internally for creating connections to hosts.

Regards,  
–janis

---

<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:** [October 21, 2012, 2:47pm UTC](https://forum.ansible.com/t/hostname-to-ip-mapping/12008/2 "2012-10-21T14:47:28Z")

</div>

Not \*entirely\* sure I understand the use case, but if you want to  
connect by IP, I still find it useful to have real names for hosts.

This works in 0.8 and would contact machines by IP but still allow you  
to refer to them by name.

[webservers]  
whatever\_name ansible\_ssh\_host=192.168.10.50

You may also wish to consider writing your own external inventory  
script to provide your ideal way to organize your host configurations  
-- it's pretty easy to do and there are several examples in the  
plugins directory for doing this.

If you set the variables ansible\_ssh\_host and ansible\_ssh\_user on  
particular hosts, in an external script, you can control how each host  
alias is connected to.

--Michael

---

<div class="post-metadata">

**Author:** ![Janis\_Gengeris](https://avatars.discourse-cdn.com/v4/letter/j/9fc29f/32.png) [@Janis\_Gengeris](https://forum.ansible.com/u/Janis_Gengeris)\
**Post date:** [October 21, 2012, 4:52pm UTC](https://forum.ansible.com/t/hostname-to-ip-mapping/12008/3 "2012-10-21T16:52:43Z")

</div>

ansible\_ssh\_host is not working as expected, I filled a ticket about this.
