# Reuse one http connection for multiple tasks

**URL:** <https://forum.ansible.com/t/reuse-one-http-connection-for-multiple-tasks/30753>\
**Category:** Ansible Project\
**Created:** [July 11, 2019, 12:30pm UTC](https://forum.ansible.com/t/reuse-one-http-connection-for-multiple-tasks/30753 "2019-07-11T12:30:42Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alexandru\_Obretin](https://avatars.discourse-cdn.com/v4/letter/a/b9bd4f/32.png) [@Alexandru\_Obretin](https://forum.ansible.com/u/Alexandru_Obretin)\
**Post date:** [July 11, 2019, 12:30pm UTC](https://forum.ansible.com/t/reuse-one-http-connection-for-multiple-tasks/30753/1 "2019-07-11T12:30:42Z")

</div>

Hello,

I am working on a project that wants to provide ansible support for a solution that exposes a REST API. Over this API I designed an intermediate python layer to translate the yaml playbook into REST calls.

In my case, the playbook is composed of one task executed multiple times. During execution, I open a http connection to the actual target equipment using the python library requests, get an authentication token and send http calls. I noticed in the log files that although my fetch authentication token mechanism is a singleton class and all the tasks are executed against the same IP address, for each task the session is recreated and a new token is fetched.

Is there a way to reuse the http connection within the task list?

I have seen a similar post called: **connection caching throughout a play** but I hope there might be a workaround.

Regards,  
Alex

---

<div class="post-metadata">

**Author:** ![goneri](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/goneri/32/1023_2.png) [@goneri](https://forum.ansible.com/u/goneri)\
**Post date:** [July 12, 2019, 9:26am UTC](https://forum.ansible.com/t/reuse-one-http-connection-for-multiple-tasks/30753/2 "2019-07-12T09:26:28Z")

</div>

Alexandru Obretin \<alexandru.obretin93@gmail.com\> writes:

> Hello,
> 
> I am working on a project that wants to provide ansible support for a  
> solution that exposes a REST API. Over this API I designed an intermediate  
> python layer to translate the yaml playbook into REST calls.
> 
> In my case, the playbook is composed of one task executed multiple times.  
> During execution, I open a http connection to the actual target equipment  
> using the python library requests, get an authentication token and send  
> http calls. I noticed in the log files that although my fetch  
> authentication token mechanism is a singleton class and all the tasks are  
> executed against the same IP address, for each task the session is  
> recreated and a new token is fetched.
> 
> Is there a way to reuse the http connection within the task list?
> 
> I have seen a similar post called: \*connection caching throughout a play \*but  
> I hope there might be a workaround.

Hi Alexandru,

You can use a HttpApi plugin for that. See:

&nbsp;&nbsp;[https://docs.ansible.com/ansible/latest/plugins/httpapi.html](https://docs.ansible.com/ansible/latest/plugins/httpapi.html)

Best regards,
