mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-06-03 14:59:09 -07:00
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
This commit is contained in:
parent
2f1fc3e042
commit
915d232d5f
5 changed files with 42 additions and 4 deletions
|
@ -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
|
||||||
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):
|
||||||
|
|
||||||
# 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):
|
||||||
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
|
||||||
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):
|
||||||
|
|
||||||
# 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):
|
||||||
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):
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue