Ansible multiple hosts behind NAT port forwarding

I have 2 servers behind NAT, so they share the same IP address, SSH service runs on two different ports. Here is my ansible inventory file.

[web]
195.154.255.26 ansible_connection=ssh ansible_ssh_user=root ansible_ssh_pass=serverok123 ansible_port=4000
195.154.255.26 ansible_connection=ssh ansible_ssh_user=root ansible_ssh_pass=serverok123 ansible_port=4001

When I run the ansible command against the inventory file, it only gets executed on one of the servers.

ansible hosts behind NAT proxy

Solution

The problem is that all the hosts have the same IP address. You need to use a unique name, so Ansible can identify each host separately.

I updated the inventory file as follows to get it to work.

[web]
port4000 ansible_ssh_host=195.154.255.26 ansible_connection=ssh ansible_ssh_user=root ansible_ssh_pass=serverok123 ansible_port=4000
port4001 ansible_ssh_host=195.154.255.26 ansible_connection=ssh ansible_ssh_user=root ansible_ssh_pass=serverok123 ansible_port=4001

port4000, and port4001 can be any unique name that can be used to identify the hosts. If hosts have a unique hostname, you can use it.

After the change, I can execute commands on both hosts that are behind NAT port forwarding.

ansible 2 servers with different ports behind nat

Back to Ansible

Need help with Linux Server or WordPress? We can help!

Leave a Reply

Your email address will not be published. Required fields are marked *