From 5319437bc2930f81ea9f7999330eb32d984c4a88 Mon Sep 17 00:00:00 2001 From: Abhijeet Kasurde Date: Fri, 4 Dec 2020 11:54:42 +0530 Subject: [PATCH] json_query: Handle AnsibleUnicode, AnsibleUnsafeText (#1434) jmespath library does not undestand custom string types such as AnsibleUnicode, and AnsibleUnsafeText. So user need to use ``to_json | from_json`` filter while using functions like ``starts_with`` and ``contains`` etc. This hack will allow user to get rid of this filter. Fixes: #320 Signed-off-by: Abhijeet Kasurde --- changelogs/fragments/320_unsafe_text.yml | 2 ++ plugins/filter/json_query.py | 3 +++ 2 files changed, 5 insertions(+) create mode 100644 changelogs/fragments/320_unsafe_text.yml diff --git a/changelogs/fragments/320_unsafe_text.yml b/changelogs/fragments/320_unsafe_text.yml new file mode 100644 index 0000000000..aa0621d085 --- /dev/null +++ b/changelogs/fragments/320_unsafe_text.yml @@ -0,0 +1,2 @@ +bugfixes: +- json_query - handle ``AnsibleUnicode`` and ``AnsibleUnsafeText`` (https://github.com/ansible-collections/community.general/issues/320). diff --git a/plugins/filter/json_query.py b/plugins/filter/json_query.py index 123d929bba..972109a045 100644 --- a/plugins/filter/json_query.py +++ b/plugins/filter/json_query.py @@ -35,6 +35,9 @@ def json_query(data, expr): raise AnsibleError('You need to install "jmespath" prior to running ' 'json_query filter') + # Hack to handle Ansible String Types + # See issue: https://github.com/ansible-collections/community.general/issues/320 + jmespath.functions.REVERSE_TYPES_MAP['string'] = jmespath.functions.REVERSE_TYPES_MAP['string'] + ('AnsibleUnicode', 'AnsibleUnsafeText', ) try: return jmespath.search(expr, data) except jmespath.exceptions.JMESPathError as e: