diff --git a/lib/ansible/plugins/filter/core.py b/lib/ansible/plugins/filter/core.py index 4cf7c658ac5..ca3a033cbd1 100644 --- a/lib/ansible/plugins/filter/core.py +++ b/lib/ansible/plugins/filter/core.py @@ -48,7 +48,7 @@ except: from ansible import errors from ansible.compat.six import iteritems, string_types from ansible.compat.six.moves import reduce -from ansible.module_utils._text import to_text +from ansible.module_utils._text import to_bytes, to_text from ansible.parsing.yaml.dumper import AnsibleDumper from ansible.utils.hashing import md5s, checksum_s from ansible.utils.unicode import unicode_wrap @@ -229,7 +229,7 @@ def get_hash(data, hashtype='sha1'): except: return None - h.update(data) + h.update(to_bytes(data, errors='surrogate_or_strict')) return h.hexdigest() def get_encrypted_password(password, hashtype='sha512', salt=None): diff --git a/test/integration/roles/test_filters/tasks/main.yml b/test/integration/roles/test_filters/tasks/main.yml index 1f6f0682e9d..ec81d7f3fce 100644 --- a/test/integration/roles/test_filters/tasks/main.yml +++ b/test/integration/roles/test_filters/tasks/main.yml @@ -113,3 +113,8 @@ that: - "users | json_query('[*].hosts[].host') == ['host_a', 'host_b', 'host_c', 'host_d']" +- name: Test hash filter + assert: + that: + - '"{{ "hash" | hash("sha1") }}" == "2346ad27d7568ba9896f1b7da6b5991251debdf2"' + - '"{{ "café" | hash("sha1") }}" == "f424452a9673918c6f09b0cdd35b20be8e6ae7d7"'