diff --git a/lib/ansible/inventory/script.py b/lib/ansible/inventory/script.py index 723089db88..a69135aecb 100644 --- a/lib/ansible/inventory/script.py +++ b/lib/ansible/inventory/script.py @@ -26,6 +26,7 @@ from ansible import utils from ansible import errors import sys + class InventoryScript(object): ''' Host inventory parser for ansible using external inventory scripts. ''' @@ -53,6 +54,7 @@ class InventoryScript(object): # not passing from_remote because data from CMDB is trusted self.raw = utils.parse_json(self.data) + self.raw = utils.json_dict_unicode_to_bytes(self.raw) all = Group('all') groups = dict(all=all) @@ -141,7 +143,7 @@ class InventoryScript(object): if out.strip() == '': return dict() try: - return utils.parse_json(out) + return utils.json_dict_unicode_to_bytes(utils.parse_json(out)) except ValueError: raise errors.AnsibleError("could not parse post variable response: %s, %s" % (cmd, out)) diff --git a/lib/ansible/utils/__init__.py b/lib/ansible/utils/__init__.py index 7a7c94e9f4..195046caf0 100644 --- a/lib/ansible/utils/__init__.py +++ b/lib/ansible/utils/__init__.py @@ -1215,6 +1215,25 @@ def to_unicode(value): return value return value.decode("utf-8") +def json_dict_unicode_to_bytes(d): + ''' Recursively convert dict keys and values to byte str + + Specialized for json return because this only handles, lists, tuples, + and dict container types (the containers that the json module returns) + ''' + + if isinstance(d, unicode): + return d.encode('utf-8') + elif isinstance(d, dict): + return dict(map(json_dict_unicode_to_bytes, d.iteritems())) + elif isinstance(d, list): + return list(map(json_dict_unicode_to_bytes, d)) + elif isinstance(d, tuple): + return tuple(map(json_dict_unicode_to_bytes, d)) + else: + return d + + def get_diff(diff): # called by --diff usage in playbook and runner via callbacks # include names in diffs 'before' and 'after' and do diff -U 10