Skip to content

Instantly share code, notes, and snippets.

@mikesager
Created July 20, 2020 04:00
Show Gist options
  • Select an option

  • Save mikesager/f091fc374547e4304ec2d8c517f13cbe to your computer and use it in GitHub Desktop.

Select an option

Save mikesager/f091fc374547e4304ec2d8c517f13cbe to your computer and use it in GitHub Desktop.
SOLVED: cloud-init runcmd doesn't work

If you've added some runcmd lines to your cloud-init config, and the commands don't seem to be executing, here's a few things you should know:

WTF is going on?

  1. runcmd only has code to "shellify" your strings or array of strings (i.e: turns them into a shell script)
  2. runcmd will only write a shell script into the file /var/lib/cloud/instance/scripts/runcmd
  3. runcmd is part of the cloud_config_modules boot stage
  4. You need to execute that runcmd shell script using the scripts-user module
  5. scripts-user is part of the cloud_final_modules boot stage

Solution

Here's an example config that WORKS: (as of v19.3)

cloud_config_modules:
  - runcmd

cloud_final_modules:
  - scripts-user

runcmd:
  - [ touch, /tmp/myfile ]

You can test this by manually running cloud-init modules --mode config; cloud-init modules --mode final.

Yes, I agree, the docs aren't very good ;)

@b-mc
Copy link
Copy Markdown

b-mc commented Dec 12, 2024

4 years later and your gist is still helping - thank you.
What's the point of writing scripts first in one stage, only to run them later in a different one?

@mastier
Copy link
Copy Markdown

mastier commented Feb 7, 2025

Thanks! Useful explanation!

@didmar
Copy link
Copy Markdown

didmar commented Feb 9, 2025

Very helpful, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment