Ansible multiple hosts with port forwarding

sohail sahi picture sohail sahi · Oct 23, 2014 · Viewed 8.8k times · Source

I have hosts inventory with multiple hosts each with port forwarding, Hosts file is :

[all]

10.80.238.11:20003

10.80.238.11:20001

10.80.238.11:20007

10.80.238.11:20009

I am trying to ping them with a playbook, but always get response from first entry in this case 10.80.238.11:20003 not from others. Authentication is on place, whatever host I move to first place I get response from it but not others, my playbook is:

---
- hosts: all

  remote_user: root

  gather_facts: no

  tasks:

  - name: test connection

    ping:

Any idea how to fix this???

Answer

leucos picture leucos · Oct 23, 2014

I suppose that the port forwarding you're doing is for SSH.

So you have to tell ansible which ssh port to connect to. The problem is that all your hosts have the same IP. So you have to use hostnames, so Ansible can distinguish them.

Let's assume that you refer to host with SSH port forwarded to 2000X as hostX, then the right syntax to specify ssh port along with the host IP is :

host3 ansible_ssh_port=20003 ansible_ssh_host=10.80.238.11
host1 ansible_ssh_port=20001 ansible_ssh_host=10.80.238.11
host7 ansible_ssh_port=20007 ansible_ssh_host=10.80.238.11
host9 ansible_ssh_port=20009 ansible_ssh_host=10.80.238.11

You can then issue :

ansible host3 -m ping 

or even :

ansible all -m ping

Note that you should not create an all group since Ansible creates it automagically.