Introduction to cloud-init

How to know if cloud-init is installed in your VM

$ dpkg --get-selections | grep cloud-init

How to install cloud-init

$ sudo apt install cloud-init

The cloud.cfg file

In an instance that has cloud-init, there will be a directory /etc/cloud, here lies the cloud.cfg file

$ ls -l /etc/cloud
cloud.cfg  cloud.cfg.d  cloud.cfg.old  templates

cloud-init uses YAML-formatted file instructions to perform tasks. You decide the initial configuration you want cloud-init to perform by providing instructions within the YAML files. When an instance boots, the cloud-init service starts and searches for and executes the instructions. You define the tasks by configuring the /etc/cloud/cloud.cfg file

Lets look at a typical cloud.cfg file

# The top level settings are used as module
# and system configuration.

# This will cause the set+update hostname module to not operate (if true)
preserve_hostname: true

# Example datasource config
# datasource:
#    Ec2:
#      metadata_urls: [ 'blah.com' ]
#      timeout: 5 # (defaults to 50 seconds)
#      max_wait: 10 # (defaults to 120 seconds)

# disbaled network configuration as it conflicts with udev
network:
   config: disabled

# The modules that run in the 'init' stage
cloud_init_modules:
 - migrator
 - seed_random
 - bootcmd
 - write-files
 - ca-certs
 - rsyslog
 - users-groups
 - ssh

# The modules that run in the 'config' stage
cloud_config_modules:
# Emit the cloud config ready event
# this can be used by upstart jobs for 'start on cloud-config'.
 - emit_upstart
 - ssh-import-id
 - set-passwords
 - grub-dpkg
 - timezone
 - disable-ec2-metadata
 - runcmd
 - bluecat-license
 - bluecat-netconf
 - bluecat-service-config

# The modules that run in the 'final' stage
cloud_final_modules:
 - fan
 - scripts-vendor
 - scripts-per-once
 - scripts-per-boot
 - scripts-per-instance
 - scripts-user
 - ssh-authkey-fingerprints
 - keys-to-console
 - phone-home
 - final-message
 - power-state-change

We can see cloud_init_modulescloud_config_modules, and cloud_final_modules blocks. Modules are predefined tasks that we want cloud-init to do, there is a reference for what each module does here Module Reference

The modules are executed in order within three phases:

  1. The network phase (cloud_init_modules)
  2. The configuration phase (cloud_config_modules)
  3. The final phase (cloud_final_modules)

When cloud-init runs for the first time on a VM, all the modules you have configured run in their respective phases. On a subsequent running of cloud-init, whether a module runs within a phase depends on the module frequency of the individual module. Some modules run every time cloud-init runs; some modules only run the first time cloud-init runs, even if the instance ID changes.

The possible module frequency values are as follows:

  • Per instance means that the module runs on first boot of an instance. For example, if you clone an instance or create a new instance from a saved image, the modules designated as per instance run again.
  • Per once means that the module runs only once. For example, if you clone an instance or create a new instance from a saved image, the modules designated per once do not run again on those instances.
  • Per always means the module runs on every boot.

When checking a module in Module Reference the module will say which frecuancy it has