Paul V
Paul V

Reputation: 133

ansible playbook shell module escaping special characters

Using in an ansible playbook for ansible 2.4.2.0

- name: Check the Tomcat version
  shell: "unzip -c {{ mount_path_instance }}/tomcat9/lib/catalina.jar META-INF/MANIFEST.MF |awk '/Implementation-Version: / {sub(/[^ ]+ /, \"\"); print \$0}'"
  register: tomcat_version
  when: instance_check_v9.stat.exists

I get the error

TASK [redhat-tomcat-update : include_tasks] ************************************
fatal: [localhost]: FAILED! => {"reason": "Syntax Error while loading YAML.\n\n\nThe error appears to have been in '/home/ansible/work/play_redhat_tomcat_update/roles/redhat-tomcat-update/tasks/variables.yml': line 39, column 153, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n- name: Check the Tomcat version\n  shell: \"unzip -c {{ mount_path_instance }}/tomcat9/lib/catalina.jar META-INF/MANIFEST.MF |awk '/Implementation-Version: / {sub(/[^ ]+ /, \\\"\\\"); print \\$0}'\"\n                                                                                                                                                        ^ here\nWe could be wrong, but this one looks like it might be an issue with\nmissing quotes.  Always quote template expression brackets when they\nstart a value. For instance:\n\n    with_items:\n      - {{ foo }}\n\nShould be written as:\n\n    with_items:\n      - \"{{ foo }}\"\n\nexception type: <class 'yaml.scanner.ScannerError'>\nexception: while parsing a quoted scalar\n  in \"<unicode string>\", line 39, column 10\nfound unknown escape character\n  in \"<unicode string>\", line 39, column 153"}

running the next command works fine

ansible localhost -m shell -a "unzip -c {{ mount_path_instance }}/tomcat9/lib/catalina.jar META-INF/MANIFEST.MF |awk '/Implementation-Version: / {sub(/[^ ]+ /, \"\"); print \$0}'" --extra-vars='{"mount_path_instance": "/appl/tomcat/paul_1_uat", "instance_name": "paul_1_uat" }'

[WARNING]: Consider using unarchive module rather than running unzip

localhost | SUCCESS | rc=0 >> 9.0.7.redhat-12

does some one see what is wrong in the yml definition?

Upvotes: 2

Views: 11016

Answers (1)

mdaniel
mdaniel

Reputation: 33231

YAML expands backslashes in double-quoted strings, and \$ is not a legal escape in YAML (the \" is fine)

What you'll want is to use the alternate syntax that side-steps the double-quoting issue:

shell: |
   unzip -c {{ mount_path_instance }}/tomcat9/lib/catalina.jar META-INF/MANIFEST.MF | awk '/Implementation-Version: / {sub(/[^ ]+ /, ""); print $0}'

Ironically, you don't actually have to escape that dollarsign because the awk command is already in single-quotes, so you could also just drop the \$ from your existing command, too, but I am pretty sure you'll get less backslash-itis using the shell: | syntax


With regard to:

ansible localhost -m shell -a "unzip -c {{ mount_path_instance }}/tomcat9/lib/catalina.jar META-INF/MANIFEST.MF |awk '/Implementation-Version: / {sub(/[^ ]+ /, \"\"); print \$0}'"

running fine, that's because your shell actually collapsed the \$ for you, which one can see with a simple echo:

echo "thingy \"\" \$0"

Upvotes: 2

Related Questions