Ansible - Escape special characters using urlencode

by
Jeremy Canfield |
Updated: February 28 2023
| Ansible articles
There are two similiar but distinct filters that can be used to escape literal or special characters.
- regex_escape
- urlencode (this article)
urlencode can be used to encode special characters in a string. In this example, the whitespace in "fo o" is encoded as %20.
---
- hosts: localhost
tasks:
- name: encode special characters
debug:
msg: "{{ 'fo o' | urlencode }}"
...
The following should be returned.
ok: [localhost] => {
"msg": "fo%20o"
}
In this example, the long dash (also known as em dash) in ba–r will be encoded.
---
- hosts: localhost
tasks:
- name: encode special characters
debug:
msg: "{{ 'ba–r' | urlencode }}"
...
The following should be returned.
ok: [localhost] => {
"msg": "ba%E2%80%93r"
}
Did you find this article helpful?
If so, consider buying me a coffee over at