EC2 JSON模板bash脚本不执行这一切这一切、脚本、模板、JSON

2023-09-11 09:42:17 作者:稳拿女人心

我有这部分我的模板:

            "UserData"       : { "Fn::Base64" : { "Fn::Join" : ["", [
              "sudo yum update -y\n",
              "sleep 30\n",
              "sudo mkdir /data \n",
              "cd /data/\n"
                  ]]}}

有服务器。看来要执行的第一行(因为我可以使用wget),但不这样做的第三或第四线。有什么想法吗?谢谢!

for the server. It seems to execute the first line (since I can use wget) but doesn't do the 3rd or 4th lines. Any thoughts? Thanks!

推荐答案

用户数据不作为脚本运行,除非前两个字符是#!

The user-data is not run as a script unless the first two characters are #!

尝试加入以下作为用户数据的第一行,这样CloudInit知道运行它作为一个shell脚本:

Try adding the following as the first line of the user-data so that CloudInit knows to run it as a shell script:

#!/bin/sh

正如波多黎各指出的那样,你可以删除须藤,因为的用户数据脚本为根的实例的第一次启动运行。

As Rico points out, you can drop the sudo because user-data scripts are run as root on the first boot of the instance.

此外,睡30 CD /数据/ 不提供任何好处,如果这是整体用户数据的脚本。

Also, the sleep 30 and cd /data/ are not providing any benefit if this is the entirety of the user-data script.

这将导致code这样的:

This would result in code like:

"UserData"       : { "Fn::Base64" : { "Fn::Join" : ["", [
    "#!/bin/sh\n",
    "yum update -y\n",
    "mkdir /data \n",
]]}}

您可以添加光盘回来,如果有要添加到用户数据的脚本。进一步的陈述

You could add the cd back in if there are further statements you are adding to the user-data script.