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.
runcmdincloud_config_modulesandscripts-userincloud_final_moduleswill 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_modulesorcloud_final_modulesin your cloud-config.Did you include
#cloud-configat 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 oflsand the date/echo will be in/var/log/cloud-init-output.logandfoo.barwill 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 --longto see if cloud-init succeeded or failed. You can also check/var/log/cloud-init.logforTracebackorWARNto get an idea of what failed. Searching forrumcmdorscripts-usershould tell you if those commands ran and what was written out where.