# Same host in inventory twice with two different ports, not working

**URL:** <https://forum.ansible.com/t/same-host-in-inventory-twice-with-two-different-ports-not-working/21293>\
**Category:** Ansible Project\
**Created:** [April 28, 2016, 1:35am UTC](https://forum.ansible.com/t/same-host-in-inventory-twice-with-two-different-ports-not-working/21293 "2016-04-28T01:35:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![John\_Gateley](https://avatars.discourse-cdn.com/v4/letter/j/4af34b/32.png) [@John\_Gateley](https://forum.ansible.com/u/John_Gateley)\
**Post date:** [April 28, 2016, 1:35am UTC](https://forum.ansible.com/t/same-host-in-inventory-twice-with-two-different-ports-not-working/21293/1 "2016-04-28T01:35:47Z")

</div>

Hello,

I have an inventory file with the same host twice, once using ssh on port 22, once using ssh on port 12345. This is inventory file bad.hosts2:

[host1]

[foo.example.com:22](http://foo.example.com:22) ansible\_connection=ssh ansible\_ssh\_user=root

[host2]

[foo.example.com:12345](http://foo.example.com:12345) ansible\_connection=ssh ansible\_ssh\_user=root

The intent is to use one host (host1) to copy a new sshd config file and to setup sshd to run on a different port. From then on, the second host (host2) would be used.

However, the behavior is strange: no matter what I do, port 22 is used.

Johns-MacBook-Air:ansible.bug j$ ansible -i bad.hosts2 -m ping host1

foo.example.com| success \>\> {

“changed”: false,

“ping”: “pong”

}

Johns-MacBook-Air:ansible.bug j$ ansible -i bad.hosts2 -m ping host2

[foo.example.com](http://foo.example.com) | success \>\> {

“changed”: false,

“ping”: “pong”

}

Note sshd is only running on port 22, and if I switch the order of the two hosts in the inventory file, both host1 and host2 will fail the ping.

What is happening here?

Thanks

John

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [April 28, 2016, 11:28pm UTC](https://forum.ansible.com/t/same-host-in-inventory-twice-with-two-different-ports-not-working/21293/2 "2016-04-28T23:28:42Z")

</div>

Instead of using groups, you should likely use aliases such as:

host1 ansible\_host=[foo.example.com](http://foo.example.com) ansible\_port=22  
host2 ansible\_host=[foo.example.com](http://foo.example.com) ansible\_port=12345

Inventory names in Ansible are unique, so aliasing like that allows you to duplicate a host, while keep configurations for each different.

---

<div class="post-metadata">

**Author:** ![alexey.vazhnov](https://avatars.discourse-cdn.com/v4/letter/a/b487fb/32.png) [@alexey.vazhnov](https://forum.ansible.com/u/alexey.vazhnov)\
**Post date:** [May 4, 2016, 12:39pm UTC](https://forum.ansible.com/t/same-host-in-inventory-twice-with-two-different-ports-not-working/21293/3 "2016-05-04T12:39:06Z")

</div>

I think what better solution for storing SSH settings (hostname, port, username, proxycommand, identityfile …) is ~/.ssh/config.

---

<div class="post-metadata">

**Author:** ![Matt\_Hite](https://avatars.discourse-cdn.com/v4/letter/m/5fc32e/32.png) [@Matt\_Hite](https://forum.ansible.com/u/Matt_Hite)\
**Post date:** [May 4, 2016, 3:07pm UTC](https://forum.ansible.com/t/same-host-in-inventory-twice-with-two-different-ports-not-working/21293/4 "2016-05-04T15:07:41Z")

</div>

Try using “ansible\_ssh\_port” rather than hostname:port.
