# Unable to reuse ssh connections in Ansible despite PIPELINING = True

**URL:** <https://forum.ansible.com/t/unable-to-reuse-ssh-connections-in-ansible-despite-pipelining-true/35539>\
**Category:** Ansible Project\
**Created:** [July 23, 2021, 8:19am UTC](https://forum.ansible.com/t/unable-to-reuse-ssh-connections-in-ansible-despite-pipelining-true/35539 "2021-07-23T08:19:26Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Easy\_King](https://avatars.discourse-cdn.com/v4/letter/e/c2a13f/32.png) [@Easy\_King](https://forum.ansible.com/u/Easy_King)\
**Post date:** [July 23, 2021, 8:19am UTC](https://forum.ansible.com/t/unable-to-reuse-ssh-connections-in-ansible-despite-pipelining-true/35539/1 "2021-07-23T08:19:26Z")

</div>

My corporate firewall policy allows only 20 connections per minute 60 seconds between the same source and destinations.

Owing to this the ansible play hangs after a while.

I would like multiple tasks to use the same ssh session rather than creating new sessions. For this purpose i set the below pipelining = True in the local folder ansible.cfg as well as in the command line.

cat /opt/automation/startservices/ansible.cfg

[defaults]  
host\_key\_checking = False  
gathering = smart  
[ssh\_connection]  
ssh\_args = -o ControlMaster=auto -o ControlPersist=600s  
control\_path = %(directory)s/%%h-%%r  
pipelining = True

ANSIBLE\_SSH\_PIPELINING=0 ansible-playbook -i /opt/automation/startservices/finalallmw.hosts /opt/automation/startservices/va\_action.yml -e ‘{ dest\_host: myremotehost7 }’ -e dest\_user=oracle

The playbook is too big to be shared here but it is this task which loops and this is where it hangs due to more than 20 ssh connections in 60 seconds.

171 - name: Copying from “{{ inventory\_hostname }}” to this ansible server.  
172 synchronize:  
173 src: “{{ item.path }}”  
174 dest: “{{ playbook\_dir }}/homedirbackup/{{ inventory\_hostname }}/{{ dtime }}/”  
175 mode: pull  
176 copy\_links: yes  
177 with\_items:  
178 - “{{ to\_copy.files }}”

With the pipelining settings set; my play still hangs after 20 connections.

Below are the playbook settings:

45 hosts: “{{ groups[‘dest\_nodes’] | default(groups[‘all’]) }}”  
46 user: “{{ USER | default(dest\_user) }}”  
47 any\_errors\_fatal: True  
49 gather\_facts: false  
51 tags: always  
52  
53 vars:  
54 ansible\_host\_key\_checking: false  
55 ansible\_ssh\_extra\_args: -o StrictHostKeyChecking=no -o ConnectionAttempts=5

Can you please suggest any solution to the issue on the ansible side where all tasks use the same ssh session and is pipelining not working here?

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [July 23, 2021, 8:25am UTC](https://forum.ansible.com/t/unable-to-reuse-ssh-connections-in-ansible-despite-pipelining-true/35539/2 "2021-07-23T08:25:41Z")

</div>

> My corporate firewall policy allows only 20 connections per minute 60 seconds between the same source and destinations.

Sounds to me like a ridiculous policy. Ask for an exception instead of trying to throttle Ansible.

Regads  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![Antony\_Stone](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Antony\_Stone](https://forum.ansible.com/u/Antony_Stone)\
**Post date:** [July 23, 2021, 8:33am UTC](https://forum.ansible.com/t/unable-to-reuse-ssh-connections-in-ansible-despite-pipelining-true/35539/3 "2021-07-23T08:33:10Z")

</div>

I agree. This would cause problems just for a large number of standard  
websites, unless your IT networking people are relying on browsers using  
persistent connections, and even then, static content, images, and dynamic  
content are often going to be supplied by different parts of a CDN.

Antony.

---

<div class="post-metadata">

**Author:** ![Matthew\_Davis](https://avatars.discourse-cdn.com/v4/letter/m/22d042/32.png) [@Matthew\_Davis](https://forum.ansible.com/u/Matthew_Davis)\
**Post date:** [July 4, 2023, 7:50am UTC](https://forum.ansible.com/t/unable-to-reuse-ssh-connections-in-ansible-despite-pipelining-true/35539/4 "2023-07-04T07:50:07Z")

</div>

Hi,

Did you find a solution for this?

I don’t think pipelining will actually result in reusing the same SSH session across the play. It will reduce the number of SSH sessions per task, but I think it will still result in many new SSH sessions.

I can’t figure out how to reuse sessions for a non-SSH connector.

Regards,  
Matt

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [July 5, 2023, 1:29pm UTC](https://forum.ansible.com/t/unable-to-reuse-ssh-connections-in-ansible-despite-pipelining-true/35539/5 "2023-07-05T13:29:29Z")

</div>

A few things:  
- pipelining is not about connection reuse but about writing to disk  
- for ssh connection plugin, the control persist settings are what  
reuse connections/authentication, but not sessions  
- the synchronize action does it's own connection handling

---

<div class="post-metadata">

**Author:** ![Mark\_Foster](https://avatars.discourse-cdn.com/v4/letter/m/898d66/32.png) [@Mark\_Foster](https://forum.ansible.com/u/Mark_Foster)\
**Post date:** [July 5, 2023, 5:09pm UTC](https://forum.ansible.com/t/unable-to-reuse-ssh-connections-in-ansible-despite-pipelining-true/35539/6 "2023-07-05T17:09:44Z")

</div>

Consider you might have conflicting settings in your ssh config (~/.ssh/config and/or /etc/ssh/ssh\_config)?

Here’s what I have…  
Host \*  
ControlMaster auto  
ControlPath ~/.ansible/cp/%h-%r

ControlPersist 10m

control\_path probably needs to match ControlPath. Not sure about the others.

---

<div class="post-metadata">

**Author:** ![Matthew\_Davis](https://avatars.discourse-cdn.com/v4/letter/m/22d042/32.png) [@Matthew\_Davis](https://forum.ansible.com/u/Matthew_Davis)\
**Post date:** [July 12, 2023, 10:22am UTC](https://forum.ansible.com/t/unable-to-reuse-ssh-connections-in-ansible-despite-pipelining-true/35539/7 "2023-07-12T10:22:00Z")

</div>

> - for ssh connection plugin, the control persist settings are what reuse connections/authentication, but not sessions

Is there a mechanism to re-use sessions? Or a mechanism to re-use connections for non-ssh connection plugins?

Thanks,  
Matt

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [July 12, 2023, 2:07pm UTC](https://forum.ansible.com/t/unable-to-reuse-ssh-connections-in-ansible-despite-pipelining-true/35539/8 "2023-07-12T14:07:01Z")

</div>

Kind of, there is ansible-connection but it can be complicated. I've  
been toying around with the idea of a 'persistent: no|yes' keyword to  
allow for the use/reuse of persistent sessions. This would first look  
at the connection plugin for support and fallback to a connection  
manager+ansible-connection on controller otherwise.

---

<div class="post-metadata">

**Author:** ![Matthew\_Davis](https://avatars.discourse-cdn.com/v4/letter/m/22d042/32.png) [@Matthew\_Davis](https://forum.ansible.com/u/Matthew_Davis)\
**Post date:** [July 12, 2023, 11:17pm UTC](https://forum.ansible.com/t/unable-to-reuse-ssh-connections-in-ansible-despite-pipelining-true/35539/9 "2023-07-12T23:17:04Z")

</div>

Why would you need to check whether the connection plugin supports such a new feature? ConnectionBase already has methods for \_connect, connected, reset and close, separate from exec\_command, put\_file and fetch\_file  
So wouldn’t this work for all connection plugins if Ansible just starts calling exec\_command and put\_file multiple times per connection?

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [July 13, 2023, 2:10pm UTC](https://forum.ansible.com/t/unable-to-reuse-ssh-connections-in-ansible-despite-pipelining-true/35539/10 "2023-07-13T14:10:35Z")

</div>

Ansible already does this per per task, this still just uses 'shared  
auth' in the case of ssh, not full session (unlike winrm/prsp). In any  
case this feature would be 'across tasks' or block of tasks.
