Commit ac0de5a9 by Toshio Kuratomi Committed by James Cammarata

jinja2 cannot handle byte strs with non-ascii. So we need to transform…

jinja2 cannot handle byte strs with non-ascii.  So we need to transform potential byte str into unicode type.  This fix is for dynamic inventory.

Fixes #10007

Conflicts:
	v2/ansible/inventory/script.py
	v2/ansible/module_utils/basic.py
parent 8b8700d3
......@@ -456,6 +456,8 @@ class PlaybookRunnerCallbacks(DefaultRunnerCallbacks):
item = None
if type(results) == dict:
item = results.get('item', None)
host = utils.to_bytes(host)
results = utils.to_bytes(results)
if item:
msg = "fatal: [%s] => (item=%s) => %s" % (host, item, results)
else:
......
......@@ -22,7 +22,7 @@ import subprocess
import ansible.constants as C
from ansible.inventory.host import Host
from ansible.inventory.group import Group
from ansible.module_utils.basic import json_dict_unicode_to_bytes
from ansible.module_utils.basic import json_dict_bytes_to_unicode
from ansible import utils
from ansible import errors
import sys
......@@ -55,7 +55,7 @@ class InventoryScript(object):
# not passing from_remote because data from CMDB is trusted
self.raw = utils.parse_json(self.data)
self.raw = json_dict_unicode_to_bytes(self.raw)
self.raw = json_dict_bytes_to_unicode(self.raw)
all = Group('all')
groups = dict(all=all)
......
......@@ -251,6 +251,24 @@ def json_dict_unicode_to_bytes(d):
else:
return d
def json_dict_bytes_to_unicode(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, str):
return unicode(d, 'utf-8')
elif isinstance(d, dict):
return dict(map(json_dict_bytes_to_unicode, d.iteritems()))
elif isinstance(d, list):
return list(map(json_dict_bytes_to_unicode, d))
elif isinstance(d, tuple):
return tuple(map(json_dict_bytes_to_unicode, d))
else:
return d
class AnsibleModule(object):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment