I am running multipass
with cloudinit
and I have a bash script which I need to run after the VMs have been deployed once but for each user account. It has simple commands in it mostly for customising vim and bash.rc
. How can I achieve this?
I tried playing around with runcmd feature of cloudinit but it is a bit problematic and even simple commands like the below don't output anything.
package_update: true
package_upgrade: true
packages:
- git
- openssh-server
- zip
- tree
- vim
- htop
- net-tools
- sysstat
- locate
cloud_config_modules:
- runcmd
cloud_final_modules:
- scripts-user
runcmd:
- [ ls, -l, / ]
- [ sh, -xc, "echo $(date) ': hello world!'" ]
- [ bash, -c, echo "=========hello world'=========" >>foo.bar]
users:
- name: aryan
lock_passwd: true
sudo: ALL=(ALL) NOPASSWD:ALL
shell: /bin/bash
ssh_authorized_keys:
- ...
That gist is technically correct, but it's a little misleading.
runcmd
incloud_config_modules
andscripts-user
incloud_final_modules
will already be specified by default in/etc/cloud/cloud.cfg
, so unless you want to explicitly turn off the other modules in use by cloud-init (you don't want to do this unless you have a good reason to), you don't need to specifycloud_config_modules
orcloud_final_modules
in your cloud-config.Did you include
#cloud-config
at the top of your userdata script? If not, your cloud config is invalid. If you did include it, these runcmds will all work. The output ofls
and the date/echo will be in/var/log/cloud-init-output.log
andfoo.bar
will be in/
. If not, there's something else wrong in your cloud config, or something that prevented cloud-init from initializing completely.On your launched instance, you can run
cloud-init status --long
to see if cloud-init succeeded or failed. You can also check/var/log/cloud-init.log
forTraceback
orWARN
to get an idea of what failed. Searching forrumcmd
orscripts-user
should tell you if those commands ran and what was written out where.