Escape special characters in a variable

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

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

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

repo_ssh_url: “https://{{ repository_login }}:{{ repository_password|urlencode }}@bitbucket.org/path/project.git

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

Thanks a lot!