base64 filter: Added ability to specify encoding (#39714)

* base64 filter: Added ability to specify encoding

* Added unicode chars for further testing

* Removed errors to keep previous behaviour in place

* Removed surrogate pairs due to issues loading YAML in CI
pull/39762/head
Jordan Borean 6 years ago committed by GitHub
parent b5cffe8ced
commit fc210a4584
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -0,0 +1,3 @@
minor_changes:
- Added an ``encoding`` option to the ``b64encode`` and ``b64decode`` filters
to specify the encoding of the string that is base64 encoded.

@ -935,6 +935,13 @@ To work with Base64 encoded strings::
{{ encoded | b64decode }} {{ encoded | b64decode }}
{{ decoded | b64encode }} {{ decoded | b64encode }}
As of version 2.6, you can define the type of encoding to use, the default is ``utf-8``::
{{ encoded | b64decode(encoding='utf-16-le') }}
{{ decoded | b64encode(encoding='utf-16-le') }}
.. versionadded:: 2.6
To create a UUID from a string (new in version 1.9):: To create a UUID from a string (new in version 1.9)::
{{ hostname | to_uuid }} {{ hostname | to_uuid }}

@ -459,12 +459,12 @@ def do_groupby(environment, value, attribute):
return [tuple(t) for t in _do_groupby(environment, value, attribute)] return [tuple(t) for t in _do_groupby(environment, value, attribute)]
def b64encode(string): def b64encode(string, encoding='utf-8'):
return to_text(base64.b64encode(to_bytes(string, errors='surrogate_or_strict'))) return to_text(base64.b64encode(to_bytes(string, encoding=encoding, errors='surrogate_or_strict')))
def b64decode(string): def b64decode(string, encoding='utf-8'):
return to_text(base64.b64decode(to_bytes(string, errors='surrogate_or_strict'))) return to_text(base64.b64decode(to_bytes(string, errors='surrogate_or_strict')), encoding=encoding)
def flatten(mylist, levels=None): def flatten(mylist, levels=None):

@ -185,3 +185,11 @@
- flat_two == [1, 2, 3, 4, [5], 6, 7] - flat_two == [1, 2, 3, 4, [5], 6, 7]
vars: vars:
orig_list: [1, 2, [3, [4, [5]], 6], 7] orig_list: [1, 2, [3, [4, [5]], 6], 7]
- name: Test base64 filter
assert:
that:
- "'Ansible - くらとみ\n' | b64encode == 'QW5zaWJsZSAtIOOBj+OCieOBqOOBvwo='"
- "'QW5zaWJsZSAtIOOBj+OCieOBqOOBvwo=' | b64decode == 'Ansible - くらとみ\n'"
- "'Ansible - くらとみ\n' | b64encode(encoding='utf-16-le') == 'QQBuAHMAaQBiAGwAZQAgAC0AIABPMIkwaDB/MAoA'"
- "'QQBuAHMAaQBiAGwAZQAgAC0AIABPMIkwaDB/MAoA' | b64decode(encoding='utf-16-le') == 'Ansible - くらとみ\n'"
Loading…
Cancel
Save