AWS Cloudformation使用Fn :: Join在文件中输出双引号 [英] AWS Cloudformation output double quotes in a file using Fn::Join
问题描述
经过大量的研究和挫折,我并没有获得自己想要的输出。
After much research and frustration, I'm not quite getting the output I'm hoping for.
所需的文件输出例如是
"accessKeyId":"UIIUHO]SOMEKEY[SHPIUIUHIU"
但是我得到的是
accessKeyId:UIIUHO]SOMEKEY[SHPIUIUHIU
下面是AWS Cloudformation模板中的行
Below is the line in an AWS Cloudformation template
{"Fn::Join": ["", ["echo \" accessKeyId:", {"Ref": "AccessKeyId"}, "\" >> /home/ubuntu/myfile.json"] ] },
我尝试在echo语句中添加\,但没有引号输出。有人可以在上面显示如何产生所需的输出吗? ?
I've tried adding \" with in the echo statement but no quotes are output. Can someone show how to produce the desired output above?
推荐答案
实际上是正确转义引号的问题。
It's a problem of correctly escaping the quotes in fact.
原因是:CloudFormation字符串中的 \ 被转义为
(双引号)。
Reason is : \"
inside a CloudFormation string is escaped as "
(double-quote).
例如, hello \ me\
给您:
hello "me"
在您的行中,您真正要喂入bash的内容是:
In your line, what you really feed to bash is :
echo " accessKeyId:XXXXX" >> /home/ubuntu/myfile.json
考虑bash使用引号,您会得到字符串
Considering bash use of quotes, you get the string
accessKeyId:XXXXX
在您的 /home/ubuntu/myfile.json
内部要解决您的问题,建议使用:
To solve your problem, I would recommend using:
{"Fn::Join": ["", ["echo '\"accessKeyId\":\"", {"Ref": "AccessKeyId"}, "\"' >> /home/ubuntu/myfile.json"] ] },
被转义为
echo '"accessKeyId":"XXXXX"' >> /home/ubuntu/myfile.json
(很难读:echo所用的整个字符串都在里面
(hard to read : the whole string used by echo is inside single-quotes).
我现在无法尝试,但应该可以解决问题。
I'm not able to try it now, but it should do the trick.
这篇关于AWS Cloudformation使用Fn :: Join在文件中输出双引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!