ansible.builtin.b64encode filter – Encode a string as Base64

Note

This filter plugin is part of ansible-core and included in all Ansible installations. In most cases, you can use the short plugin name b64encode. However, we recommend you use the Fully Qualified Collection Name (FQCN) ansible.builtin.b64encode for easy linking to the plugin documentation and to avoid conflicting with other collections that may have the same filter plugin name.

Synopsis

  • Base64 encoding function.

Input

This describes the input of the filter, the value before | ansible.builtin.b64encode.

Parameter

Comments

Input

string / required

A string to encode.

Keyword parameters

This describes keyword parameters of the filter. These are the values key1=value1, key2=value2 and so on in the following example: input | ansible.builtin.b64encode(key1=value1, key2=value2, ...)

Parameter

Comments

encoding

string

The encoding to use to transform from a text string to a byte string.

Defaults to using ‘utf-8’.

urlsafe

boolean

added in ansible-core 2.19

Encode string using URL- and filesystem-safe alphabet, which substitutes - instead of + and _ instead of / in the Base64 alphabet.

Choices:

  • false ← (default)

  • true

Examples

# Base64 encode a string
b64lola: "{{ 'lola'| b64encode }}"

# Base64 encode the content of 'stuff' variable
b64stuff: "{{ stuff | b64encode }}"

# Base64 encode the content with different encoding
b64stuff: "{{ 'Ansible - くらとみ\n' | b64encode(encoding='utf-16-le') }}"
# => 'QQBuAHMAaQBiAGwAZQAgAC0AIABPMIkwaDB/MAoA'

# URL-safe Base64 encoding
b64stuff: "{{ 'https://www.python.org/example-1' | b64encode(urlsafe=True) }}"
# => 'aHR0cHM6Ly93d3cucHl0aG9uLm9yZy9leGFtcGxlLTE='

Return Value

Key

Description

Return value

string

A Base64 encoded string.

Returned: success

Authors

  • ansible core team

Hint

Configuration entries for each entry type have a low to high priority order. For example, a variable that is lower in the list will override a variable that is higher up.