[英]ansible playbook shell module escaping special characters
在Ansible Playbook中使用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
我得到錯誤
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"}
運行下一個命令可以正常工作
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" }'
[警告]:考慮使用非歸檔模塊,而不是運行解壓縮
本地主機| 成功| rc = 0 >> 9.0.7.redhat-12
有人看到yml定義有什么問題嗎?
YAML將反斜杠擴展為雙引號字符串,並且\\$
不是YAML中的合法轉義符( \\"
很好)
您想要的是使用替代雙引號問題的替代語法:
shell: |
unzip -c {{ mount_path_instance }}/tomcat9/lib/catalina.jar META-INF/MANIFEST.MF | awk '/Implementation-Version: / {sub(/[^ ]+ /, ""); print $0}'
具有諷刺意味的是,由於awk命令已經在單引號中,因此您實際上不必逃脫$ sign,因此您也可以從現有命令中刪除\\$
,但是我敢肯定,反斜杠會更少-itis使用shell: |
句法
關於至於對於有關:
ansible localhost -m shell -a "unzip -c {{ mount_path_instance }}/tomcat9/lib/catalina.jar META-INF/MANIFEST.MF |awk '/Implementation-Version: / {sub(/[^ ]+ /, \"\"); print \$0}'"
運行良好,這是因為您的shell實際上為您折疊了\\$
,可以通過簡單的回顯看到它:
echo "thingy \"\" \$0"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.