bash 如何在ansible shell模块中转义特殊字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48787560/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to escape special characters in ansible shell module
提问by Shyam Jos
I tried bash escape and double quotes methods to escape the special characters in below shell command, But both didn't work, What is the proper way to escape special characters in ansible playbook?
我尝试了 bash 转义和双引号方法来转义以下 shell 命令中的特殊字符,但两者都不起作用,在 ansible playbook 中转义特殊字符的正确方法是什么?
The offending line appears to be:
name: Syncing system date with htpdate failed!, Trying wget method...
shell: date -s "$(curl -s --head http://google.com | grep '^Date:' | sed 's/Date: //g' ) +0530"
^ here
exception type: <class 'yaml.scanner.ScannerError'>
exception: mapping values are not allowed in this context
in "<unicode string>", line 15, column 93
回答by tinita
One of the problems here is the colon followed by a space :
. This is usually an indicator for a mapping key.
这里的问题之一是冒号后跟一个空格:
。这通常是映射键的指示符。
YAML does not allow nested mappings on one line, e.g.:
YAML 不允许在一行上嵌套映射,例如:
foo: bar: baz
That's why YAML designers chose to forbid :
in a mapping value if it's on the same line as the key. (It could have been been solved as well by simply ignoring further occurances and treat that as regular content.)
这就是为什么 YAML 设计者选择:
在映射值中禁止,如果它与键在同一行。(它也可以通过简单地忽略进一步的事件并将其视为常规内容来解决。)
You have several choices. You can just put the whole value in quotes, which is not a good idea in this case since you have both single and double quotes which you would have to escape then.
您有多种选择。您可以将整个值放在引号中,在这种情况下这不是一个好主意,因为您有单引号和双引号,然后您必须将其转义。
A workaround can be to escape the space in the sed command:
解决方法可以是转义 sed 命令中的空间:
shell: date -s "$(curl -s --head http://google.com | grep '^Date:' | sed 's/Date:\ //g') +0530"
A more general solution is to use a folded block scalar:
更通用的解决方案是使用折叠块标量:
shell: >
date -s "$(curl -s --head http://google.com | grep '^Date:' | sed 's/Date: //g') +0530"
You could even seperate this into several lines now, because the folded block scalar will fold consecutive lines into one:
你现在甚至可以把它分成几行,因为折叠的块标量会将连续的行折叠成一行:
shell: >
date -s "$(curl -s --head http://google.com
| grep '^Date:' | sed 's/Date: //g') +0530"
The second problem is, as Javier mentioned, the sed expression s/Date/: //g
. You probably want s/Date: //g
. Also look at the suggestion by @tripleee how to improve your command.
第二个问题,正如 Javier 提到的,是 sed 表达式s/Date/: //g
。你可能想要s/Date: //g
. 另请查看@tripleee 如何改进您的命令的建议。
回答by Alvaro Ni?o
Consider that escape caracter is \
not /
考虑到逃逸字符\
不是/
EDIT
编辑
Your problem is not related to escape character, it is caused your sed
expression has an additional /
你的问题与转义字符无关,是因为你的sed
表达式有一个额外的/
sed 's/Date: ///g'
sed 's/Date: ///g'
Should be writen like
sed 's/foo/bar/g'
应该写成
sed 's/foo/bar/g'
In your case sed 's/Date: //g'
在你的情况下 sed 's/Date: //g'