简体   繁体   English

如何手动运行 cloud-init?

[英]How to run cloud-init manually?

I'm writing a CloudFormation template and I'm trying to debug the user-data script I provide in the template.我正在编写一个 CloudFormation 模板,并且正在尝试调试我在模板中提供的用户数据脚本。 How can I run the cloud-init manually and make it perform the same actions it does when starting a new instance?如何手动运行cloud-init并使其执行与启动新实例时相同的操作?

You can just run it like this:你可以像这样运行它:

/usr/bin/cloud-init -d init

This runs the cloud init setup with the initial modules.这将使用初始模块运行云初始化设置。 (The -d option is for debug) If want to run all the modules you have to run: (-d 选项用于调试)如果要运行所有模块,您必须运行:

/usr/bin/cloud-init -d modules

Keep in mind that the second time you run these it doesn't do much since it has already run at boot time.请记住,第二次运行它们时它不会做太多事情,因为它已经在启动时运行了。 To force to run after boot time you can run from the command line:要强制在启动后运行,您可以从命令行运行:

( cd /var/lib/cloud/ && sudo rm -rf * )

In older versions the equivalent of cloud-init init is:在旧版本中, cloud-init init的等效项是:

/usr/bin/cloud-init start

You may also find this question useful although it applies to the older versions of cloud-init: How do I make cloud-init startup scripts run every time my EC2 instance boots?您可能还会发现这个问题很有用,尽管它适用于旧版本的 cloud-init: 如何在每次 EC2 实例启动时运行 cloud-init 启动脚本?

The documentation for cloud init here just gives you examples.此处的cloud init 文档仅提供示例。 But it doesn't explain the command line options or each one of the modules, so you have to play around with different values in the config to get your desired results.但它没有解释命令行选项或每个模块,因此您必须在配置中使用不同的值才能获得所需的结果。 Of course you can also look at the code.当然你也可以看一下代码。

rm -f /var/log/cloud-init.log \
&& rm -Rf /var/lib/cloud/* \
&& cloud-init -d init \
&& cloud-init -d modules --mode final

Kudus to @Rico, and also, if you want to run a single module - either for testing or because your distro doesn't enable a module by default (hi Precise!), you can感谢@Rico,而且,如果您想运行单个模块 - 无论是用于测试还是因为您的发行版默认未启用模块(嗨,精确!),您都可以

/usr/bin/cloud-init -d single -n <module-name>

For example when my distro doesn't run write_files by default (like a lot of old distros), I use this at the top of runcmd:例如,当我的发行版默认不运行write_files (像很多旧发行版一样),我在 runcmd 的顶部使用它:

runcmd:
 - /usr/bin/cloud-init -d single -n write-files

[I know its not really an answer to the OP, but when looking to solve my problem this question was one of the top results, so I figure other people might find this useful] [我知道这并不是对 OP 的真正答案,但是在寻求解决我的问题时,这个问题是最好的结果之一,所以我认为其他人可能会觉得这很有用]

On most Linux distros (including CentOS and Ubuntu), you can restart the cloud-init service using systemctl:在大多数 Linux 发行版(包括 CentOS 和 Ubuntu)上,您可以使用 systemctl 重新启动 cloud-init 服务:

systemctl restart cloud-init

And then check the output of the journal to see the results:然后查看日志的输出,看看结果:

journalctl -f -u cloud-init

As documentation , you can simply run 作为文档,您可以简单地运行

sudo cloud-init clean

and add --logs to clean all log file.并添加--logs以清除所有日志文件。 It will redo everything when you reboot重新启动时它会重做一切

On Amazon Linux 2, we figured out that cloud-init is run after initial launch and then removed.在 Amazon Linux 2 上,我们发现cloud-init在初始启动后运行,然后被删除。 This caused a problem when we built custom AMIs with Packer and then wanted to launch them with user-data scripts.当我们使用 Packer 构建自定义 AMI 然后想要使用用户数据脚本启动它们时,这会导致问题。 Here is the Packer shell provisioner (HCL2 format) we use at the end of a build to reset cloud-init :这是我们在构建结束时用于重置cloud-init的 Packer shell provisioner(HCL2 格式):

  provisioner "shell" {
    inline = [
      "echo 'Waiting for cloud-init'; while [ ! -f /var/lib/cloud/instance/boot-finished ]; do sleep 1; done; echo 'Done'",
      "sudo yum install cloud-init -y",
      "sudo cloud-init clean",
    ]
  }

AMIs built with templates that have this will launch with cloud-init support.使用具有此功能的模板构建的 AMI 将在cloud-init支持下启动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM