From 03d8fa05b68b2d700adbca2a5e83151e1aef71b5 Mon Sep 17 00:00:00 2001 From: Nathaniel Case Date: Mon, 24 Sep 2018 09:09:17 -0400 Subject: [PATCH] Try to be more helpful when JSON gives up (#45600) * Try to be more helpful when JSON gives up --- lib/ansible/module_utils/connection.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/lib/ansible/module_utils/connection.py b/lib/ansible/module_utils/connection.py index 08017a52a1c..24f27460d0f 100644 --- a/lib/ansible/module_utils/connection.py +++ b/lib/ansible/module_utils/connection.py @@ -142,6 +142,27 @@ class Connection(object): try: data = json.dumps(req) + except TypeError as exc: + data = req.get('params') + + if isinstance(data, dict): + data = data.get('var_options', {}) + + for key, value in iteritems(data): + try: + dummy = json.dumps(value) + except TypeError: + raise ConnectionError( + "Failed to encode some variables as JSON for communication with ansible-connection. " + "Please open an issue and mention that the culprit is most likely '%s'" % key + ) + + raise ConnectionError( + "Failed to encode some variables as JSON for communication with ansible-connection. " + "The original exception was: %s" % to_text(exc) + ) + + try: out = self.send(data) response = json.loads(out)