Inventory: same host different port + username?

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 :smiley:
Am i doing it wrong? :slight_smile:

Thanks!

Robin

"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.

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 :wink:

Thanks in advance!

Robin

Not sure if you want to, but technically possible:

[pirates]
alias1 ansible_ssh_host=foo.example.com ansible_ssh_user=westley

[mercenaries]
alias2 ansible_ssh_host=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.

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 :slight_smile:

Thanks again!