用户数据(cloud-init)脚本未在EC2上执行

lol*_*ter 10 linux amazon-ec2 cloud-init

我的用户数据脚本

#!
set -e -x
echo `whoami`
su root
yum update -y
touch ~/PLEASE_WORK.txt
Run Code Online (Sandbox Code Playgroud)

从命令输入:

ec2-run-instances ami-05355a6c -n 1 -g mongo-group -k mykey -f myscript.sh -t t1.micro -z us-east-1a
Run Code Online (Sandbox Code Playgroud)

但是当我检查文件时/var/log/cloud-init.log,它tail -n 5是:

[CLOUDINIT] 2013-07-22 16:02:29,566 - cloud-init-cfg[INFO]: cloud-init-cfg ['runcmd']
[CLOUDINIT] 2013-07-22 16:02:29,583 - __init__.py[DEBUG]: restored from cache type DataSourceEc2
[CLOUDINIT] 2013-07-22 16:02:29,686 - cloud-init-cfg[DEBUG]: handling runcmd with freq=None and args=[]
[CLOUDINIT] 2013-07-22 16:02:33,691 - cloud-init-run-module[INFO]: cloud-init-run-module ['once-per-instance', 'user-scripts', 'execute', 'run-parts', '/var/lib/cloud/data/scripts']
[CLOUDINIT] 2013-07-22 16:02:33,699 - __init__.py[DEBUG]: restored from cache type DataSourceEc2
Run Code Online (Sandbox Code Playgroud)

我还验证了curl http://169.254.169.254/latest/user-data按预期返回我的文件.

没有其他错误或我的脚本输出发生.如何在启动时正确执行用户数据脚本?

lza*_*zap 14

Cloud-init不接受普通的bash脚本,就像那样.这是一个吃YAML文件的野兽,它定义了你的实例(包,ssh键和其他东西).

使用MIME,您也可以发送任意shell脚本,但您必须对它们进行MIME编码.

$ cat my-boothook.txt
#!/bin/sh
echo "Hello World!"
echo "This will run as soon as possible in the boot sequence"

$ cat my-user-script.txt
#!/usr/bin/perl
print "This is a user script (rc.local)\n"

$ cat my-include.txt
# these urls will be read pulled in if they were part of user-data
# comments are allowed.  The format is one url per line
http://www.ubuntu.com/robots.txt
http://www.w3schools.com/html/lastpage.htm

$ cat my-upstart-job.txt
description "a test upstart job"
start on stopped rc RUNLEVEL=[2345]
console output
task
script
echo "====BEGIN======="
echo "HELLO From an Upstart Job"
echo "=====END========"
end script

$ cat my-cloudconfig.txt
#cloud-config
ssh_import_id: [smoser]
apt_sources:
 - source: "ppa:smoser/ppa"

$ ls
my-boothook.txt     my-include.txt      my-user-script.txt
my-cloudconfig.txt  my-upstart-job.txt

$ write-mime-multipart --output=combined-userdata.txt \
   my-boothook.txt:text/cloud-boothook \
   my-include.txt:text/x-include-url \
   my-upstart-job.txt:text/upstart-job \
   my-user-script.txt:text/x-shellscript \
   my-cloudconfig.txt

$ ls -l combined-userdata.txt 
-rw-r--r-- 1 smoser smoser 1782 2010-07-01 16:08 combined-userdata.txt
Run Code Online (Sandbox Code Playgroud)

combined-userdata.txt是您要在那里粘贴的文件.

更多信息:

https://help.ubuntu.com/community/CloudInit

另请注意,这在很大程度上取决于您使用的图像.但是你说这是基于cloud-init的图像,所以这适用.还有其他云启动器没有命名为cloud-init - 那么它可能会有所不同.

  • 啊,我明白了 - 一切都必须是MIME编码的.我正在使用通用的amazon linux AMI,但我在启动日志中看到了`cloud-init`进程,所以我认为它与ubuntu风味相同. (2认同)

And*_*rea 13

实际上,cloud-init允许使用单个shell脚本作为输入(尽管您可能希望将MIME存档用于更复杂的设置).

OP脚本的问题是第一行不正确.你应该使用这样的东西:

#!/bin/sh
Run Code Online (Sandbox Code Playgroud)

这样做的原因是,虽然cloud-init用于#!识别用户脚本,但操作系统需要一个完整的shebang行才能执行脚本.

那么在OP的情况下发生的事情是cloud-init行为正确(即它下载并尝试运行脚本)但操作系统无法实际执行它.


请参阅:Wikipedia上的Shebang(Unix)

  • [关于cloud-init的文档在这里有些误导](http://cloudinit.readthedocs.org/en/latest/topics/format.html#user-data-script),其中声明"开始于:`#!使用MIME档案时``或`Content-Type:text/x-shellscript`." 我可以看到有人可能没有意识到#!需要包括shebang之后的路径. (2认同)