# How to escape single quote in regex\_replace expression?

**URL:** <https://forum.ansible.com/t/how-to-escape-single-quote-in-regex-replace-expression/28394>\
**Category:** Ansible Project\
**Created:** [August 22, 2018, 12:00pm UTC](https://forum.ansible.com/t/how-to-escape-single-quote-in-regex-replace-expression/28394 "2018-08-22T12:00:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Veena](https://avatars.discourse-cdn.com/v4/letter/v/51bf81/32.png) [@Veena](https://forum.ansible.com/u/Veena)\
**Post date:** [August 22, 2018, 12:00pm UTC](https://forum.ansible.com/t/how-to-escape-single-quote-in-regex-replace-expression/28394/1 "2018-08-22T12:00:45Z")

</div>

Hi,

I’m using Ansible 2.5 and trying to escape single quote in a string. This string will be passed as argument to shell command.

regex\_replace(“'”, “'”) is giving error.

- set\_fact:

default\_passphrase: “{{ Secrete123’!#) | regex\_replace('”‘"’, ‘“'”’) }}"

Please suggest how to achieve this.

Thanks,  
-Veena

---

<div class="post-metadata">

**Author:** ![jean-yves](https://avatars.discourse-cdn.com/v4/letter/j/a587f6/32.png) [@jean-yves](https://forum.ansible.com/u/jean-yves)\
**Post date:** [August 22, 2018, 12:11pm UTC](https://forum.ansible.com/t/how-to-escape-single-quote-in-regex-replace-expression/28394/2 "2018-08-22T12:11:47Z")

</div>

Hi,

Did you try with the quote filter ?

Regards,

JYL

---

<div class="post-metadata">

**Author:** ![Veena](https://avatars.discourse-cdn.com/v4/letter/v/51bf81/32.png) [@Veena](https://forum.ansible.com/u/Veena)\
**Post date:** [August 22, 2018, 12:31pm UTC](https://forum.ansible.com/t/how-to-escape-single-quote-in-regex-replace-expression/28394/3 "2018-08-22T12:31:55Z")

</div>

quote filter also didn’t work. Got the below error

template error while templating string: unexpected char u"'"

---

<div class="post-metadata">

**Author:** ![jean-yves](https://avatars.discourse-cdn.com/v4/letter/j/a587f6/32.png) [@jean-yves](https://forum.ansible.com/u/jean-yves)\
**Post date:** [August 22, 2018, 2:05pm UTC](https://forum.ansible.com/t/how-to-escape-single-quote-in-regex-replace-expression/28394/4 "2018-08-22T14:05:13Z")

</div>

What do you want to do ?  
Perhaps set a specific default password

Did you try this ?

&nbsp;&nbsp;&nbsp;&nbsp;- set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;default\_passphrase: "Secrete123\\'!#)"

---

<div class="post-metadata">

**Author:** ![Veena](https://avatars.discourse-cdn.com/v4/letter/v/51bf81/32.png) [@Veena](https://forum.ansible.com/u/Veena)\
**Post date:** [August 22, 2018, 2:11pm UTC](https://forum.ansible.com/t/how-to-escape-single-quote-in-regex-replace-expression/28394/5 "2018-08-22T14:11:48Z")

</div>

I’m just trying with a dummy password. In the actual use case, the value will be read from config file which will be generated after user sets password. So I need to first get the password value in the 1st task and then set\_fact, further use it in the shell command. I don’t want to block the customer from using any special character.
