Customize VM Instance with Cloud-init

This Platform9 OpenStack tutorial describes tips on using various options to customize a virtual machine using cloud-init.

Customize Virtual Machine Hostname

Virtual machines deployed using cloud-init enabled images will by default be configured with hostname created using the name given to the virtual machine, and a randomization suffix.

Set Default Password and Inject Custom Script Using User Data

Create a text file with following information.

Copy

Paste the configuration above to the cloud-init config text area:

where:

The first line is NOT a comment and should not be skipped or modified. It is a directive indicating that the content that follows should be merged with cloud.cfg that the virtual machine instance already has.

This is a YAML file. Be careful with the YAML syntax around spacing. Failing to include appropriate spacing where required will result in silent failures (For example, inability to logon to the instance).

Verify the YAML syntax by writing small sample python code such as this:

Python
Copy

The output should be a Python dict string representation of the above YAML file.

Output
Copy
  • 'winterwonderland' is the desired default password you wish to set for this virtual machine
  • 'runcmd' is a list of commands you would like to inject into this virtual machine. Each element can be a string or another list (like execev). Following article offers comprehensive details: Cloud-Init: Run commands on first boot.

Finally, invoke Nova command via CLI or REST API to create a new instance, passing the extra parameters for customization.

CLI
Copy

Inject SSH Key-Pair into Virtual Machine

  • First, generate a new ssh key pair to be used for the new virtual machine:
Bash
Copy
  • Now add the key pair to Nova:
Nova
Copy
  • List key pairs registered with Nova:
Nova
Copy
  • Now with nova boot command you can optionally pass the key-name as the argument:
Nova
Copy
  • That's it. Once the virtual machine instance is active, you can ssh into the instance using the private key.

Example cloud-config File with Comments

Some features only work with cloudinit >= v0.7.5 (Ubuntu 14 or newer, and CentOS 7.x)

YAML
Copy
Type to search, ESC to discard
Type to search, ESC to discard
Type to search, ESC to discard