Commit 915d232d by Toshio Kuratomi

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
parent 2f1fc3e0
...@@ -456,6 +456,8 @@ class PlaybookRunnerCallbacks(DefaultRunnerCallbacks): ...@@ -456,6 +456,8 @@ class PlaybookRunnerCallbacks(DefaultRunnerCallbacks):
item = None item = None
if type(results) == dict: if type(results) == dict:
item = results.get('item', None) item = results.get('item', None)
host = utils.to_bytes(host)
results = utils.to_bytes(results)
if item: if item:
msg = "fatal: [%s] => (item=%s) => %s" % (host, item, results) msg = "fatal: [%s] => (item=%s) => %s" % (host, item, results)
else: else:
......
...@@ -22,7 +22,7 @@ import subprocess ...@@ -22,7 +22,7 @@ import subprocess
import ansible.constants as C import ansible.constants as C
from ansible.inventory.host import Host from ansible.inventory.host import Host
from ansible.inventory.group import Group 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 utils
from ansible import errors from ansible import errors
import sys import sys
...@@ -59,7 +59,7 @@ class InventoryScript(object): ...@@ -59,7 +59,7 @@ class InventoryScript(object):
# not passing from_remote because data from CMDB is trusted # not passing from_remote because data from CMDB is trusted
self.raw = utils.parse_json(self.data) 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') all = Group('all')
groups = dict(all=all) groups = dict(all=all)
......
...@@ -251,6 +251,24 @@ def json_dict_unicode_to_bytes(d): ...@@ -251,6 +251,24 @@ def json_dict_unicode_to_bytes(d):
else: else:
return d 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): class AnsibleModule(object):
......
...@@ -22,7 +22,7 @@ import subprocess ...@@ -22,7 +22,7 @@ import subprocess
import ansible.constants as C import ansible.constants as C
from ansible.inventory.host import Host from ansible.inventory.host import Host
from ansible.inventory.group import Group 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 utils
from ansible import errors from ansible import errors
import sys import sys
...@@ -59,7 +59,7 @@ class InventoryScript(object): ...@@ -59,7 +59,7 @@ class InventoryScript(object):
# not passing from_remote because data from CMDB is trusted # not passing from_remote because data from CMDB is trusted
self.raw = utils.parse_json(self.data) 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') all = Group('all')
groups = dict(all=all) groups = dict(all=all)
......
...@@ -251,6 +251,24 @@ def json_dict_unicode_to_bytes(d): ...@@ -251,6 +251,24 @@ def json_dict_unicode_to_bytes(d):
else: else:
return d 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): 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