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:
Peter Sprygada 2016-03-13 16:24:06 -07:00
parent f46ebf2c30
commit eef9c54874

View file

@ -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):