Connecting to a remote server from local machine via ssh-tunnel

ansiblessh

I am running Ansible on my machine. And my machine does not have ssh access to the remote machine. Port 22 connection originating from local machine are blocked by the institute firewall. But I have access to a machine (ssh-tunnel), through which I can login to the remote machine. Now is there a way we can run ansible playbook from local machine on remote hosts.

In a way is it possible to make Ansible/ssh connect to the remote machine, via ssh-tunnel. But not exactly login to ssh-tunnel. The connection will pass through the tunnel.

Other way is I can install ansible on ssh-tunnel, but that is not the desired and run plays from there. But that would not be a desired solution.

Please let me know if this is possible.

Best Answer

There are two ways to achieve this without install the Ansible on the ssh-tunnel machine.

Solution#1:

Use these variables in your inventory:

[remote_machine]
remote ansible_ssh_host=127.0.0.1 ansible_ssh_port=2222 ansible_ssh_user='username' ansible_ssh_private_key_file='/home/user/private_key'

hope you understand above parameters, if need help please ask in comments

Solution#2:

Create ~/.ssh/config file and add the following parameters:

####### Access to the Private Server through ssh-tunnel/bastion ########

Host ssh-tunnel-server
    HostName x.x.x.x
    StrictHostKeyChecking no
    User username
    ForwardAgent yes

Host private-server
  HostName y.y.y.y
  StrictHostKeyChecking no
  User username
  ProxyCommand ssh -q ssh-tunnel-server nc -q0 %h %p

Hope that help you, if you need any help, feel free to ask