# template + delegate\_to + ssh shared connections = closed connections

**URL:** <https://forum.ansible.com/t/template-delegate-to-ssh-shared-connections-closed-connections/18056>\
**Category:** Ansible Project\
**Tags:** rhel, ubuntu\
**Created:** [April 22, 2015, 7:59pm UTC](https://forum.ansible.com/t/template-delegate-to-ssh-shared-connections-closed-connections/18056 "2015-04-22T19:59:35Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mark5](https://avatars.discourse-cdn.com/v4/letter/m/90ced4/32.png) [@Mark5](https://forum.ansible.com/u/Mark5)\
**Post date:** [April 22, 2015, 7:59pm UTC](https://forum.ansible.com/t/template-delegate-to-ssh-shared-connections-closed-connections/18056/1 "2015-04-22T19:59:35Z")

</div>

Finding when I’m delegating the template function to a single host, I end up with SSH Error Shared connection to xxxxxx: Connection closed.

Maybe one or two servers will make it through fine, but the other 5 or so will fail.

This is between Ubuntu (ansible 1.9.0.1) and RHEL 6.x servers.

The objective is to install a configuration file onto a monitoring server for each host in the inventory. the target file name is {{ ansible\_fqdn }}.cfg. I solved this by establishing another role and setting serial: 1 in the playbook, but somewhat of a pain.

And yes, my monitoring server is also in that list to generate a configuration file for itself (as it runs other services).

Anyone else see this or able to reproduce?

---

<div class="post-metadata">

**Author:** ![Cory\_Wagner](https://avatars.discourse-cdn.com/v4/letter/c/9de053/32.png) [@Cory\_Wagner](https://forum.ansible.com/u/Cory_Wagner)\
**Post date:** [April 30, 2015, 2:28pm UTC](https://forum.ansible.com/t/template-delegate-to-ssh-shared-connections-closed-connections/18056/2 "2015-04-30T14:28:24Z")

</div>

I noticed the same issue yesterday with 1.9.0.1 and 1.9.1. I have one role that runs a bunch of tasks from the app server and delegates the tasks to another node. It runs through a handful of tasks and then some or all hosts will randomly fail on various taks, like this:

fatal: [mdmsapp01 → cnsasg2] =\> SSH Error: Shared connection to cnsasg2 closed.  
It is sometimes useful to re-run the command using -vvvv, which prints SSH debug output to help diagnose the issue.  
fatal: [mdmclwapp1 → cnsasg2] =\> SSH Error: Shared connection to cnsasg2 closed.  
It is sometimes useful to re-run the command using -vvvv, which prints SSH debug output to help diagnose the issue.  
fatal: [mdmngaapp1 → cnsasg2] =\> SSH Error: Shared connection to cnsasg2 closed.  
It is sometimes useful to re-run the command using -vvvv, which prints SSH debug output to help diagnose the issue.  
fatal: [mdmkeuapp1 → cnsasg2] =\> SSH Error: Shared connection to cnsasg2 closed.  
It is sometimes useful to re-run the command using -vvvv, which prints SSH debug output to help diagnose the issue.  
fatal: [mdmvecapp1 → cnsasg2] =\> SSH Error: Shared connection to cnsasg2 closed.  
It is sometimes useful to re-run the command using -vvvv, which prints SSH debug output to help diagnose the issue.

FATAL: all hosts have already failed – aborting

---

<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:** [April 30, 2015, 2:35pm UTC](https://forum.ansible.com/t/template-delegate-to-ssh-shared-connections-closed-connections/18056/3 "2015-04-30T14:35:53Z")

</div>

this sounds like a probably race condition with the per host forks  
interfering with each other, try setting serial: 1 to confirm.

---

<div class="post-metadata">

**Author:** ![Cory\_Wagner](https://avatars.discourse-cdn.com/v4/letter/c/9de053/32.png) [@Cory\_Wagner](https://forum.ansible.com/u/Cory_Wagner)\
**Post date:** [May 11, 2015, 2:47pm UTC](https://forum.ansible.com/t/template-delegate-to-ssh-shared-connections-closed-connections/18056/4 "2015-05-11T14:47:14Z")

</div>

I didn’t try with serial, but I have not had an issue when running the same playbook against one or two delegates.

---

<div class="post-metadata">

**Author:** ![Christian\_Rusa1](https://avatars.discourse-cdn.com/v4/letter/c/b4bc9f/32.png) [@Christian\_Rusa1](https://forum.ansible.com/u/Christian_Rusa1)\
**Post date:** [May 29, 2015, 6:30am UTC](https://forum.ansible.com/t/template-delegate-to-ssh-shared-connections-closed-connections/18056/5 "2015-05-29T06:30:34Z")

</div>

I can confirm this problem.

In my case it is a playbook which creates a configuration file for the server on the backupserver.

If I run this playbook against 1 host it works.  
If I run it against a group of hosts it fails.  
If I run it against a group of host using -f 1 as parameter it works.

Error is always the SSH Error: Shared connection to xxxxx closed.

Ansible version is 1.9-0.git201503262158~unstable

---

<div class="post-metadata">

**Author:** ![Shay\_Rojansky](https://avatars.discourse-cdn.com/v4/letter/s/838e76/32.png) [@Shay\_Rojansky](https://forum.ansible.com/u/Shay_Rojansky)\
**Post date:** [June 8, 2015, 5:00pm UTC](https://forum.ansible.com/t/template-delegate-to-ssh-shared-connections-closed-connections/18056/6 "2015-06-08T17:00:30Z")

</div>

I have this issue as well: running a play which delegates some action to the same host pretty consistently fails with the above error. Findings are exactly the same as below, including the -f 1 behavior.

Any ideas on workarounds?

---

<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:** [June 8, 2015, 5:02pm UTC](https://forum.ansible.com/t/template-delegate-to-ssh-shared-connections-closed-connections/18056/7 "2015-06-08T17:02:25Z")

</div>

have you tried switching to paramiko as your connection?

---

<div class="post-metadata">

**Author:** ![Shay\_Rojansky](https://avatars.discourse-cdn.com/v4/letter/s/838e76/32.png) [@Shay\_Rojansky](https://forum.ansible.com/u/Shay_Rojansky)\
**Post date:** [June 8, 2015, 5:05pm UTC](https://forum.ansible.com/t/template-delegate-to-ssh-shared-connections-closed-connections/18056/8 "2015-06-08T17:05:01Z")

</div>

I was just looking into that, is that a reliable workaround?

Will try soon and report the results.

---

<div class="post-metadata">

**Author:** ![Shay\_Rojansky](https://avatars.discourse-cdn.com/v4/letter/s/838e76/32.png) [@Shay\_Rojansky](https://forum.ansible.com/u/Shay_Rojansky)\
**Post date:** [June 10, 2015, 9:39am UTC](https://forum.ansible.com/t/template-delegate-to-ssh-shared-connections-closed-connections/18056/9 "2015-06-10T09:39:42Z")

</div>

I can confirm that switching to paramiko appears to solve the problem in my case. This seems like a bit of a shame openssh is the recommended way to go (more features).

---

<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:** [June 10, 2015, 2:11pm UTC](https://forum.ansible.com/t/template-delegate-to-ssh-shared-connections-closed-connections/18056/10 "2015-06-10T14:11:48Z")

</div>

the issue is that under normal operation you are hitting different  
hosts, when delegating you may have several forks hitting the same  
host, which when you factor in control master ssh sockets, it can get  
complicated. paramiko avoids this by not having a shared resource.

in 2.0 we've added some code that does a better job of dealing with  
this multiconcurrency when using ssh.

---

<div class="post-metadata">

**Author:** ![Shay\_Rojansky](https://avatars.discourse-cdn.com/v4/letter/s/838e76/32.png) [@Shay\_Rojansky](https://forum.ansible.com/u/Shay_Rojansky)\
**Post date:** [June 10, 2015, 4:31pm UTC](https://forum.ansible.com/t/template-delegate-to-ssh-shared-connections-closed-connections/18056/11 "2015-06-10T16:31:16Z")

</div>

Great to hear, thanks!

---

<div class="post-metadata">

**Author:** ![Shay\_Rojansky](https://avatars.discourse-cdn.com/v4/letter/s/838e76/32.png) [@Shay\_Rojansky](https://forum.ansible.com/u/Shay_Rojansky)\
**Post date:** [June 15, 2015, 1:22pm UTC](https://forum.ansible.com/t/template-delegate-to-ssh-shared-connections-closed-connections/18056/12 "2015-06-15T13:22:45Z")

</div>

FYI, after working with paramiko for a while I still have issues when delegating multiple tasks to the same host, this time with the error “Error reading SSH protocol banner”.

---

<div class="post-metadata">

**Author:** ![Nathan\_Brown](https://avatars.discourse-cdn.com/v4/letter/n/8edcca/32.png) [@Nathan\_Brown](https://forum.ansible.com/u/Nathan_Brown)\
**Post date:** [July 2, 2015, 1:01am UTC](https://forum.ansible.com/t/template-delegate-to-ssh-shared-connections-closed-connections/18056/13 "2015-07-02T01:01:55Z")

</div>

I ran into this today and played around with different server and client versions of OpenSSH. My environment consists of servers running Ubuntu 14.04 and clients running OSX.

If you run OSX, this problem will go away if you `brew install openssh`.

On the server I tested these openssh versions:

- Default: OpenSSH\_6.6.1p1 Ubuntu-2ubuntu2, OpenSSL 1.0.1f 6 Jan 2014
- Latest: OpenSSH\_6.9p1, OpenSSL 1.0.1f 6 Jan 2014

On the client I tested these openssh clients:

- Default: OpenSSH\_6.2p2, OSSLShim 0.9.8r 8 Dec 2011
- Latest (brew): OpenSSH\_6.8p1, OpenSSL 1.0.2a 19 Mar 2015

---

<div class="post-metadata">

**Author:** ![Nathan\_Brown](https://avatars.discourse-cdn.com/v4/letter/n/8edcca/32.png) [@Nathan\_Brown](https://forum.ansible.com/u/Nathan_Brown)\
**Post date:** [July 2, 2015, 5:11pm UTC](https://forum.ansible.com/t/template-delegate-to-ssh-shared-connections-closed-connections/18056/14 "2015-07-02T17:11:27Z")

</div>

Actually I was wrong. Forgot to tear town my control sockets when swapping server versions.

This is entirely the fault of the ConsoleKit patch, at least for Ubuntu 14.04

[https://bugs.launchpad.net/ubuntu/+source/openssh/+bug/1334916/](https://bugs.launchpad.net/ubuntu/+source/openssh/+bug/1334916/)

---

<div class="post-metadata">

**Author:** ![Shay\_Rojansky](https://avatars.discourse-cdn.com/v4/letter/s/838e76/32.png) [@Shay\_Rojansky](https://forum.ansible.com/u/Shay_Rojansky)\
**Post date:** [July 5, 2015, 1:19pm UTC](https://forum.ansible.com/t/template-delegate-to-ssh-shared-connections-closed-connections/18056/15 "2015-07-05T13:19:54Z")

</div>

Thanks for pointing out the bug Nathan! I’ll add my vote.

---

<div class="post-metadata">

**Author:** ![Roger\_Sherman1](https://avatars.discourse-cdn.com/v4/letter/r/c68b51/32.png) [@Roger\_Sherman1](https://forum.ansible.com/u/Roger_Sherman1)\
**Post date:** [August 15, 2015, 4:58am UTC](https://forum.ansible.com/t/template-delegate-to-ssh-shared-connections-closed-connections/18056/16 "2015-08-15T04:58:19Z")

</div>

I’m having this same issue. Is there any fix for this yet, or is it just use the workaround of using paramiko or just doing one node at a time?

---

<div class="post-metadata">

**Author:** ![Chris\_May](https://avatars.discourse-cdn.com/v4/letter/c/ecc23a/32.png) [@Chris\_May](https://forum.ansible.com/u/Chris_May)\
**Post date:** [October 2, 2015, 10:16am UTC](https://forum.ansible.com/t/template-delegate-to-ssh-shared-connections-closed-connections/18056/17 "2015-10-02T10:16:18Z")

</div>

–user root

For me at least, and I know this won’t be available for everyone seems to get around this particular issue.
