# Inventory: same host different port + username?

**URL:** <https://forum.ansible.com/t/inventory-same-host-different-port-username/15228>\
**Category:** Ansible Project\
**Created:** [May 14, 2014, 7:38am UTC](https://forum.ansible.com/t/inventory-same-host-different-port-username/15228 "2014-05-14T07:38:38Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Robin\_Speekenbrink](https://avatars.discourse-cdn.com/v4/letter/r/e19adc/32.png) [@Robin\_Speekenbrink](https://forum.ansible.com/u/Robin_Speekenbrink)\
**Post date:** [May 14, 2014, 7:38am UTC](https://forum.ansible.com/t/inventory-same-host-different-port-username/15228/1 "2014-05-14T07:38:38Z")

</div>

Just a quick question:  
I have a docker container running an SSH daemon on a host running an SSH deamon both with different users and ports… Thus my invenvory looks something like this:  
[hosts:vars]

ansible\_ssh\_user=hroot  
ansible\_ssh\_port=2221

[hosts]  
host1  
host2

[containers:vars]

ansible\_ssh\_user=croot  
ansible\_ssh\_port=2222

[containers]  
host1  
host2

But when calling ansible containers the information from the hosts are used…

Another thing in my situation: the hosts lists themselves (host1 and host2 in my example) should be dynamic since the’re running in an autoscaling EC2 cloud 😃  
Am i doing it wrong? 🙂

Thanks!

Robin

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [May 14, 2014, 10:17pm UTC](https://forum.ansible.com/t/inventory-same-host-different-port-username/15228/2 "2014-05-14T22:17:20Z")

</div>

"But when calling ansible containers the information from the hosts are used… "

This is correct. Variables are brought in from all the groups a host is a member of, not just the hosts you are selecting.

Imagine you have hosts grouped by function (webservers, dbservers), datacenter (phoenix, atlanta), and also hardware type (dell3200, ibm286), etc.

Some hardware information might come from the hardware group, but you selected the webserver group to configure.

Thus variables should come from all sources.

---

<div class="post-metadata">

**Author:** ![Robin\_Speekenbrink](https://avatars.discourse-cdn.com/v4/letter/r/e19adc/32.png) [@Robin\_Speekenbrink](https://forum.ansible.com/u/Robin_Speekenbrink)\
**Post date:** [May 23, 2014, 7:57am UTC](https://forum.ansible.com/t/inventory-same-host-different-port-username/15228/3 "2014-05-23T07:57:38Z")

</div>

Michael,

Thanks for the response. This clears up why all variables would be used. How then would i achieve my goal of having two seperate logins per machine? (one on the psysical host and the other on the environment inside it?)  
The IP adress would be the same, the hostname could be the same and i’d hate to have to register seperate DNS entries just to alter the SSH-port 😉

Thanks in advance!

Robin

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [May 23, 2014, 12:36pm UTC](https://forum.ansible.com/t/inventory-same-host-different-port-username/15228/4 "2014-05-23T12:36:42Z")

</div>

Not sure if you want to, but technically possible:

[pirates]  
alias1 ansible\_ssh\_host=[foo.example.com](http://foo.example.com) ansible\_ssh\_user=westley

[mercenaries]  
alias2 ansible\_ssh\_host=[foo.example.com](http://foo.example.com) ansible\_ssh\_user=inigo

Just don’t write plays that target “hosts: all” in that case, as you’ll work on servers twice.

---

<div class="post-metadata">

**Author:** ![Robin\_Speekenbrink](https://avatars.discourse-cdn.com/v4/letter/r/e19adc/32.png) [@Robin\_Speekenbrink](https://forum.ansible.com/u/Robin_Speekenbrink)\
**Post date:** [May 27, 2014, 7:22pm UTC](https://forum.ansible.com/t/inventory-same-host-different-port-username/15228/5 "2014-05-27T19:22:25Z")

</div>

Michael,

Thanks, that would be what i’m looking for: running ansible commands on the hosts and on the VM’s (docker containers in my case) inside the hosts)

lets see if that works 🙂

Thanks again!
