Reuse one http connection for multiple tasks

21 views
Skip to first unread message

Alexandru Obretin

unread,
Jul 11, 2019, 8:30:42 AM7/11/19
to Ansible Project
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

Gonéri Le Bouder

unread,
Jul 12, 2019, 5:26:40 AM7/12/19
to Alexandru Obretin, Ansible Project
Alexandru Obretin <alexandru...@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:

https://docs.ansible.com/ansible/latest/plugins/httpapi.html

Best regards,
--
Gonéri Le Bouder
Reply all
Reply to author
Forward
0 new messages