mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-25 14:20:22 -07:00
Clarify exception handling in net modules (#15507)
* Clarify exception handling in EOS Also modify to EOS to standardize modules. It makes vimdiff a lot less angry * Move IOS exception handling into Cli * Move IOS-XR exception handling into Cli * Move JUNOS exception handling into Cli * Move NXOS exception handling into Cli And reorganize to make it match the other modules * Move OpenSwitch exception handling into Cli More speculative restructuring here
This commit is contained in:
parent
50792f46a5
commit
250b975704
6 changed files with 150 additions and 129 deletions
|
@ -19,10 +19,10 @@
|
|||
|
||||
import re
|
||||
|
||||
from ansible.module_utils.urls import fetch_url
|
||||
from ansible.module_utils.shell import Shell, HAS_PARAMIKO
|
||||
from ansible.module_utils.basic import AnsibleModule, env_fallback
|
||||
from ansible.module_utils.basic import AnsibleModule, env_fallback, get_exception
|
||||
from ansible.module_utils.shell import Shell, ShellError, HAS_PARAMIKO
|
||||
from ansible.module_utils.netcfg import parse
|
||||
from ansible.module_utils.urls import fetch_url
|
||||
|
||||
NET_PASSWD_RE = re.compile(r"[\r\n]?password: $", re.I)
|
||||
|
||||
|
@ -60,6 +60,7 @@ CLI_ERRORS_RE = [
|
|||
re.compile(r"unknown command")
|
||||
]
|
||||
|
||||
|
||||
def to_list(val):
|
||||
if isinstance(val, (list, tuple)):
|
||||
return list(val)
|
||||
|
@ -68,6 +69,7 @@ def to_list(val):
|
|||
else:
|
||||
return list()
|
||||
|
||||
|
||||
class Nxapi(object):
|
||||
|
||||
def __init__(self, module):
|
||||
|
@ -169,14 +171,19 @@ class Cli(object):
|
|||
key_filename = self.module.params['ssh_keyfile']
|
||||
|
||||
try:
|
||||
self.shell = Shell(prompts_re=CLI_PROMPTS_RE, errors_re=CLI_ERRORS_RE, kickstart=False)
|
||||
self.shell = Shell(kickstart=False, prompts_re=CLI_PROMPTS_RE, errors_re=CLI_ERRORS_RE)
|
||||
self.shell.open(host, port=port, username=username, password=password, key_filename=key_filename)
|
||||
except Exception, exc:
|
||||
msg = 'failed to connect to %s:%s - %s' % (host, port, str(exc))
|
||||
except ShellError:
|
||||
e = get_exception()
|
||||
msg = 'failed to connect to %s:%s - %s' % (host, port, str(e))
|
||||
self.module.fail_json(msg=msg)
|
||||
|
||||
def send(self, commands, encoding='text'):
|
||||
return self.shell.send(commands)
|
||||
try:
|
||||
return self.shell.send(commands)
|
||||
except ShellError:
|
||||
e = get_exception()
|
||||
self.module.fail_json(msg=e.message, commands=commands)
|
||||
|
||||
|
||||
class NetworkModule(AnsibleModule):
|
||||
|
@ -201,15 +208,17 @@ class NetworkModule(AnsibleModule):
|
|||
super(NetworkModule, self)._load_params()
|
||||
provider = self.params.get('provider') or dict()
|
||||
for key, value in provider.items():
|
||||
if key in NET_COMMON_ARGS.keys():
|
||||
if key in NET_COMMON_ARGS:
|
||||
if self.params.get(key) is None and value is not None:
|
||||
self.params[key] = value
|
||||
|
||||
def connect(self):
|
||||
if self.params['transport'] == 'nxapi':
|
||||
self.connection = Nxapi(self)
|
||||
else:
|
||||
self.connection = Cli(self)
|
||||
cls = globals().get(str(self.params['transport']).capitalize())
|
||||
try:
|
||||
self.connection = cls(self)
|
||||
except TypeError:
|
||||
e = get_exception()
|
||||
self.fail_json(msg=e.message)
|
||||
|
||||
self.connection.connect()
|
||||
|
||||
|
@ -218,6 +227,12 @@ class NetworkModule(AnsibleModule):
|
|||
|
||||
self._connected = True
|
||||
|
||||
def configure(self, commands):
|
||||
commands = to_list(commands)
|
||||
if self.params['transport'] == 'cli':
|
||||
return self.configure_cli(commands)
|
||||
else:
|
||||
return self.execute(commands, command_type='cli_conf')
|
||||
|
||||
def configure_cli(self, commands):
|
||||
commands = to_list(commands)
|
||||
|
@ -226,13 +241,6 @@ class NetworkModule(AnsibleModule):
|
|||
responses.pop(0)
|
||||
return responses
|
||||
|
||||
def configure(self, commands):
|
||||
commands = to_list(commands)
|
||||
if self.params['transport'] == 'cli':
|
||||
return self.configure_cli(commands)
|
||||
else:
|
||||
return self.execute(commands, command_type='cli_conf')
|
||||
|
||||
def execute(self, commands, **kwargs):
|
||||
if not self.connected:
|
||||
self.connect()
|
||||
|
@ -240,6 +248,7 @@ class NetworkModule(AnsibleModule):
|
|||
|
||||
def disconnect(self):
|
||||
self.connection.close()
|
||||
self._connected = False
|
||||
|
||||
def parse_config(self, cfg):
|
||||
return parse(cfg, indent=2)
|
||||
|
@ -262,7 +271,6 @@ def get_module(**kwargs):
|
|||
|
||||
module = NetworkModule(**kwargs)
|
||||
|
||||
# HAS_PARAMIKO is set by module_utils/shell.py
|
||||
if module.params['transport'] == 'cli' and not HAS_PARAMIKO:
|
||||
module.fail_json(msg='paramiko is required but does not appear to be installed')
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue