Skip to the content.

Notes on cloud init (WIP)

Scripts executed per boot, and not only at provisioning time

Cloud-init is typically designed for initialization tasks during the instance first boot process. It is also a place to add tasks to be executed at the provisioning phase of the VM, and not tasks to be executed every boot (different from systemd)—but note you can configure it to do so.

To run scripts for every boot, you can use the per-boot folder. If one wants to use cloud-init to run a given scriptin every boot, add the script in /var/lib/cloud/scripts/per-boot. Scripts in this folder are executed in alphabetical order. Make sure the script has #!/bin/sh in its first line and proper executable permission such as chmod 744 script.sh.

Re-execute a modified cloud init once VM is provisioned

Some times you may get into a situation that you provisioned a VM with a given cloud-init, but once you are in the VM you realized you made a mistake in the cloud-init script. You want to quickly fix it to conclude the testing but you don’t want to reprovision the VM.

Basically what you can do is to modify the runcmd script:

vi  /var/lib/cloud/instance/scripts/runcmd

and run cloud-init for that particular cloud-init step (user script):

sudo /usr/bin/cloud-init single -n cc_scripts_user

Where

References