用户数据脚本不在我的自定义 AMI 上运行,而是在标准 Amazon linux 中运行 [英] User-data scripts is not running on my custom AMI, but working in standard Amazon linux

查看:34
本文介绍了用户数据脚本不在我的自定义 AMI 上运行,而是在标准 Amazon linux 中运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这几天我搜索了很多关于用户数据脚本不起作用"的话题,但直到现在,我还没有了解我的情况,请帮我弄清楚发生了什么,谢谢很多!

I searched a lot of topic about "user-data script is not working" in these few days, but until now, I haven't gotten any idea about my case yet, please help me to figure out what happened, thanks a lot!

根据 AWS User-data 的解释:

According to AWS User-data explanation:

当您在 Amazon EC2 中启动实例时,您可以选择将用户数据传递给实例,这些数据可用于执行常见的自动化配置任务,甚至在实例启动后运行脚本.

When you launch an instance in Amazon EC2, you have the option of passing user data to the instance that can be used to perform common automated configuration tasks and even run scripts after the instance starts.

所以我尝试在实例启动时传递我自己的用户数据,这是我的用户数据:

So I tried to pass my own user-data when instance launch, this is my user-data:

#!/bin/bash

echo 'test' > /home/ec2-user/user-script-output.txt

但是这个路径下没有文件:/home/ec2-user/user-script-output.txt

But there is no file in this path: /home/ec2-user/user-script-output.txt

我检查了/var/lib/cloud/instance/user-data.txt,该文件存在并且与我的用户数据脚本相同.

I checked /var/lib/cloud/instance/user-data.txt, the file is exist and same as my user-data script.

我也在/var/log/cloud-init.log中查看了日志,没有错误提示.

Also I checked the log in /var/log/cloud-init.log, there is no error message.

但是,如果我使用 Amazon linux (2014.09.01) 启动一个新实例,则用户数据脚本正在运行,但我不确定我的 AMI(基于 Amazon linux)和 Amazon linux 之间有什么区别.

But the user-data script is working if I launch an new instance with Amazon linux(2014.09.01), but I'm not sure what difference between my AMI (based on Amazon linux) and Amazon linux.

我看到的唯一不同部分是我是否运行此脚本:

The only different part I saw is if I run this script:

sudo yum list installed | grep cloud-init

我的 AMI:

cloud-init.noarch 0.7.2-8.33.amzn1 @amzn-main

cloud-init.noarch 0.7.2-8.33.amzn1 @amzn-main

亚马逊Linux:

已安装 cloud-init.noarch 0.7.2-8.33.amzn1

cloud-init.noarch 0.7.2-8.33.amzn1 installed

我不确定这是不是原因?

I'm not sure this is the reason?

如果您需要更多信息,我很乐意提供,请告诉我在我自己的 AMI 中发生了什么以及如何修复它?

If you need more information, I'm glad to provide, please let me know what happened in my own AMI and how to fix it?

非常感谢

更新

刚刚从这个post,

如果我在用户数据文件的顶部添加#cloud-boothook,它就可以工作!

If I add #cloud-boothook in the top of user-data file, it works!

#cloud-boothook
#!/bin/bash
echo 'test' > /home/ec2-user/user-script-output.txt

但还是不知道为什么.

推荐答案

User_data 仅在第一次启动时运行.由于您的图像是自定义图像,我想它已经启动了一次,因此 user_data 已停用.

User_data is run only at the first start up. As your image is a custom one, I suppose it have already been started once and so user_data is desactivated.

对于 Windows,可以通过选中 Ec2 Services 中的框来完成属性.我正在研究如何在自定义图像创建结束时以自动方式执行此操作.

For windows, it can be done by checking a box in Ec2 Services Properties. I'm looking at the moment how to do that in an automated way at the end of the custom image creation.

对于linux,我想机制是一样的,需要在你的自定义镜像上重新激活user_data.

For linux, I suppose the mechanism is the same, and user_data needs to be re-activated on your custom image.

#cloud-boothook 使其工作,因为它将脚本从 user_data 机制更改为 cloud-boothook 在每次启动时运行.

The #cloud-boothook make it works because it changes the script from a user_data mechanism to a cloud-boothook one that runs on each start.

这是使用 powershell 在 Windows 上重新激活启动的代码:

Here is the code to reactivate start on windows using powershell:

$configFile = "C:\Program Files\Amazon\Ec2ConfigService\Settings\Config.xml"
[xml] $xdoc = get-content $configFile
$xdoc.SelectNodes("//Plugin") |?{ $_.Name -eq "Ec2HandleUserData"} |%{ $_.State = "Enabled" }
$xdoc.SelectNodes("//Plugin") |?{ $_.Name -eq "Ec2SetComputerName"} |%{ $_.State = "Enabled" }
$xdoc.OuterXml | Out-File -Encoding UTF8 $configFile

$configFile = "C:\Program Files\Amazon\Ec2ConfigService\Settings\BundleConfig.xml"
[xml] $xdoc = get-content $configFile
$xdoc.SelectNodes("//Property") |?{ $_.Name -eq "AutoSysprep"} |%{ $_.Value = "Yes" }
$xdoc.OuterXml | Out-File -Encoding UTF8 $configFile

(我知道这个问题关注 linux,但它可以帮助其他人......)

(I know the question focus linux, but it could help others ...)

这篇关于用户数据脚本不在我的自定义 AMI 上运行,而是在标准 Amazon linux 中运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆