Bootstrap FreeKB - Ansible - Escape special characters using urlencode
Ansible - Escape special characters using urlencode

Updated:   |  Ansible articles

There are two similiar but distinct filters that can be used to escape literal or special characters.

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 Buy Me A Coffee



Comments


Add a Comment


Please enter 592817 in the box below so that we can be sure you are a human.