From a023cbce14977cc0d65652e8a4d1785bf4682876 Mon Sep 17 00:00:00 2001 From: James Cammarata Date: Tue, 11 Feb 2014 14:19:00 -0600 Subject: [PATCH] When using jsonify in modules, try several unicode encoding for dumps() Fixes #4821 --- lib/ansible/module_utils/basic.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/ansible/module_utils/basic.py b/lib/ansible/module_utils/basic.py index 86d8566ff7..75e50831a7 100644 --- a/lib/ansible/module_utils/basic.py +++ b/lib/ansible/module_utils/basic.py @@ -840,7 +840,12 @@ class AnsibleModule(object): self.fail_json(msg='Boolean %s not in either boolean list' % arg) def jsonify(self, data): - return json.dumps(data) + for encoding in ("utf-8", "latin-1", "unicode_escape"): + try: + return json.dumps(data, encoding=encoding) + except UnicodeDecodeError, e: + continue + self.fail_json(msg='Invalid unicode encoding encountered') def from_json(self, data): return json.loads(data)