Question

How to edit and re-run Cloud-Config user data?

I’m debugging a Cloud-Config user data script. It’d be nice to make changes and re-run, without rebooting, if that’s possible?

Here’s me doing it wrong:

#cloud-config

runcmd:
  - date >> /var/log/runcmd.log

After the instance boots, /var/log/cloud-init.log has:

Running module runcmd
[...]
finish: modules-config/config-runcmd: SUCCESS: config-runcmd ran successfully

and runcmd.log has one entry – so far, so good. Goal #1 is to add a second entry.

cloud-init init seems to only run vendor data. The internet says to first run cloud-init clean. This seems to delete my user data.

cloud-init single --name runcmd --frequency always writes the script out, but never runs it:

Cloud-init v. 19.1-1-gbaa47854-0ubuntu1~18.04.1 running 'single'
[...]
Writing to /var/lib/cloud/instances/152272562/scripts/runcmd - wb: [700] 38 bytes
Reading from /proc/uptime (quiet=False)
Read 14 bytes from /proc/uptime
cloud-init mode 'single' took 0.198 seconds (0.20)

I beleive the semaphore file is ignored ignored with --frequency always. Removing the semaphore file didn’t help.

If I reach goal #1, the second is to edit the user data – will Cloud-Config read from /var/lib/cloud/instance/user-data.txt, or is this just an artifact?

Subscribe
Share

Submit an answer
You can type!ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

To run “runcmd” you have to execute two modules:

  1. runcmd create script
  2. scripts_user actually runs it
 cloud-init --file cloud-init.yaml single --name runcmd --frequency=always
 cloud-init --file cloud-init.yaml single --name scripts_user --frequency=always

I’ve run into that same cloud-init debugging issue myself, I’m asking around internally to see if anyone has a way to re-run the cloud-init user data in a running vm.

One alternative: If you install doctl you can create droplets from the CLI and specify a local file as user-data.

There’s probably a way to automate it further, but I’ve found myself running:

doctl compute droplet create <name> --user-data-file <path-to-file> --region <region-slug> --image <image-slug> --size <size-slug> --ssh-keys <saved-key-id>

This isn’t perfect, but it at least allows you to make edits to your cloud-init and quickly deploy a new instance to test.

Even kicking off 5 creates of 1GB instances will only cost you $0.007x5 = 3.5 cents, as long as you remember to delete!

EDIT: Also to answer your last q: I’m pretty sure /var/lib/cloud/instance/user-data.txt, IS just an artifact. I.E. if you edit it and rebuild the Droplet it will still run your original user-data