From e01dd7facca2afa5fca78817ddad2ba6d8899ff4 Mon Sep 17 00:00:00 2001 From: Peter Sprygada Date: Sun, 13 Mar 2016 16:24:06 -0700 Subject: [PATCH] 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 --- .../modules/network/nxos/nxos_command.py | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/lib/ansible/modules/network/nxos/nxos_command.py b/lib/ansible/modules/network/nxos/nxos_command.py index 21da8d70a3..186b521be0 100644 --- a/lib/ansible/modules/network/nxos/nxos_command.py +++ b/lib/ansible/modules/network/nxos/nxos_command.py @@ -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):