Shell Module and escaping quotes

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.

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": }

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
   command: tail -1 /app/psoft/install_logs/deployment_version_history.log
   register: mylog

- name: Update log
   shell: echo "81cdc80ec7fdb0201e00fe2f8767b10ec136c687 peoplesoft.yml" >> /app/psoft/install_logs/deployment_version_history.log
   when: mylog.stdout != "81cdc80ec7fdb0201e00fe2f8767b10ec136c687 peoplesoft.yml"

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

a little googling led me to this post 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.

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

Awesome! That worked :slight_smile: Thanks so much for your help.