# Adding a new connection plugin

**URL:** <https://forum.ansible.com/t/adding-a-new-connection-plugin/27688>\
**Category:** Ansible Developer\
**Created:** [June 1, 2018, 7:29pm UTC](https://forum.ansible.com/t/adding-a-new-connection-plugin/27688 "2018-06-01T19:29:27Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![akhinextgen](https://avatars.discourse-cdn.com/v4/letter/a/a6a055/32.png) [@akhinextgen](https://forum.ansible.com/u/akhinextgen)\
**Post date:** [June 1, 2018, 7:29pm UTC](https://forum.ansible.com/t/adding-a-new-connection-plugin/27688/1 "2018-06-01T19:29:27Z")

</div>

Hi,

I am trying to write a connection plugin for enabling REST connections, and to that end,  
I looked at the existing connection plugins. One of the plugins that caught my attention is the network\_cli connection plugin. To understand the code better, I tried to trace the control flow from the nxos vlan module back to the network\_cli connection plugin. Here’s what I have gathered so far:  
nxos\_vlan ( **modules/network/nxos/nxos\_vlan** )

> 

load\_config ( **module\_utils/network/nxos/nxos** )

> 

connection.edit\_config  
Here **connection** is an object of the Connection class, imported from **module\_utils/connection**. However, **edit\_config** is found in the cliconf plugin ( **plugins/cliconf/nxos** ), which gets loaded from within the network\_cli connection\_plugin. Thus, it is unclear to me that edit\_config gets invoked on an object of the Connection class (different from the network\_cli Connection class), when the class does not even have this function defined in the first place.

In view of the above, I would be very thankful if anyone could clear my above confusion and also provide some pointers towards writing a fresh connection plugin for rest connections.

Thank you very much for your help!

---

<div class="post-metadata">

**Author:** ![ganeshrn](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/ganeshrn/32/1021_2.png) [@ganeshrn](https://forum.ansible.com/u/ganeshrn)\
**Post date:** [June 5, 2018, 4:58pm UTC](https://forum.ansible.com/t/adding-a-new-connection-plugin/27688/2 "2018-06-05T16:58:48Z")

</div>

There are multiple moving parts in this code flow.  
To start with network\_cli connection plugin is applicable for network platforms that support persistent  
connection with the remote host across task.

The method called on connection object (connection.edit\_config) is serialised in json string as per jsonrpc 2.0 spec [0]  
and is send over to socket [1]. The receiving end of the socket is a daemon process called “ansible-connection”  
which is spawned from task\_executor [2] at the time of connection creation to the remote host.

“ansible-connection” receives the json string and calls handle\_request method [3] on the object of JsonRpcServer class  
In the initialization steps, “ansible-connection” loads the connection plugin (“network-cli” plugin in this case based on the value of “ansible\_connection”)  
and registers it with object JsonRpcServer class [4].  
handle\_request() in turn decodes the json string to fetch the method name, arguments and invokes it on the object of Connection class defined in network\_cli [5]

network\_cli \_connect() method which is called at the time of connection initialization [6] & [7] handles the  
connection with the remote host and also loads the cliconf and terminal plugins within the Connection object  
based on the value of “network\_os” [8] (nxos in this case)

So the call connection.edit\_config ends up in Connection class defined in network\_cli as and “edit\_config” method isnot directly defined in this class it searches for the method with the same name in nxos cliconf and terminal plugin.  
Since edit\_config is defined in nxos cliconf plugin [9] it gets invoked.

nxos\_vlan → load\_config → connection.edit\_config → ansible-connection → network\_cli → nxos cliconf

> ------ module side code --------------------------------------| |--------------- controller code -----------------------------|

[0] [http://www.jsonrpc.org/specification](http://www.jsonrpc.org/specification)  
[1] [https://github.com/ansible/ansible/blob/devel/lib/ansible/module\_utils/connection.py#L158](https://github.com/ansible/ansible/blob/devel/lib/ansible/module_utils/connection.py#L158)  
[2] [https://github.com/ansible/ansible/blob/devel/lib/ansible/executor/task\_executor.py#L917](https://github.com/ansible/ansible/blob/devel/lib/ansible/executor/task_executor.py#L917)  
[3] [https://github.com/ansible/ansible/blob/devel/bin/ansible-connection#L123](https://github.com/ansible/ansible/blob/devel/bin/ansible-connection#L123)  
[4] [https://github.com/ansible/ansible/blob/devel/bin/ansible-connection#L89](https://github.com/ansible/ansible/blob/devel/bin/ansible-connection#L89)  
[5] [https://github.com/ansible/ansible/blob/devel/lib/ansible/utils/jsonrpc.py#L25](https://github.com/ansible/ansible/blob/devel/lib/ansible/utils/jsonrpc.py#L25)  
[6] [https://github.com/ansible/ansible/blob/devel/bin/ansible-connection#L87](https://github.com/ansible/ansible/blob/devel/bin/ansible-connection#L87)  
[7] [https://github.com/ansible/ansible/blob/devel/lib/ansible/plugins/connection/network\_cli.py#L287](https://github.com/ansible/ansible/blob/devel/lib/ansible/plugins/connection/network_cli.py#L287)  
[8] [https://github.com/ansible/ansible/blob/devel/lib/ansible/plugins/connection/network\_cli.py#L318](https://github.com/ansible/ansible/blob/devel/lib/ansible/plugins/connection/network_cli.py#L318)  
[9] [https://github.com/ansible/ansible/blob/devel/lib/ansible/plugins/connection/network\_cli.py#L225](https://github.com/ansible/ansible/blob/devel/lib/ansible/plugins/connection/network_cli.py#L225)

You can refer the httpapi proposal [here](https://github.com/ansible/proposals/issues/103) which deals with a similar requirement as yours.  
FYI httpapi connection support is present in devel branch and will be available with Ansible 2.6 version onwards

Hope this helps.

Regards,  
Ganesh
