mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-05-07 13:52:54 +00:00
replaces NetCli in network with CliBase in shell
This doesn't change any of the behavior but moves NetCli to CliBase and relcates the object to the shell module
This commit is contained in:
@@ -32,7 +32,6 @@ from ansible.module_utils.basic import AnsibleModule
|
||||
from ansible.module_utils.basic import env_fallback, get_exception
|
||||
from ansible.module_utils.netcli import Cli, Command
|
||||
from ansible.module_utils.netcfg import Config
|
||||
from ansible.module_utils.shell import Shell, ShellError, HAS_PARAMIKO
|
||||
|
||||
NET_TRANSPORT_ARGS = dict(
|
||||
host=dict(required=True),
|
||||
@@ -160,63 +159,6 @@ class NetworkModule(AnsibleModule):
|
||||
exc = get_exception()
|
||||
self.fail_json(msg=exc.message)
|
||||
|
||||
class NetCli(object):
|
||||
"""Basic paramiko-based ssh transport any NetworkModule can use."""
|
||||
|
||||
def __init__(self):
|
||||
if not HAS_PARAMIKO:
|
||||
raise NetworkError(
|
||||
msg='paramiko is required but does not appear to be installed. '
|
||||
'It can be installed using `pip install paramiko`'
|
||||
)
|
||||
|
||||
self.shell = None
|
||||
self._connected = False
|
||||
self.default_output = 'text'
|
||||
|
||||
def connect(self, params, kickstart=True, **kwargs):
|
||||
host = params['host']
|
||||
port = params.get('port') or 22
|
||||
|
||||
username = params['username']
|
||||
password = params.get('password')
|
||||
key_file = params.get('ssh_keyfile')
|
||||
timeout = params['timeout']
|
||||
|
||||
try:
|
||||
self.shell = Shell(
|
||||
kickstart=kickstart,
|
||||
prompts_re=self.CLI_PROMPTS_RE,
|
||||
errors_re=self.CLI_ERRORS_RE,
|
||||
)
|
||||
self.shell.open(
|
||||
host, port=port, username=username, password=password,
|
||||
key_filename=key_file, timeout=timeout,
|
||||
)
|
||||
except ShellError:
|
||||
exc = get_exception()
|
||||
raise NetworkError(
|
||||
msg='failed to connect to %s:%s' % (host, port), exc=str(exc)
|
||||
)
|
||||
|
||||
self._connected = True
|
||||
|
||||
def disconnect(self, **kwargs):
|
||||
self.shell.close()
|
||||
self._connected = False
|
||||
|
||||
def authorize(self, params, **kwargs):
|
||||
passwd = params['auth_pass']
|
||||
self.execute(Command('enable', prompt=self.NET_PASSWD_RE, response=passwd))
|
||||
|
||||
def execute(self, commands, **kwargs):
|
||||
try:
|
||||
return self.shell.send(commands)
|
||||
except ShellError:
|
||||
exc = get_exception()
|
||||
raise NetworkError(exc.message, commands=commands)
|
||||
|
||||
|
||||
def register_transport(transport, default=False):
|
||||
def register(cls):
|
||||
NET_CONNECTIONS[transport] = cls
|
||||
|
||||
Reference in New Issue
Block a user