Vincent's Weblog

CloudInit Basics

CloudInit is a way to initialize virtual machines with some defaults, like user credentials and commands that should be run on startup.

Adding users

I use CloudInit the most to add some users to a VM. You can add multiple users at once too if you add more items

#cloud-config
users:
  - name: vincent
    groups: vincent
    sudo: ALL=(ALL) NOPASSWD:ALL
    shell: /bin/bash
    ssh_authorized_keys:
    - <public_key>
    - <optionally: a second public key>

Updating packages

Another common use is to install packages, the packages list contains the packages that we want to install, package_update and package_upgrade define if we want to update and/or upgrade the packages already installed

#cloud-config
packages:
  - apache2
  - ufw
package_update: true
package_upgrade: true

Running commands

We can also run commands upon installation, for example to enable the firewall but to allow SSH.

#cloud-config
runcmd:
  - ufw allow ssh
  - ufw enable
  - reboot

Combining it all

We can of course combine all the steps above into a single cloudInit file.

#cloud-config
users:
  - name: vincent
    groups: vincent
    sudo: ALL=(ALL) NOPASSWD:ALL
    shell: /bin/bash
    ssh_authorized_keys:
    - <public_key>
    - <optionally: a second public key>
  packages:
  - apache2
  - ufw
  package_update: true
  package_upgrade: true
  runcmd:
  - ufw allow ssh
  - ufw enable
  - reboot

As you can see, using cloudInit is verry simple.