# Escape special characters in a variable

**URL:** <https://forum.ansible.com/t/escape-special-characters-in-a-variable/21019>\
**Category:** Ansible Project\
**Created:** [March 30, 2016, 12:48pm UTC](https://forum.ansible.com/t/escape-special-characters-in-a-variable/21019 "2016-03-30T12:48:28Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Baptiste\_BOUCHEREAU](https://avatars.discourse-cdn.com/v4/letter/b/74df32/32.png) [@Baptiste\_BOUCHEREAU](https://forum.ansible.com/u/Baptiste_BOUCHEREAU)\
**Post date:** [March 30, 2016, 12:48pm UTC](https://forum.ansible.com/t/escape-special-characters-in-a-variable/21019/1 "2016-03-30T12:48:28Z")

</div>

Hello,

I am facing an issue trying to clone a git repository. Whenever the user has a password with ‘@’ or ‘:’ the authentication failed because the repo url become invalid.

Here is the configuration.

vars\_prompt:

- name: “repository\_login”  
prompt: “Type in your bitbucket login”
- name: “repository\_password”  
prompt: “Type in your bitbucket password”

vars:  
project\_directory: “/var/www/html/my\_project”  
repo\_ssh\_url: “https://{{ repository\_login }}:{{ repository\_password }}@bitbucket.org/path/project.git”  
repo\_branch: my\_branch

tasks:

- name: Clone or update the project with git

git:  
repo: “{{ repo\_ssh\_url }}”  
dest: “{{ project\_directory }}”  
update: yes  
accept\_hostkey: yes  
force: yes  
version: “{{ repo\_branch }}”

I found a solution here [http://stackoverflow.com/questions/6172719/escape-character-in-git-proxy-password](http://stackoverflow.com/questions/6172719/escape-character-in-git-proxy-password)

But it requires the user to write his password url encoded

IE → myp@ss should be written myp%40ss

I could set up an ssh key for deployment instead of using login and password authentication, but if possible, I would like to let it as it is. Is there a way to format a variable or another tip?

Thanks,

Baptiste

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [March 30, 2016, 1:53pm UTC](https://forum.ansible.com/t/escape-special-characters-in-a-variable/21019/2 "2016-03-30T13:53:06Z")

</div>

You could probably use the `urlencode` filter provided by jinja2 ([http://jinja.pocoo.org/docs/dev/templates/#urlencode](http://jinja.pocoo.org/docs/dev/templates/#urlencode))

repo\_ssh\_url: “https://{{ repository\_login }}:{{ repository\_password|urlencode }}@[bitbucket.org/path/project.git](http://bitbucket.org/path/project.git)”

---

<div class="post-metadata">

**Author:** ![Baptiste\_BOUCHEREAU](https://avatars.discourse-cdn.com/v4/letter/b/74df32/32.png) [@Baptiste\_BOUCHEREAU](https://forum.ansible.com/u/Baptiste_BOUCHEREAU)\
**Post date:** [March 30, 2016, 3:17pm UTC](https://forum.ansible.com/t/escape-special-characters-in-a-variable/21019/3 "2016-03-30T15:17:23Z")

</div>

The urlencode filter works perfectly, it’s simple and elegant.

Thanks a lot!
