# Node naming / Referencing nodes by "name"?

**URL:** <https://forum.ansible.com/t/node-naming-referencing-nodes-by-name/11618>\
**Category:** Ansible Project\
**Created:** [June 17, 2012, 8:43am UTC](https://forum.ansible.com/t/node-naming-referencing-nodes-by-name/11618 "2012-06-17T08:43:59Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marten\_Gustafson](https://avatars.discourse-cdn.com/v4/letter/m/5fc32e/32.png) [@Marten\_Gustafson](https://forum.ansible.com/u/Marten_Gustafson)\
**Post date:** [June 17, 2012, 8:43am UTC](https://forum.ansible.com/t/node-naming-referencing-nodes-by-name/11618/1 "2012-06-17T08:43:59Z")

</div>

Howdy!

I'm trying figure out how to best classify nodes in order to be able  
to run the same playbook(s) efficiently against multiple different  
target environments.

To illustrate, let's say I have a playbook that sets up an appserver  
and one for a database server. Part of the appserver is a  
configuration file that must contain the IP and port number of the  
database. Getting the values dynamically into the file is, of course,  
is solved with templates.

But I wan't this to work when I run both playbooks against different  
target environments. For example one with a single, local, Vagrant  
instance and one with three separate servers (2 app & 1 db).

So regardless of the host/inventory configuration I'd like to be able  
to do something like following in my template:  
{{ hostvars['db']['host'] }}  
and  
{{ hostvar['db']['port'] }}

But I am unable to see how to achieve this with the current layout /  
function of the hosts (inventory) file. I imagine what I'd like  
(need?) is the ability to define nodes as a value pair consisting of  
an arbitrary name and it's hostname/IP.

Or is this simply a matter of me not having wrapped my head around  
Ansible in the correct way?

The fictive example below would cause nodes to appear in the hostvar  
structure by their name (if given) and by their IP/hostname if no name  
was given.

== 1 host

---

<div class="post-metadata">

**Author:** ![Daniel\_Hokka\_Zakriss](https://avatars.discourse-cdn.com/v4/letter/d/f05b48/32.png) [@Daniel\_Hokka\_Zakriss](https://forum.ansible.com/u/Daniel_Hokka_Zakriss)\
**Post date:** [June 17, 2012, 9:49pm UTC](https://forum.ansible.com/t/node-naming-referencing-nodes-by-name/11618/2 "2012-06-17T21:49:37Z")

</div>

Mårten Gustafson wrote:

> Howdy!
> 
> I'm trying figure out how to best classify nodes in order to be able  
> to run the same playbook(s) efficiently against multiple different  
> target environments.
> 
> To illustrate, let's say I have a playbook that sets up an appserver  
> and one for a database server. Part of the appserver is a  
> configuration file that must contain the IP and port number of the  
> database. Getting the values dynamically into the file is, of course,  
> is solved with templates.
> 
> But I wan't this to work when I run both playbooks against different  
> target environments. For example one with a single, local, Vagrant  
> instance and one with three separate servers (2 app & 1 db).
> 
> So regardless of the host/inventory configuration I'd like to be able  
> to do something like following in my template:  
> {{ hostvars['db']['host'] }}  
> and  
> {{ hostvar['db']['port'] }}
> 
> But I am unable to see how to achieve this with the current layout /  
> function of the hosts (inventory) file. I imagine what I'd like  
> (need?) is the ability to define nodes as a value pair consisting of  
> an arbitrary name and it's hostname/IP.

You could have a host-variable containing the name of the database  
server to use. Your template would then use something like  
{{ hostvars[db\_server]['port'] }}  
to access the database's settings.

---

<div class="post-metadata">

**Author:** ![Marten\_Gustafson](https://avatars.discourse-cdn.com/v4/letter/m/5fc32e/32.png) [@Marten\_Gustafson](https://forum.ansible.com/u/Marten_Gustafson)\
**Post date:** [June 17, 2012, 10:19pm UTC](https://forum.ansible.com/t/node-naming-referencing-nodes-by-name/11618/3 "2012-06-17T22:19:05Z")

</div>

Hm, right. Like this:

- group: appservers  
&nbsp;&nbsp;hosts:  
&nbsp;&nbsp;&nbsp;&nbsp;- 1.2.3.4  
&nbsp;&nbsp;vars:  
&nbsp;&nbsp;&nbsp;- db\_server: 1.2.3.4

- node: 1.2.3.4  
&nbsp;&nbsp;vars:  
&nbsp;&nbsp;- port: 9999

That ought to work, thanks!

Has there been any thoughts along the lines of allowing arbitrary  
naming of hosts in the inventory file?

/mårten.

---

<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:** [June 18, 2012, 12:06am UTC](https://forum.ansible.com/t/node-naming-referencing-nodes-by-name/11618/4 "2012-06-18T00:06:56Z")

</div>

> That ought to work, thanks!
> 
> Has there been any thoughts along the lines of allowing arbitrary  
> naming of hosts in the inventory file?

Yeah, we know what we want to do, but haven’t done it yet.

Basically we need to have a variable like ansible\_ssh\_host and if set, connection.py will use that as the host address versus the name of the host record, just like ansible\_ssh\_port can be used to override the port.

Just needs a small patch and testing, syntactic sugar in the inventory parser could come later.

–Michael
