# Naming hosts in ansible inventory

**URL:** <https://forum.ansible.com/t/naming-hosts-in-ansible-inventory/11752>\
**Category:** Ansible Project\
**Tags:** ansible-project\
**Created:** [August 2, 2012, 5:12pm UTC](https://forum.ansible.com/t/naming-hosts-in-ansible-inventory/11752 "2012-08-02T17:12:07Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![CWS](https://avatars.discourse-cdn.com/v4/letter/c/e495f1/32.png) [@CWS](https://forum.ansible.com/u/CWS)\
**Post date:** [August 2, 2012, 5:12pm UTC](https://forum.ansible.com/t/naming-hosts-in-ansible-inventory/11752/1 "2012-08-02T17:12:07Z")

</div>

The workaround I've been using is to create a separate DNS (or  
/etc/hosts) entry for the tunnels. From my understanding since I  
implemented it there has been some work done with host aliases/labels,  
but I haven't had enough time to explore those.

With the DNS approach make sure that the \*first\* entry in your ansible  
hosts file has the port number set correctly. The port number of all  
following entries will be ignored. So for example you can have a  
hosts file like this:

tunnel021.local:62021  
tunnel022.local:62022

[foo]  
tunnel021.local  
tunnel022.local

The entries in group foo will use the previously defined ports for  
those hosts.

---

<div class="post-metadata">

**Author:** ![Edu\_el\_Ligre](https://avatars.discourse-cdn.com/v4/letter/e/f05b48/32.png) [@Edu\_el\_Ligre](https://forum.ansible.com/u/Edu_el_Ligre)\
**Post date:** [August 3, 2012, 7:22am UTC](https://forum.ansible.com/t/naming-hosts-in-ansible-inventory/11752/2 "2012-08-03T07:22:07Z")

</div>

Thanks,

> With the DNS approach make sure that the _first_ entry in your ansible  
> hosts file has the port number set correctly. The port number of all  
> following entries will be ignored.

Then, if I understand the above well, my use case does require keeping an up-to-date /etc/hosts because ansible will ignore specifying different ports for the same IP (127.0.0.1) in the inventory file?

> From my understanding since I  
> implemented it there has been some work done with host aliases/labels,

Anybody has used a different approach with success?

Thanks

---

<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:** [August 3, 2012, 11:08am UTC](https://forum.ansible.com/t/naming-hosts-in-ansible-inventory/11752/3 "2012-08-03T11:08:53Z")

</div>

ansible's inventory records are essentially strings.

It does respect a variable called ansible\_ssh\_port, which when set on any inventory record, will cause it to speak to exactly that port.

The port can also be specified like:

hostname:12345

although you cannot have duplicate hostnames in the inventory file (yet) because the port is not stored as part of the name.

If it were, some people would find that confusing because "hostname\*" would be needed to match "hostname:12345"

I think the ideal proposal would be something like this

some\_identifier:12345 alias\_for=foosball.example.org  
other\_identifier:12346 alias\_for=foosball.example.org

but the connection code doesn't allow keeping a host specific address separate from the name in inventory right now. Patches would be considered for 0.7
