+

Search Tips   |   Advanced Search

Developing network plugins

You can extend the existing network modules with custom plugins in your collection.


Network connection plugins

Each network connection plugin has a set of its own plugins which provide a specification of the connection for a particular set of devices. The specific plugin used is selected at runtime based on the value of the ansible_network_os variable assigned to the host. This variable should be set to the same value as the name of the plugin to be loaded. Thus, ansible_network_os=nxos will try to load a plugin in a file named nxos.py, so it is important to name the plugin in a way that will be sensible to users.

Public methods of these plugins may be called from a module or module_utils with the connection proxy object just as other connection methods can. The following is a very simple example of using such a call in a module_utils file so it may be shared with other modules.


Developing httpapi plugins

httpapi plugins serve as adapters for various HTTP(S) APIs for use with the httpapi connection plugin. They should implement a minimal set of convenience methods tailored to the API you are attempting to use.

Specifically, there are a few methods that the httpapi connection plugin expects to exist.


Making requests

The httpapi connection plugin has a send() method, but an httpapi plugin needs a send_request(self, data, **message_kwargs) method as a higher-level wrapper to send(). This method should prepare requests by adding fixed values like common headers or URL root paths. This method may do more complex work such as turning data into formatted payloads, or determining which path or method to request. It may then also unpack responses to be more easily consumed by the caller.


Authenticating

By default, all requests will authenticate with HTTP Basic authentication. If a request can return some kind of token to stand in place of HTTP Basic, the update_auth(self, response, response_text) method should be implemented to inspect responses for such tokens. If the token is meant to be included with the headers of each request, it is sufficient to return a dictionary which will be merged with the computed headers for each request. The default implementation of this method does exactly this for cookies. If the token is used in another way, say in a query string, you should instead save that token to an instance variable, where the send_request() method (above) can add it to each request

If instead an explicit login endpoint needs to be requested to receive an authentication token, the login(self, username, password) method can be implemented to call that endpoint. If implemented, this method will be called once before requesting any other resources of the server. By default, it will also be attempted once when a HTTP 401 is returned from a request.

Similarly, logout(self) can be implemented to call an endpoint to invalidate and/or release the current token, if such an endpoint exists. This will be automatically called when the connection is closed (and, by extension, when reset).


Error handling

The handle_httperror(self, exception) method can deal with status codes returned by the server. The return value indicates how the plugin will continue with the request:

For example httpapi plugins, see the source code for the httpapi plugins included with Ansible Core.


Developing NETCONF plugins

The netconf connection plugin provides a connection to remote devices over the SSH NETCONF subsystem. Network devices typically use this connection plugin to send and receive RPC calls over NETCONF.

The netconf connection plugin uses the ncclient Python library under the hood to initiate a NETCONF session with a NETCONF-enabled remote network device. ncclient also executes NETCONF RPC requests and receives responses. You must install the ncclient on the local Ansible controller.

To use the netconf connection plugin for network devices that support standard NETCONF (RFC 6241) operations such as get, get-config, edit-config, set ansible_network_os=default. You can use netconf_get, netconf_config and netconf_rpc modules to talk to a NETCONF enabled remote host.

As a contributor and user, you should be able to use all the methods under the NetconfBase class if your device supports standard NETCONF. You can contribute a new plugin if the device you are working with has a vendor specific NETCONF RPC. To support a vendor specific NETCONF RPC, add the implementation in the network OS specific NETCONF plugin.

For Junos for example:


Developing network_cli plugins

The network_cli connection type uses paramiko_ssh under the hood which creates a pseudo terminal to send commands and receive responses. network_cli loads two platform specific plugins based on the value of ansible_network_os:

To contribute a new network operating system to work with the network_cli connection, implement the cliconf and terminal plugins for that network OS.

The plugins can reside in:

The user can also set the DEFAULT_CLICONF_PLUGIN_PATH to configure the cliconf plugin path.

After adding the cliconf and terminal plugins in the expected locations, users can:


Developing cli_parser plugins in a collection

You can use cli_parse as an entry point for a cli_parser plugin in your own collection.

The following sample shows the start of a custom cli_parser plugin:

The following task uses this custom cli_parser plugin:

To develop a custom plugin: - Each cli_parser plugin requires a CliParser class. - Each cli_parser plugin requires a parse function. - Always return a dictionary with errors or parsed. - Place the custom cli_parser in plugins/cli_parsers directory of the collection. - See the current cli_parsers for examples to follow.


See also

Next Previous