mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
update to nxos_command with additional exception handling
This update adds exception handling to catch errors when trying to parse command output to json. It also removes the dependency on importing json opting to use the AnsibleModule methods instead
This commit is contained in:
parent
ae9930f59f
commit
e01dd7facc
1 changed files with 7 additions and 10 deletions
|
@ -20,7 +20,7 @@ DOCUMENTATION = """
|
|||
---
|
||||
module: nxos_command
|
||||
version_added: "2.1"
|
||||
author: "Peter sprygada (@privateip)"
|
||||
author: "Peter Sprygada (@privateip)"
|
||||
short_description: Run arbitrary command on Cisco NXOS devices
|
||||
description:
|
||||
- Sends an aribtrary command to an NXOS node and returns the results
|
||||
|
@ -62,7 +62,6 @@ options:
|
|||
trying the command again.
|
||||
required: false
|
||||
default: 1
|
||||
|
||||
"""
|
||||
|
||||
EXAMPLES = """
|
||||
|
@ -118,7 +117,6 @@ failed_conditions:
|
|||
import time
|
||||
import shlex
|
||||
import re
|
||||
import json
|
||||
|
||||
INDEX_RE = re.compile(r'(\[\d+\])')
|
||||
|
||||
|
@ -127,12 +125,6 @@ def to_lines(stdout):
|
|||
if isinstance(item, basestring):
|
||||
item = str(item).split('\n')
|
||||
yield item
|
||||
def get_response(data):
|
||||
try:
|
||||
json_data = json.loads(data)
|
||||
except ValueError:
|
||||
json_data = None
|
||||
return dict(data=data, json=json_data)
|
||||
|
||||
def main():
|
||||
spec = dict(
|
||||
|
@ -173,7 +165,12 @@ def main():
|
|||
|
||||
for index, cmd in enumerate(commands):
|
||||
if cmd.endswith('json'):
|
||||
response[index] = json.loads(response[index])
|
||||
try:
|
||||
response[index] = module.from_json(response[index])
|
||||
except ValueError, exc:
|
||||
module.fail_json(msg='failed to parse json response',
|
||||
exc_type=str(type(exc)), exc_message=str(exc),
|
||||
response=response[index])
|
||||
|
||||
for item in list(queue):
|
||||
if item(response):
|
||||
|
|
Loading…
Reference in a new issue