Custom cloud-init
Cloud-init is a tool that helps to manage cloud instance initialization. Find out more in Cloud-init documentation.
All cloud images have cloud-init pre-installed and set up with default configuration, which works reasonably well for most use-cases.
However, you may find yourself in need of some additional setup.
Applying custom cloud-init configuration
New instance
You can provide your instance with custom configuration in the form of ‘cloud-config’ (also called ‘user data’) at the time of its creation.
-
Have your
cloud-configYAML file ready.my-cloud-config.yaml#cloud-config runcmd: - [ls, -l, /] - [sh, -xc, 'echo $(date) '': hello world!'''] - [sh, -c, echo "=========hello world'========="] - ls -l /root- Make sure to include the
#cloud-configannotation on the first line of the file! - See more examples at Cloud config examples library.
- Make sure to include the
-
Pass the
cloud-configto the new instance.- In Compute > Instances, click the Launch Instance button.
Example

- Follow the launch steps as described e.g. in the Create First Instance guide.
- In the Configuration step, either click Browse and select your
cloud-configYAML file, or paste the config directly into the Customization Script textarea.Example

- Complete the steps and launch the instance.
Use the
--user-dataargument of theopenstackCLI client.openstack server create ... --user-data ./my-cloud-config.yaml my-instance - In Compute > Instances, click the Launch Instance button.
Existing instance
- Login to the instance (via SSH, web console, …).
ubuntu@my-instance:~$ ssh ubuntu@147.X.X.X - Become
rootand add the configuration to the/etc/cloud/directory.ubuntu@my-instance:~$ sudo -i root@my-instance:~$ cat <<EOF > /etc/cloud/cloud.cfg.d/99_hello-world.cfg runcmd: - [ls, -l, /] - [sh, -xc, 'echo $(date) '': hello world!'''] - [sh, -c, echo "=========hello world'========="] - ls -l /root EOF - (Optional) If you want to apply your changes immediately, force
cloud-initto reload the configuration and re-initiate.root@my-instance:~$ cloud-init clean && cloud-init init && cloud-init modules --mode=config && cloud-init modules --mode=final- This might prove useful in case of changes that affect runtime behavior, such as auto-configuration of network devices (see below).
Warning
This set of commands makes
cloud-initreinitiate all data generated during the first boot of the instance, including server SSH keys. This means that next time you log in using SSH, you will see a security warning that the new key does not match the one stored in your local trusted keys (stored usually in~/.ssh/known_hosts).Warning: Remote Host Identification has ChangedIf you are sure about the server security, you may want to remove the old key from
~/.ssh/known_hosts:user@localhost:~$ ssh-keygen -R <hostname or IP>
Custom use-cases
Here, we discuss situations that require additional configuration of cloud-init.
Automatic setup of network interfaces
By default, cloud-init auto-configures NICs on first boot of an instance.
To make it do the auto-configuration on every boot and on the run (hotplug), use this cloud-config:
#cloud-config
updates:
network:
when: [boot, hotplug]See https://cloudinit.readthedocs.io/en/latest/reference/modules.html#install-hotplug.
Last updated on
