# Shell Module and escaping quotes

**URL:** <https://forum.ansible.com/t/shell-module-and-escaping-quotes/22128>\
**Category:** Ansible Project\
**Created:** [July 26, 2016, 9:09pm UTC](https://forum.ansible.com/t/shell-module-and-escaping-quotes/22128 "2016-07-26T21:09:29Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jason3](https://avatars.discourse-cdn.com/v4/letter/j/f9ae1b/32.png) [@Jason3](https://forum.ansible.com/u/Jason3)\
**Post date:** [July 26, 2016, 9:09pm UTC](https://forum.ansible.com/t/shell-module-and-escaping-quotes/22128/1 "2016-07-26T21:09:29Z")

</div>

I apologize if this isn’t an strictly an ansible problem, but I’m attempting to run a command like the following which works when run directly on the server in bash but fails when I try running via ansible.

- name: write commit to history

shell: “[[$(tail -1 /app/psoft/install\_logs/deployment\_version\_history.log) != ‘81cdc80ec7fdb0201e00fe2f8767b10ec136c687 peoplesoft.yml’]] && { echo ‘81cdc80ec7fdb0201e00fe2f8767b10ec136c687 peoplesoft.yml’ \>\> /app/psoft/install\_logs/deployment\_version\_history.log; }”

output:

fatal: [net12204]: FAILED! =\> {“changed”: true, “cmd”: “[[$(tail -1 /app/psoft/install\_logs/deployment\_version\_history.log) != ‘81cdc80ec7fdb0201e00fe2f8767b10ec136c687 peoplesoft.yml’]] && { echo ‘81cdc80ec7fdb0201e00fe2f8767b10ec136c687 peoplesoft.yml’ \>\> /app/psoft/install\_logs/deployment\_version\_history.log; }”, “delta”: “0:00:00.006239”, “end”: “2016-07-26 16:58:47.826353”, “failed”: true, “rc”: 1, “start”: “2016-07-26 16:58:47.820114”, “stderr”: “”, “stdout”: “”, “stdout\_lines”: , “warnings”: }

I’ve tried the following variations without any luck:

shell: “[[$(tail -1 /app/psoft/install\_logs/deployment\_version\_history.log) != '81cdc80ec7fdb0201e00fe2f8767b10ec136c687 peoplesoft.yml']] && { echo '81cdc80ec7fdb0201e00fe2f8767b10ec136c687 peoplesoft.yml' \>\> /app/psoft/install\_logs/deployment\_version\_history.log }”

shell: “[[$(tail -1 /app/psoft/install\_logs/deployment\_version\_history.log) != ‘‘81cdc80ec7fdb0201e00fe2f8767b10ec136c687 peoplesoft.yml’’]] && { echo ‘‘81cdc80ec7fdb0201e00fe2f8767b10ec136c687 peoplesoft.yml’’ \>\> /app/psoft/install\_logs/deployment\_version\_history.log }”

shell: “[[$(tail -1 /app/psoft/install\_logs/deployment\_version\_history.log) != "81cdc80ec7fdb0201e00fe2f8767b10ec136c687 peoplesoft.yml"]] && { echo "81cdc80ec7fdb0201e00fe2f8767b10ec136c687 peoplesoft.yml" \>\> /app/psoft/install\_logs/deployment\_version\_history.log }”

This is two ’ ’ together instead of a double quote.

shell: “[[$(tail -1 /app/psoft/install\_logs/deployment\_version\_history.log) != ‘‘81cdc80ec7fdb0201e00fe2f8767b10ec136c687 peoplesoft.yml’’]] && { echo ‘‘81cdc80ec7fdb0201e00fe2f8767b10ec136c687 peoplesoft.yml’’ \>\> /app/psoft/install\_logs/deployment\_version\_history.log }”

The full explanation of what i’m trying to do is create a log with the history of ansible plays run and their version in git that have been run against the target application. In the full script the commit hash will be coming from a register variable in a previous step, but i can’t even get a basic case to run with all the competing special characters. The command is supposed to look at a log file and compare the last entry to the current entry to be written and if they’re not the same, to add the entry to the end of the file. I tried doing this more simply with lineinfile but it would only write if the line didn’t exist somewhere else in the file(which is not what i want).

Thanks in advance for any advice/help given.

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [July 27, 2016, 2:26pm UTC](https://forum.ansible.com/t/shell-module-and-escaping-quotes/22128/2 "2016-07-27T14:26:12Z")

</div>

> I apologize if this isn't an strictly an ansible problem, but I'm  
> attempting to run a command like the following which works when run  
> directly on the server in bash but fails when I try running via ansible.
> 
> &nbsp;&nbsp;- name: write commit to history  
> &nbsp;&nbsp;&nbsp;&nbsp;shell: "[[ $(tail -1  
> /app/psoft/install\_logs/deployment\_version\_history.log) !=  
> '81cdc80ec7fdb0201e00fe2f8767b10ec136c687 peoplesoft.yml' ]] && { echo  
> '81cdc80ec7fdb0201e00fe2f8767b10ec136c687 peoplesoft.yml' \>\>  
> /app/psoft/install\_logs/deployment\_version\_history.log; }"
> 
> output:
> 
> fatal: [net12204]: FAILED! =\> {"changed": true, "cmd": "[[ $(tail -1  
> /app/psoft/install\_logs/deployment\_version\_history.log) !=  
> '81cdc80ec7fdb0201e00fe2f8767b10ec136c687 peoplesoft.yml' ]] && { echo  
> '81cdc80ec7fdb0201e00fe2f8767b10ec136c687 peoplesoft.yml' \>\>  
> /app/psoft/install\_logs/deployment\_version\_history.log; }", "delta":  
> "0:00:00.006239", "end": "2016-07-26 16:58:47.826353", "failed": true,  
> "rc": 1, "start": "2016-07-26 16:58:47.820114", "stderr": "", "stdout": "",  
> "stdout\_lines": , "warnings": }

Shell in Ansible is using /bin/sh, depending on your distribution sh usually has limited functionality. So things like [] and $() might not work.

> The full explanation of what i'm trying to do is create a log with the  
> history of ansible plays run and their version in git that have been run  
> against the target application. In the full script the commit hash will be  
> coming from a register variable in a previous step, but i can't even get a  
> basic case to run with all the competing special characters. The command is  
> supposed to look at a log file and compare the last entry to the current  
> entry to be written and if they're not the same, to add the entry to the  
> end of the file. I tried doing this more simply with lineinfile but it  
> would only write if the line didn't exist somewhere else in the file(which  
> is not what i want).
> 
> Thanks in advance for any advice/help given.

There is a few ways to solve it, make script or do something like this:

- name: Get last log  
&nbsp;&nbsp;&nbsp;command: tail -1 /app/psoft/install\_logs/deployment\_version\_history.log  
&nbsp;&nbsp;&nbsp;register: mylog

- name: Update log  
&nbsp;&nbsp;&nbsp;shell: echo "81cdc80ec7fdb0201e00fe2f8767b10ec136c687 peoplesoft.yml" \>\> /app/psoft/install\_logs/deployment\_version\_history.log  
&nbsp;&nbsp;&nbsp;when: mylog.stdout != "81cdc80ec7fdb0201e00fe2f8767b10ec136c687 peoplesoft.yml"

---

<div class="post-metadata">

**Author:** ![Jason3](https://avatars.discourse-cdn.com/v4/letter/j/f9ae1b/32.png) [@Jason3](https://forum.ansible.com/u/Jason3)\
**Post date:** [July 27, 2016, 10:42pm UTC](https://forum.ansible.com/t/shell-module-and-escaping-quotes/22128/3 "2016-07-27T22:42:29Z")

</div>

Thanks for the response/idea Kai. I was too focused on making it work the other way to consider breaking it up like that.

I ran with the idea of grabbing the last log line in a separate task and using a when conditional however now i’m running into a different issue. Since i’m now using variables for the conditional it’s complaining about "template error while templating string: unexpected char u’a’ at 8… Not sure what this error means exactly.

Here’s the full tasks/output:

[http://pastebin.com/TsdB5ZFe](http://pastebin.com/TsdB5ZFe)

a little googling led me to this post [https://groups.google.com/forum/#!msg/ansible-project/mLgdORSFspo/PXfO76QNfMMJ](https://groups.google.com/forum/#!msg/ansible-project/mLgdORSFspo/PXfO76QNfMMJ) but i’m not sure where I would apply the raw syntax in there or if it even applies.

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [July 28, 2016, 6:42am UTC](https://forum.ansible.com/t/shell-module-and-escaping-quotes/22128/4 "2016-07-28T06:42:25Z")

</div>

Don't use curly brackets in when, they are implied, remove them and the error message will most likely be gone.

---

<div class="post-metadata">

**Author:** ![Jason3](https://avatars.discourse-cdn.com/v4/letter/j/f9ae1b/32.png) [@Jason3](https://forum.ansible.com/u/Jason3)\
**Post date:** [July 28, 2016, 12:48pm UTC](https://forum.ansible.com/t/shell-module-and-escaping-quotes/22128/5 "2016-07-28T12:48:08Z")

</div>

Awesome! That worked 🙂 Thanks so much for your help.
