# Ansible shell command adding backslash

**URL:** <https://forum.ansible.com/t/ansible-shell-command-adding-backslash/32581>\
**Category:** Ansible Project\
**Created:** [April 1, 2020, 5:14pm UTC](https://forum.ansible.com/t/ansible-shell-command-adding-backslash/32581 "2020-04-01T17:14:36Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mark\_T](https://avatars.discourse-cdn.com/v4/letter/m/a87d85/32.png) [@Mark\_T](https://forum.ansible.com/u/Mark_T)\
**Post date:** [April 1, 2020, 5:14pm UTC](https://forum.ansible.com/t/ansible-shell-command-adding-backslash/32581/1 "2020-04-01T17:14:36Z")

</div>

Hello,

- name: Determining Source Account DB Password  
shell: cat wp-config.php | grep DB\_PASSWORD | awk -F"[‘’]" ‘{print $4}’  
register: wp\_dbpw\_source

If the password contains a backslash, the output doubles the number of backslashes.

abc\*# becomes abc\*#

I’m aware extra escape characters are only for display purposes, and not actually sent to the command.

But the distorted password output prevents the script from accessing the database. When I remove the backslash from the password, the script works again.

Is there a way to output a shell command using grep, cat or awk that doesn’t cause this distortion?

Thanks,

Mark T.

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [April 1, 2020, 5:53pm UTC](https://forum.ansible.com/t/ansible-shell-command-adding-backslash/32581/2 "2020-04-01T17:53:32Z")

</div>

As much as I like them I would recommend against grep/cat/awk in this  
case as it makes things very fragile.  
Instead try to use something that PHP and ansible can both use - such as JSON.

For example my WordPress has:

define('DB\_PASSWORD',  
'(U\>Trob!t]J=-APTjNr[b9()yL#-95r8[3s4MpM~S0dI=`Pb7td9$):M{kENu|i323');

This playbook then gets that password:

&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;- name: JSON encode constants  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;command: php -r 'require\_once("wp-config.php");  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo json\_encode(get\_defined\_constants(true)["user"]);'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;register: php\_out

&nbsp;&nbsp;&nbsp;&nbsp;- set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;php\_constants: "{{ php\_out.stdout | from\_json }}"

&nbsp;&nbsp;&nbsp;&nbsp;- debug: var=php\_constants.DB\_PASSWORD

TASK [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [wp] =\>  
&nbsp;&nbsp;php\_constants.DB\_PASSWORD:  
(U\>Trob!t]J=-APTjNr[b9()yL#-95r8[3s4MpM~S0dI=`Pb7td9$):M{kENu|i323

DIck

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [April 1, 2020, 6:16pm UTC](https://forum.ansible.com/t/ansible-shell-command-adding-backslash/32581/3 "2020-04-01T18:16:51Z")

</div>

Obviously this method gets you all the other constants as welll

---

<div class="post-metadata">

**Author:** ![Mark\_T](https://avatars.discourse-cdn.com/v4/letter/m/a87d85/32.png) [@Mark\_T](https://forum.ansible.com/u/Mark_T)\
**Post date:** [May 14, 2020, 7:38pm UTC](https://forum.ansible.com/t/ansible-shell-command-adding-backslash/32581/4 "2020-05-14T19:38:36Z")

</div>

Hello,

Thanks for the feedback. Your code works with all special characters that I’ve used, but I run into a problem when there are two backslashes (\) side-by-side in a password.

Is there any way to fix this?

Regards,

Mark

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [May 14, 2020, 9:24pm UTC](https://forum.ansible.com/t/ansible-shell-command-adding-backslash/32581/5 "2020-05-14T21:24:27Z")

</div>

Do you mean the actual password has two, or the escaped string had two (meaning the password has one)?
