# Share object between modules

**URL:** <https://forum.ansible.com/t/share-object-between-modules/20655>\
**Category:** Ansible Developer\
**Created:** [February 23, 2016, 7:16am UTC](https://forum.ansible.com/t/share-object-between-modules/20655 "2016-02-23T07:16:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nitin\_Kumar](https://avatars.discourse-cdn.com/v4/letter/n/c4cdca/32.png) [@Nitin\_Kumar](https://forum.ansible.com/u/Nitin_Kumar)\
**Post date:** [February 23, 2016, 7:16am UTC](https://forum.ansible.com/t/share-object-between-modules/20655/1 "2016-02-23T07:16:40Z")

</div>

Hi All,

We have different module to do different activity.  
[https://github.com/Juniper/ansible-junos-stdlib/tree/master/library](https://github.com/Juniper/ansible-junos-stdlib/tree/master/library)

Every time each modules/tasks will connect to the device (and close the connection).

So if we are using say 4 modules/tasks in our playbook, We are ideally connecting to Juniper devices 4 time (While starting connection each time lot of other process is followed)

That obviously will be impacting the performance.

Is there a way to share the connection object over the different modules?

Thanks

Nitin Kr

---

<div class="post-metadata">

**Author:** ![Adam\_Hamsik](https://avatars.discourse-cdn.com/v4/letter/a/8baadc/32.png) [@Adam\_Hamsik](https://forum.ansible.com/u/Adam_Hamsik)\
**Post date:** [February 23, 2016, 7:55am UTC](https://forum.ansible.com/t/share-object-between-modules/20655/2 "2016-02-23T07:55:06Z")

</div>

I think only way to work around this is somehow create persistent connection to junos device. Same way like ssh is doing it.

---

<div class="post-metadata">

**Author:** ![Mike\_Biancaniello](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@Mike\_Biancaniello](https://forum.ansible.com/u/Mike_Biancaniello)\
**Post date:** [February 23, 2016, 5:54pm UTC](https://forum.ansible.com/t/share-object-between-modules/20655/3 "2016-02-23T17:54:25Z")

</div>

First off, I don’t know that this would be possible with modules because, I believe, that the modules are executed outside of Ansible, even when they are run locally (this is why you can write the modules in bash, perl, tcl, etc). Therefore, sharing anything (other than text sent as a task\_arg) between them is not possible.

However, the Juniper-supplied modules only seem to support:  
commit, get\_config, get\_facts, install\_config, install\_os, rollback, shutdown, srx\_cluster, zeroize

So, most of your operations are probably going to be `install_config` (`install_config` does the `commit` for you so you don’t have to call that separately).

IF this is the case, you could always generate the bits of config from templates, then use the `assemble` ansible module to put them into a single file and then only call `install_config` the one time.

Unfortunately, if you are wanting to do things in between the juniper tasks (like check, verify, etc), then you might be able to do something with pickles and creating an action\_plugin or modifying the Juniper-supplied libraries.

I think the only way to do this would be to modify the Juniper-supplied libraries. Currently,
