Ansible 2.2.1.0 aliases does not work as expected

Hi everybody!

It seems, inventory aliases does not work if trying to connect to hosts behind NAT with same address and different ssh_port.

I,m wondering, if I have made some mistake in configuration, or it is a bug or known feature?

Ansible version:

d.lomanchuk@LenovoX220i:~/.ansible$ ansible --version ansible 2.2.1.0 config file = /etc/ansible/ansible.cfg configured module search path = Default w/o overrides

My inventory:

d.lomanchuk@LenovoX220i:~/.ansible$ cat test_inventory [denet] mycoolbox ansible_host=192.168.1.202 ansible_port=2210 ansible_user=dmitry ansible_password=******* le_lxc ansible_host=192.168.1.202 ansible_port=2323 ansible_user=dmitry ansible_password=*******`

`

Trying to ping hosts i see failures sometimes:

`
d.lomanchuk@LenovoX220i:~/.ansible$ ansible -i test_inventory -m ping all
le_lxc | FAILED! => {
“failed”: true,
“msg”: “failed to transfer file to /home/dmitry/.ansible/tmp/ansible-tmp-1489399666.62-12650861868843/ping.py:\n\nscp: /home/dmitry/.ansible/tmp/ansible-tmp-1489399666.62-12650861868843/ping.py: No such file or directory\n”
}
mycoolbox | SUCCESS => {
“changed”: false,
“ping”: “pong”
}

`

But, if I try to ping them again immediately after such failure, everything looks good:

d.lomanchuk@LenovoX220i:~/.ansible$ ansible -i test_inventory -m ping all
mycoolbox | SUCCESS => {
"changed": false,
"ping": "pong"
}
le_lxc | SUCCESS => {
"changed": false,
"ping": "pong"
}

However, if I try to run hostname command via “shell” module immediately after that, I see that ansible connects to the same host (one of all):

`
d.lomanchuk@LenovoX220i:~/.ansible$ ansible -i test_inventory -m shell -a “hostname” all
le_lxc | SUCCESS | rc=0 >>
denetpbx

mycoolbox | SUCCESS | rc=0 >>
denetpbx

`

Aliases works as expected, only if I type them in /etc/hosts on my machine.

`

d.lomanchuk@LenovoX220i:~/.ansible$ tail -n 2 /etc/hosts
192.168.1.202 mycoolbox
192.168.1.202 le_lxc
`

Then, everything works as expected.

`
d.lomanchuk@LenovoX220i:~/.ansible$ ansible -i test_inventory -m shell -a “hostname” all
le_lxc | SUCCESS | rc=0 >>
letsencrypt

mycoolbox | SUCCESS | rc=0 >>
denetpbx

`

It was my silly configuration mistake.

There are a lot of servers with very long hostnames in my network.
That is why I changed ansible.cfg control_path parameter to:
So, if ansible_host and ansible_user are the same for multiple hosts - the problem occures.

control_path = %(directory)s/%%h-%%r

For example:

`
[test-servers]

alias1 ansible_host=foo.example.com ansible_port=2222 ansible_user=dmitry

alias2 ansible_host=foo.example.com ansible_port=2233 ansible_user=dmitry
`