Score:1

How to ensure Cloud-Init runs exactly once and once only?

us flag

As far as I can see, cloud-init runs every time the config changes. Not just the very first time the system boots, but every time the provided configuration changes. This makes somewhat sense, as I guess it's hard to define the "first time" (the cloned VM already ran before being frozen and used as template, so it's never really the first time). However, I've—from time to time, quite rarely, but still—found that cloud-init re-runs on already provisioned system when they reboot.

Some steps, however, seem to screw up the setup when cloud-init is run on a fully configured system. For example, if it is run one more time after the initial setup and cloud-init sets some configuration to value X, but you had it manually overridden afterwards to Y and now cloud-init re-runs to set it back to X. Or, have the system recreate your SSH host keys.

I've thus found it quite useful to manually run:

sudo touch /etc/cloud/cloud-init.disabled

...after the initial setup to prevent it from ever running cloud-init again. (In cases, where cloud-init really is only used for an initial "clone & set IPs/hostname" kind of configurations.)

But is there any way to automate this? Like adding some parameter to the /etc/cloud/cloud.cfg that disabled itself after the next run?

cn flag
EML
It *always* runs, unless you disable it. Even if you don't have per-boot config, the per-instance config can be persuaded to run again. What you're doing is, IMHO, the right thing to do (or modifying the boot command line). Just put this in you `/etc/rc.d` or similar.
Score:2
jp flag

Different cloud-init modules have different frequencies: once-per-instance and always. You can check attribute Module frequency for each module in the Modules documentation. For example, bootcmd runs on every boot but runcmd only on the first boot. The frequencies can be overridden in a cloud-config file modules lists - cloud_init_modules, cloud_config_modules and cloud_final_modules. Example override:

cloud_final_modules:
- [scripts-user, always]
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.