From ddec06ccfee87cf1712c765b93fa197a62269b20 Mon Sep 17 00:00:00 2001 From: Toshio Kuratomi Date: Mon, 20 Jul 2015 12:33:07 -0700 Subject: [PATCH] Detect the old python-json library Fixes #11654 --- lib/ansible/module_utils/basic.py | 11 +++++++++-- lib/ansible/module_utils/facts.py | 9 +++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/lib/ansible/module_utils/basic.py b/lib/ansible/module_utils/basic.py index 14b45a9d7d..d110337226 100644 --- a/lib/ansible/module_utils/basic.py +++ b/lib/ansible/module_utils/basic.py @@ -70,14 +70,21 @@ from itertools import imap, repeat try: import json + # Detect the python-json library which is incompatible + # Look for simplejson if that's the case + try: + if not isinstance(json.loads, types.FunctionType) or not isinstance(json.dumps, types.FunctionType): + raise ImportError + except AttributeError: + raise ImportError except ImportError: try: import simplejson as json except ImportError: - sys.stderr.write('Error: ansible requires a json module, none found!') + print('{"msg": "Error: ansible requires the stdlib json or simplejson module, neither was found!", "failed": true}') sys.exit(1) except SyntaxError: - sys.stderr.write('SyntaxError: probably due to json and python being for different versions') + print('{"msg": "SyntaxError: probably due to installed simplejson being for a different python version", "failed": true}') sys.exit(1) HAVE_SELINUX=False diff --git a/lib/ansible/module_utils/facts.py b/lib/ansible/module_utils/facts.py index 4ff127f87e..de158d4343 100644 --- a/lib/ansible/module_utils/facts.py +++ b/lib/ansible/module_utils/facts.py @@ -16,6 +16,7 @@ # along with Ansible. If not, see . import os +import sys import stat import array import errno @@ -43,9 +44,17 @@ except ImportError: try: import json + # Detect python-json which is incompatible and fallback to simplejson in + # that case + try: + json.loads + json.dumps + except AttributeError: + raise ImportError except ImportError: import simplejson as json + # -------------------------------------------------------------- # timeout function to make sure some fact gathering # steps do not exceed a time limit