How to ensure a variable containing special character won't cause further problems?

Viewed 45

I can't figure out the proper way to get Ansible vars_prompt to add double quotes around the password to ensure numbers and special characters are escaped. Below is an example of what I am talking about.

I have tried a number of combinations with no success. Basically when a password is entered I want Ansible to add double quotes around the password that helps ensure if the user has special characters it won't cause problems with Ansible or the ldap/database it connects to.

vars_prompt:
  - name: my_password
    prompt: Enter password
  --password="{{ my_password }}"
1 Answers

The quote filter seems to be what you are looking for, as you seems to want to pass the provided password to a command flag.


Given the playbook, where I purposely added private: no to the variable prompt in order to see what the entered password is, but feel free to remove it:

- hosts: localhost
  gather_facts: no

  vars_prompt:
  - name: my_password
    prompt: Enter password
    private: no

  tasks:
    - debug:
        msg: --password={{ my_password | quote }}

This is an example run with the password abc "' 123:

Enter password: abc "' 123

PLAY [localhost] *************************************************************

TASK [debug] *****************************************************************
ok: [localhost] => 
  msg: --password='abc "'"'"' 123'
Related