# SSH inside a task

**URL:** <https://forum.ansible.com/t/ssh-inside-a-task/15294>\
**Category:** Ansible Project\
**Created:** [May 21, 2014, 3:31pm UTC](https://forum.ansible.com/t/ssh-inside-a-task/15294 "2014-05-21T15:31:01Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michael\_Peters](https://avatars.discourse-cdn.com/v4/letter/m/c2a13f/32.png) [@Michael\_Peters](https://forum.ansible.com/u/Michael_Peters)\
**Post date:** [May 21, 2014, 3:31pm UTC](https://forum.ansible.com/t/ssh-inside-a-task/15294/1 "2014-05-21T15:31:01Z")

</div>

I'm trying to ssh within an ansible task (sounds silly, but is  
actually part of a delegate\_to in a playbook that streams a backup  
from one server to another) but it just hangs.

I've broken it down into simple test case that fails (just hangs and a  
^C doesn't kill it) and was hoping someone could help me figure out  
why.

I can definitely execute the command (ssh 10.0.1.75 "ls /"') on the  
db2 server directly but not using ansible from the control server.

# test.yml

---

<div class="post-metadata">

**Author:** ![Christian\_Rusa](https://avatars.discourse-cdn.com/v4/letter/c/f0a364/32.png) [@Christian\_Rusa](https://forum.ansible.com/u/Christian_Rusa)\
**Post date:** [May 21, 2014, 5:09pm UTC](https://forum.ansible.com/t/ssh-inside-a-task/15294/2 "2014-05-21T17:09:30Z")

</div>

Have you already tried ssh -n?  
I think this could fix your problem as you are not running an interactive session.

---

<div class="post-metadata">

**Author:** ![Michael\_Peters](https://avatars.discourse-cdn.com/v4/letter/m/c2a13f/32.png) [@Michael\_Peters](https://forum.ansible.com/u/Michael_Peters)\
**Post date:** [May 21, 2014, 5:39pm UTC](https://forum.ansible.com/t/ssh-inside-a-task/15294/3 "2014-05-21T17:39:41Z")

</div>

Thanks for the suggestion, but unfortunately it doesn't work either.  
Still hangs in the same spot.

---

<div class="post-metadata">

**Author:** ![Michael\_Peters](https://avatars.discourse-cdn.com/v4/letter/m/c2a13f/32.png) [@Michael\_Peters](https://forum.ansible.com/u/Michael_Peters)\
**Post date:** [May 21, 2014, 5:55pm UTC](https://forum.ansible.com/t/ssh-inside-a-task/15294/4 "2014-05-21T17:55:48Z")

</div>

Btw, it also hangs when I use scp.

---

<div class="post-metadata">

**Author:** ![Michael\_Peters](https://avatars.discourse-cdn.com/v4/letter/m/c2a13f/32.png) [@Michael\_Peters](https://forum.ansible.com/u/Michael_Peters)\
**Post date:** [May 21, 2014, 9:03pm UTC](https://forum.ansible.com/t/ssh-inside-a-task/15294/5 "2014-05-21T21:03:03Z")

</div>

I changed pty to False in my ansible.cfg and it has fixed the problem.  
I feel slightly stupid...

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [May 21, 2014, 10:21pm UTC](https://forum.ansible.com/t/ssh-inside-a-task/15294/6 "2014-05-21T22:21:52Z")

</div>

Don’t do this 🙂

Use Ansible and if need be, configure jumphosts, or whatever, but Ansible shouldn’t ever _NEED_ to call ssh.

Ansible already has lots of wrappers for that.

A good pattern for checking for SSH being up is using the wait\_for module to check if the port is open (usually 22) followed by a pause task of a few seconds to make sure SSHd is ready.

Ready to go!

---

<div class="post-metadata">

**Author:** ![Michael\_Peters](https://avatars.discourse-cdn.com/v4/letter/m/c2a13f/32.png) [@Michael\_Peters](https://forum.ansible.com/u/Michael_Peters)\
**Post date:** [May 22, 2014, 12:41pm UTC](https://forum.ansible.com/t/ssh-inside-a-task/15294/7 "2014-05-22T12:41:06Z")

</div>

> Don't do this 🙂
> 
> Use Ansible and if need be, configure jumphosts, or whatever, but Ansible  
> shouldn't ever \*NEED\* to call ssh.

I agree it shouldn't be done in general, but I'm interested to see how  
you would have solved my initial, more complicated problem. I just  
simplified to the smallest thing that had the same symptoms for my  
email.  
What I was trying to do was run a backup on one server (that may or  
may not be managed by ansible) but stream that backup to the ansible  
managed server and run some other tasks on it. I think the original  
command was something like this:

&nbsp;&nbsp;&nbsp;&nbsp;innobackupex --stream=tar ./ | pigz | ssh user@ansible\_host "cat -

> /data/backups/backup.tar"

So I don't actually want the backup stored on the delegate server, but  
compressed and streamed to the target server that ansible is  
controlling.

I thought about using netcat, but that seemed more involved with  
having to control it on both ends.  
Any other thoughts on how to tackle this?

> Ansible already has lots of wrappers for that.

Even though I didn't want to have to write the backup to disk on the  
delegate server, I did try to use copy + delegate\_to, but now that I  
look back, that wasn't going to work.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [May 23, 2014, 12:21pm UTC](https://forum.ansible.com/t/ssh-inside-a-task/15294/8 "2014-05-23T12:21:57Z")

</div>

“I agree it shouldn’t be done in general, but I’m interested to see how  
you would have solved my initial, more complicated problem.”

Thanks for providing the actual example of what you are trying to do – this helps us understand more.

I’d probably start by trying to do something with the ‘script’ module and get it to the point where it’s not interactive, and go from there.
