# Connect to host - SSH Tunnel

**URL:** <https://forum.ansible.com/t/connect-to-host-ssh-tunnel/20075>\
**Category:** Ansible Project\
**Created:** [December 24, 2015, 12:49pm UTC](https://forum.ansible.com/t/connect-to-host-ssh-tunnel/20075 "2015-12-24T12:49:16Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![fabgcruz](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/fabgcruz/32/11002_2.png) [@fabgcruz](https://forum.ansible.com/u/fabgcruz)\
**Post date:** [December 24, 2015, 12:49pm UTC](https://forum.ansible.com/t/connect-to-host-ssh-tunnel/20075/1 "2015-12-24T12:49:16Z")

</div>

Hello,

I currently have a host that is accessed saw ssh tunnel, I need to manage that host with ansible. How can I do that?

To connect to the host I use the command: ssh -N -R 10000: localhost: 22 [user@hostX](mailto:user@hostX).

I added this command on a .sh file and run:

ssh -t user@otherserver “./file.sh”

Thanks.

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [December 24, 2015, 5:08pm UTC](https://forum.ansible.com/t/connect-to-host-ssh-tunnel/20075/2 "2015-12-24T17:08:16Z")

</div>

just setup this info in your hosts file:

hostx ansible\_ssh\_host=localhost:10000

then ansible will use the existing tunnel every time to get to hostx

---

<div class="post-metadata">

**Author:** ![fabgcruz](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/fabgcruz/32/11002_2.png) [@fabgcruz](https://forum.ansible.com/u/fabgcruz)\
**Post date:** [December 24, 2015, 10:07pm UTC](https://forum.ansible.com/t/connect-to-host-ssh-tunnel/20075/3 "2015-12-24T22:07:35Z")

</div>

Hello

Thanks for your reply, my scenario is as follows:

hostA — used as a bridge to access the hostX (local machine).

HostC — ansible server you need to manage hostX, perform installations and upgrades using ansible.

It is possible ?

Thank you so much.

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [December 24, 2015, 10:19pm UTC](https://forum.ansible.com/t/connect-to-host-ssh-tunnel/20075/4 "2015-12-24T22:19:58Z")

</div>

yes, just setup the tunnel and then use ansible as I mentioned in my  
previous email.

you can even use ansible to setup the tunnel with a play on the hostC

---

<div class="post-metadata">

**Author:** ![Tim\_Fletcher](https://avatars.discourse-cdn.com/v4/letter/t/b5e925/32.png) [@Tim\_Fletcher](https://forum.ansible.com/u/Tim_Fletcher)\
**Post date:** [December 28, 2015, 12:11am UTC](https://forum.ansible.com/t/connect-to-host-ssh-tunnel/20075/5 "2015-12-28T00:11:44Z")

</div>

Would using a netcat tunnel work for you?

We had a similar setup were I used to work and used ssh config to automate the tunnel usage.

Host targethoust  
HostName targethost  
User myuser  
ProxyCommand ssh proxyhost nc %h %p 2\> /dev/null

[http://backdrift.org/transparent-proxy-with-ssh](http://backdrift.org/transparent-proxy-with-ssh)

---

<div class="post-metadata">

**Author:** ![Alex\_Leonhardt](https://avatars.discourse-cdn.com/v4/letter/a/ec9cab/32.png) [@Alex\_Leonhardt](https://forum.ansible.com/u/Alex_Leonhardt)\
**Post date:** [December 28, 2015, 12:42pm UTC](https://forum.ansible.com/t/connect-to-host-ssh-tunnel/20075/6 "2015-12-28T12:42:37Z")

</div>

Never really looked into it, but if/when using netcat, does that mean that from your proxy host / bastion to the target host, the connection is in clear text? I would assume that to be the case?

Alex

---

<div class="post-metadata">

**Author:** ![Tim\_Fletcher](https://avatars.discourse-cdn.com/v4/letter/t/b5e925/32.png) [@Tim\_Fletcher](https://forum.ansible.com/u/Tim_Fletcher)\
**Post date:** [January 2, 2016, 11:53am UTC](https://forum.ansible.com/t/connect-to-host-ssh-tunnel/20075/7 "2016-01-02T11:53:54Z")

</div>

No the tunnel is setup from the bastion to the target to carry a second ssh connection from workstation to target.
