Run a script via cloud-init

cloud-initserver-setupterraform

I know cloud-init allows to run commands as part of the cloud-init yml

runcmd:
 - [ pwd ]

but I am looking for a way to execute a full shell script.

Some people just pipe the output of a curl into a shell – but that feels like a very wonky approach. Maybe the script could be integrated into the yml via some templating, but that doesn't sound too great either.

Much better would be to ship the script alongside of the yml to the host and refer to it.
But I haven't found any documentation on how to do this.

Is this possible?

I am using terraform to setup the server, but I am wondering if this could also done with pure cloud-init.

Best Answer

Try combining it with write_files.

You can write script somewhere (e.g. as /usr/local/bin/myscript.sh) and then do runcmd: /usr/local/bin/myscript.sh

Make sure to set executable permission.

Related Topic