mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-22 12:50: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,7 +19,7 @@
|
|||
|
||||
import re
|
||||
|
||||
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, Command, HAS_PARAMIKO
|
||||
from ansible.module_utils.netcfg import parse
|
||||
|
||||
|
@ -78,11 +78,11 @@ class Cli(object):
|
|||
timeout = self.module.params['timeout']
|
||||
|
||||
try:
|
||||
self.shell = Shell(kickstart=False, prompts_re=CLI_PROMPTS_RE,
|
||||
errors_re=CLI_ERRORS_RE)
|
||||
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, timeout=timeout)
|
||||
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 authorize(self):
|
||||
|
@ -90,7 +90,11 @@ class Cli(object):
|
|||
self.send(Command('enable', prompt=NET_PASSWD_RE, response=passwd))
|
||||
|
||||
def send(self, commands):
|
||||
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):
|
||||
|
@ -115,21 +119,20 @@ 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):
|
||||
try:
|
||||
self.connection = Cli(self)
|
||||
self.connection.connect()
|
||||
self.connection.send('terminal length 0')
|
||||
if self.params['authorize']:
|
||||
self.connection.authorize()
|
||||
self._connected = True
|
||||
self.connection = Cli(self)
|
||||
|
||||
except Exception, exc:
|
||||
self.fail_json(msg=exc.message)
|
||||
self.connection.connect()
|
||||
self.connection.send('terminal length 0')
|
||||
|
||||
if self.params['authorize']:
|
||||
self.connection.authorize()
|
||||
|
||||
self._connected = True
|
||||
|
||||
def configure(self, commands):
|
||||
commands = to_list(commands)
|
||||
|
@ -139,14 +142,9 @@ class NetworkModule(AnsibleModule):
|
|||
return responses
|
||||
|
||||
def execute(self, commands, **kwargs):
|
||||
try:
|
||||
if not self.connected:
|
||||
self.connect()
|
||||
return self.connection.send(commands, **kwargs)
|
||||
except ShellError, exc:
|
||||
self.fail_json(msg=exc.message, command=exc.command)
|
||||
except Exception, exc:
|
||||
self.fail_json(msg=exc.message, commands=commands)
|
||||
if not self.connected:
|
||||
self.connect()
|
||||
return self.connection.send(commands, **kwargs)
|
||||
|
||||
def disconnect(self):
|
||||
self.connection.close()
|
||||
|
@ -172,7 +170,6 @@ def get_module(**kwargs):
|
|||
|
||||
module = NetworkModule(**kwargs)
|
||||
|
||||
# HAS_PARAMIKO is set by module_utils/shell.py
|
||||
if 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