connections

Collection of task plugins to work with Nornir hosts’ connections

Sample usage

Code to invoke connections task plugins:

from nornir import InitNornir

from nornir_salt.plugins.tasks import connections

nr = InitNornir(config_file="config.yaml")

# get a list of connection
connections_active = nr.run(
    task=connections,
    call="ls"
)

# close all connections
conn_close = nr.run(
    task=connections,
    call="close"
)

# connect netmiko connections
connections_connect = nr.run(
    task=connections,
    call="open",
    conn_name="netmiko",
    username="user123",
    password="pass123",
    reconnect=[
        {
            "username": "user321",
            "password": "pass321",
        },
        "local_creds",
    ]
)

API reference

nornir_salt.plugins.tasks.connections.connections(task, call, **kwargs)

Dispatcher function to call one of the functions.

Parameters
  • call – (str) nickname of function to call

  • arg – (list) function arguments

  • kwargs – (dict) function key-word arguments

Returns

call function execution results

Call function nicknames:

  • ls - calls conn_list task

  • close - calls conn_close task

  • open - calls conn_open task

nornir_salt.plugins.tasks.connections.conn_list(task, conn_name: str = 'all') list

Function to list host’s active connections.

Parameters

conn_name – name of connection to list, default is “all”

Returns

list of hosts’ connections

nornir_salt.plugins.tasks.connections.conn_close(task, conn_name: str = 'all') list

Task to close host’s connections.

Parameters

conn_name – name of connection to close, default is “all”

Returns

list of connections closed

nornir_salt.plugins.tasks.connections.conn_open(task, conn_name, host: Optional[nornir.core.inventory.Host] = None, hostname: Optional[str] = None, username: Optional[str] = None, password: Optional[str] = None, port: Optional[int] = None, platform: Optional[str] = None, extras: Optional[Dict[str, Any]] = None, default_to_host_attributes: bool = True, close_open: bool = False, reconnect: list = None, raise_on_error: bool = False)

Helper function to open connections to hosts. Supports reconnect logic retrying different connection parameters.

Parameters
  • conn_name – name of connection to open

  • hostname – hostname or ip address to connect to

  • username – username to use to open the connection

  • password – password to use to open the connection

  • port – port to use to open the connection

  • platform – platform name connection parameter

  • extras – connection plugin extras parameters

  • default_to_host_attributes – on True uses host’s inventory data for not supplied arguments

  • close_open – if True, closes already open connection and connects again

  • reconnect – list of parameters to use to try connecting to device if primary set of parameters fails. If reconnect item is a dictionary, it is supplied as **kwargs to host open connection call. Alternatively, reconnect item can refer to a name of credentials set from inventory data credentials section within host, groups or defaults section.

  • raise_on_error – raises error if not able to establish connection even after trying all reconnect parameters, used to signal exception to parent function e.g. RetryRunner

  • host – Nornir Host object supplied by RetryRunner

Returns

boolean, True on success

Sample reconnect list content:

[
    {
        "username": "foo123",
        "port": "24",
        "password": "123foo",
    },
    "deprecated_creds",
    "local_account"
]

Where deprecated_creds and local_account could be stored in Nornir inventory default data credentials section:

defaults:
  data:
    credentials:
      deprecated_creds:
        password: foo
        username: bar
      local_account:
        password: nornir
        username: nornir