How to run custom install commands/scripts with cloudinit

2.2k views Asked by At

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:                                                    
       - ...
1

There are 1 answers

7
falcojr On

That gist is technically correct, but it's a little misleading. runcmd in cloud_config_modules and scripts-user in cloud_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 specify cloud_config_modules or cloud_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 of ls and the date/echo will be in /var/log/cloud-init-output.log and foo.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 for Traceback or WARN to get an idea of what failed. Searching for rumcmd or scripts-user should tell you if those commands ran and what was written out where.