Ansible - Encode a string using the b64encode filter

by
Jeremy Canfield |
Updated: March 02 2022
| Ansible articles
The b64encode filter is used to encode a string. The b64decode filter is used to decode a string. In this example, string foo is encoded.
Notice in this example that foo is wrapped in quotes. When wrapped in quotes, foo is interpreted as a string. If foo were not wrapped in quotes, foo would be interpreted as a variable.
- name: "encoded the 'foo' string"
debug:
msg: "{{ 'foo' | b64encode }}"
The following should be returned.
TASK [encode the 'foo' string]
ok: [server1.example.com] => {
"msg": [
"Zm9v"
]
}
Let's say you are using the vars plugin to create a variable. In this example, the foo variable contains a value of Hello.
vars:
foo: "Hello"
Here is how you would encode the foo variable, which will encode value Hello. Notice that foo is not wrapped in quotes, so that foo is interpreted as a variable, not a string.
- name: "encoded the 'foo' variable"
debug:
msg: "{{ foo | b64encode }}"
The following should be returned.
TASK [encode the 'foo' variable]
ok: [server1.example.com] => {
"msg": [
""SGVsbG8="
]
}
Did you find this article helpful?
If so, consider buying me a coffee over at