Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible multiple hosts with port forwarding

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???

like image 483
sohail sahi Avatar asked Oct 23 '14 11:10

sohail sahi


1 Answers

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.

like image 173
leucos Avatar answered Nov 04 '22 09:11

leucos