# How can you set multiple hosts on the same machine with dynamic inventories

**URL:** <https://forum.ansible.com/t/how-can-you-set-multiple-hosts-on-the-same-machine-with-dynamic-inventories/21020>\
**Category:** Ansible Project\
**Created:** [March 30, 2016, 2:23pm UTC](https://forum.ansible.com/t/how-can-you-set-multiple-hosts-on-the-same-machine-with-dynamic-inventories/21020 "2016-03-30T14:23:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ryangrannell](https://avatars.discourse-cdn.com/v4/letter/r/c0e974/32.png) [@ryangrannell](https://forum.ansible.com/u/ryangrannell)\
**Post date:** [March 30, 2016, 2:23pm UTC](https://forum.ansible.com/t/how-can-you-set-multiple-hosts-on-the-same-machine-with-dynamic-inventories/21020/1 "2016-03-30T14:23:42Z")

</div>

Hi,

I’m currently setting up two VMs that are accessible behind a single domain name but on different ports. I cannot figure  
out how to specifiy host-vars seperately for each host. My aim is to allow ansible to SSH into each of these machines; I’m not currently  
using hostvars for anything else.

If I had two machines accessible on different ports and different domains - [example-1.com:22](http://example-1.com:22) and [example-2.com:9001](http://example-2.com:9001) - I would produce this dynamic inventory JSON output:

`{ "mymachines": { "hosts": ["example-1.com", "example-2.com"], "hostvars": { "example-1.com": { "ansible_port": "22" }, "example-2.com": { "ansible_port": "9001" } } } }`

However, there is no analogue for different hosts with the same domain name:

`{ "mymachines": { "hosts": ["example-0.com", "example-0.com",], "hostvars": { "example-0.com": { "ansible_port": "22" }, ??: { "ansible_port": "9001" } } } }`

I cannot specify the domain-names alongside their ports  
`

{  
“mymachines”: {  
“hosts”: [  
“[example-0.com:22](http://example-0.com:22)”,  
“[example-0.com:9001](http://example-0.com:9001)”,  
]  
}  
}  
`

as Ansible passes the entire host to OpenSSH without using the -p argument, which means the host never resolves. It seems  
to run code equivalent to the following

`ssh foo@example-0.com:9001 # BAD`

not

`ssh foo@example-0.com -p 9001 # GOOD`

Do you have any advice for how I can create a dynamic inventory that allows SSH access to machines with the same domain name  
but different ports? I’ll post more information if needed.

Thanks

---

<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:** [March 30, 2016, 3:17pm UTC](https://forum.ansible.com/t/how-can-you-set-multiple-hosts-on-the-same-machine-with-dynamic-inventories/21020/2 "2016-03-30T15:17:07Z")

</div>

Just as with ini formatted inventories, if you want to use the same IP or hostname for 2 different “hosts”, you need to create an alias for them, and specify the hostname using ansible\_host (ansible\_ssh\_host for versions before v2)

---

<div class="post-metadata">

**Author:** ![ryangrannell](https://avatars.discourse-cdn.com/v4/letter/r/c0e974/32.png) [@ryangrannell](https://forum.ansible.com/u/ryangrannell)\
**Post date:** [March 30, 2016, 4:05pm UTC](https://forum.ansible.com/t/how-can-you-set-multiple-hosts-on-the-same-machine-with-dynamic-inventories/21020/3 "2016-03-30T16:05:05Z")

</div>

> Thanks for your (quick!) suggestion. That worked perfectly, thank you. For reference by anyone reading this question, the working solution looks roughly like this:

`{ "example-1": { "vars": { "ansible_host": "example-1.com", "ansible_port": 5513 }, "hosts": ["unused-alias-1"] }, "example-2": { "vars": { "ansible_host": "example-2.com", "ansible_port": 22 }, "hosts": ["unused-alias-0"] } }`

---

<div class="post-metadata">

**Author:** ![Uditha\_Desilva](https://avatars.discourse-cdn.com/v4/letter/u/f475e1/32.png) [@Uditha\_Desilva](https://forum.ansible.com/u/Uditha_Desilva)\
**Post date:** [March 30, 2016, 10:05pm UTC](https://forum.ansible.com/t/how-can-you-set-multiple-hosts-on-the-same-machine-with-dynamic-inventories/21020/4 "2016-03-30T22:05:10Z")

</div>

Are you sure that’s quite right? I’d have expected the “ansible\_host” value within each host entry to be the same, otherwise you wouldn’t have had the problem in the first place…?

---

<div class="post-metadata">

**Author:** ![ryangrannell](https://avatars.discourse-cdn.com/v4/letter/r/c0e974/32.png) [@ryangrannell](https://forum.ansible.com/u/ryangrannell)\
**Post date:** [March 31, 2016, 9:42am UTC](https://forum.ansible.com/t/how-can-you-set-multiple-hosts-on-the-same-machine-with-dynamic-inventories/21020/5 "2016-03-31T09:42:23Z")

</div>

Sorry, thanks for catching that error. I attached an amended version:

{  
“alias-1”: {  
“vars”: {  
“ansible\_host”: “[example-0.com](http://example-1.com/)”,  
“ansible\_port”: 5513  
},  
“hosts”: [  
“unused-alias-1”  
]  
},  
“alias-2”: {  
“vars”: {  
“ansible\_host”: “[example-0.com](http://example-2.com/)”,  
“ansible\_port”: 22  
},  
“hosts”: [  
“unused-alias-0”  
]  
}  
}
