mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-25 06:10: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
|
@ -16,10 +16,11 @@
|
|||
# You should have received a copy of the GNU General Public License
|
||||
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
||||
#
|
||||
|
||||
from distutils.version import LooseVersion
|
||||
|
||||
from ansible.module_utils.basic import AnsibleModule, env_fallback
|
||||
from ansible.module_utils.shell import Shell, HAS_PARAMIKO
|
||||
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
|
||||
|
||||
try:
|
||||
|
@ -67,6 +68,7 @@ def to_list(val):
|
|||
else:
|
||||
return list()
|
||||
|
||||
|
||||
def xml_to_json(val):
|
||||
if isinstance(val, basestring):
|
||||
return jxmlease.parse(val)
|
||||
|
@ -91,13 +93,12 @@ class Cli(object):
|
|||
password = self.module.params['password']
|
||||
key_filename = self.module.params['ssh_keyfile']
|
||||
|
||||
self.shell = Shell()
|
||||
|
||||
try:
|
||||
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))
|
||||
self.shell = Shell()
|
||||
self.shell.open(host, port=port, username=username, password=password, key_filename=key_filename)
|
||||
except ShellError:
|
||||
e = get_exception()
|
||||
msg = 'failed to connect to %s:%s - %s' % (host, port, str(e))
|
||||
self.module.fail_json(msg=msg)
|
||||
|
||||
if self.shell._matched_prompt.strip().endswith('%'):
|
||||
|
@ -105,7 +106,11 @@ class Cli(object):
|
|||
self.shell.send('set cli screen-length 0')
|
||||
|
||||
def run_commands(self, commands, **kwargs):
|
||||
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)
|
||||
|
||||
def configure(self, commands, **kwargs):
|
||||
commands = to_list(commands)
|
||||
|
@ -281,13 +286,18 @@ 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):
|
||||
cls = globals().get(str(self.params['transport']).capitalize())
|
||||
self.connection = cls(self)
|
||||
try:
|
||||
self.connection = cls(self)
|
||||
except TypeError:
|
||||
e = get_exception()
|
||||
self.fail_json(msg=e.message)
|
||||
|
||||
self.connection.connect()
|
||||
|
||||
msg = 'connecting to host: {username}@{host}:{port}'.format(**self.params)
|
||||
|
@ -336,7 +346,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')
|
||||
elif module.params['transport'] == 'netconf' and not HAS_PYEZ:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue