[enhancement]: Disable cloud-init when packer is used to customize image
Enhancement
Hello, we are working to implement cloud-init as a main provisionning instance tool on GCP.
Our custom company GCP image, could need to be be re-customized using packer as engine. we would like to be able to "disable" cloud-init when packer start à cloud-init based image.
But It look-like the actual mecanism available are is not suffisant to work with cloud and packer : https://cloudinit.readthedocs.io/en/latest/howto/disable_cloud_init.html
This looks like a user support request, not a bug.
But It look-like the actual mecanism available are is not suffisant to work with cloud and packer
Why?
This looks like a user support request, not a bug.
But It look-like the actual mecanism available are is not suffisant to work with cloud and packer
Why?
Hello @holmanb
First i must place some context elements.
Our team are currently buidling a complete provisioning ecosystem on top of cloud-init and packer about redhat GCE compute instance. Packer combinated with ansible is used to personalise the "frozen" parametters and setups. But many settings still must been tuned hot-stage to complie with environnement and stuff like that.
We booth share to our users prepackaged instance image that implie a base image with cloud-init configuration. But we also would like to let our users to personalise thoses images by using packer and ansible one more time.
In this precise case, because the instance image are kind of spring loaded with cloud-init ready to go at startup it make it complicated to provisionning and personalise it without have cloud-init running in first place trying to do his job.
Because packer or either Google compute instance, (but probably most of the cloud platform) are provisionning instances before do anything else. It's not possible to alterate GRUB lines or preload at early step environnement variable that would disable it.
I hope it's more clear why..
Thank's Ludovic.
The issue you've described does not appear to be problem with cloud-init, rather it sounds like a problem with the method you (or your users) are using to "re-customise" disk images.
You first create disk images that are configured for cloud-init to run when a VM using one of those disk images boots - that is an expected way to use cloud-init.
Whenever you decide to perform additional (re)configuration of those disk images you are doing so via Packer which creates a VM booting using one of those disk images. Obviously Packer booting such a disk image results in cloud-init running, as that is the way the disk image has been configured.
So I'd assume that you either need to (a) tidy up the Packer VM after you are finished (re)configuring it to remove cloud-init related information (i.e. run "cloud-init clean") so that upon the next boot of the (re)configured disk image cloud-init acts as it should on a 1st/clean boot, or (b) use a different mechanism to (re)configure disk images that does not involve booting them (i.e. mounting a disk image's filesystems and then chrooting into it to make changes).
Yeah but it's a point of view, because cloud-init itself provide options to disable-it but it's hardly managable to use it on-fly during boot time on cloud platform, unlike if it was on-premise. Re-configure with packer is just the way that made me discover it.
Anyway i've managed to find a 1st step solution that is quite "easy" and "clean"
This code allow to disable cloud-init himself on-fly depending on the persistant disk-id, that packer allow me to personalise.
bootcmd:
# Disable temporary Cloud-init when packer context exec is detected
- BUILD_PACKER=$(ls /dev/disk/by-id/ | grep google-packer)
- if [ ! -z "$BUILD_PACKER" ]; then systemctl set-environment KERNEL_CMDLINE='cloud-init=disabled' && echo 'Packer is detected : Disable cloud-init..'; fi
This configuration in the cloud-init *.cfg file allow to intercept cloud-init startup and disable it temporary until stop_init metadata is removed or set to false. meant to be used with GCE instance.
bootcmd:
# Disable cloud-init when stop_init instance metatada is "true"
- 'STOP_INIT=$(curl -H "Metadata-Flavor: Google" http://metadata.google.internal/computeMetadata/v1/instance/attributes/stop_init)'
- 'if [[ "$STOP_INIT" == "true" ]]; then systemctl set-environment KERNEL_CMDLINE="cloud-init=disabled" && cloud-init clean; fi'