带打包程序的AWS user_data [英] AWS user_data with Packer
问题描述
因此,我尝试使用Packer创建一个AWS映像并通过user_data_file指定一些用户数据。该文件的内容需要在实例启动时运行,因为它每次都是唯一的。我无法将其烘焙到AMI中。
So I'm trying to use Packer to create an AWS image and specify some user data via user_data_file. The contents of this file needs to be run when the instance boots as it will be unique each time. I can't bake this into the AMI.
使用包装机我有以下内容:
Using packer I have the following:
{
"variables": {
"ami_name": ""
},
"builders": [
{
"type": "amazon-ebs",
"region": "us-east-1",
"source_ami": "ami-c8580bdf",
"instance_type": "t2.micro",
"ssh_username": "ubuntu",
"ami_name": "{{ user `ami_name` }}-{{ isotime | clean_ami_name }}",
"user_data_file": "user_data.sh",
"tags": {
"os_version": "ubuntu",
"built_by": "packer",
"build_on": "{{ isotime | clean_ami_name }}",
"Name": "{{ user `ami_name` }}"
}
}],
"provisioners": [
{
"type": "ansible",
"playbook_file": "playbook.yml",
"user": "ubuntu"
}]
}
我的user_data shell脚本的内容只是通过运行于ansible脚本安装的软件包的一些基本配置行在供应者步骤。观看Packer的输出,我可以确认ansible脚本全部运行。
The contents of my user_data shell script are just a few basic config lines for a package that was installed via the ansible scripts that were run in the provisioners step. Watching the output of Packer I can confirm that the ansible scripts all run.
打包程序完成并创建了AMI,但是从未执行用户数据段。结果图像中不存在任何记录。没有/userdata.log文件,并且 /var/lib/cloud/instance/user-data.txt
为空,我觉得我缺少一些基本的东西,因为这应该是与Packer一起做非常简单的事情。
Packer completes and creates the AMI, but the user data piece is never executed. No record of it exists in resulting image. There is no /userdata.log file and /var/lib/cloud/instance/user-data.txt
is empty I feel like I missing something basic as this should be a very simple thing to do with Packer.
推荐答案
正如Rickard von Essen指出的,答案是将我的脚本复制到 / var / lib / cloud / scripts / per-instance
,它将在从此AMI启动的每个实例上执行我的脚本。
As pointed out by Rickard von Essen the answer was to copy my script to /var/lib/cloud/scripts/per-instance
which would execute my script on every instance launched from this AMI.
如果需要,您也可以将脚本放入 / var / lib / cloud / scripts / per-boot
每次实例启动时都会发生这种情况。
Alternately you can put your script in /var/lib/cloud/scripts/per-boot
if you needed this to happen each time the instance boots.
在我的情况下,由于我想向第三方服务注册实例,因此每次创建实例时,它只能执行一次。
In my case since I wanted to register the instance with a 3rd party service I only had it execute once per instance creation.
这篇关于带打包程序的AWS user_data的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!