After completing this section, you should be able to prepare a template that includes cloud-init, and use that template to create a virtual machine.
cloud-init Overview
The cloud-init tool is used to automate the initial setup of virtual machines, such as configuring the host name, network interfaces, and authorized keys.
It can be used to avoid conflicts on the network when provisioning virtual machines that have been deployed based on a template.
To use this tool, the cloud-init package must first be installed on the virtual machine.
On a Red Hat Enterprise Linux 8 system, the cloud-init package is available in the rhel-8-for-x86_64-appstream-rpms repository.
On a Red Hat Enterprise Linux 7 system, the cloud-init package is available in the rhel-7-server-rpms repository.
Once installed, the cloud-init service starts during the boot process to search for configuration instructions.
Use the options in the Run Once window to provide instructions for the immediate boot process. You can persistently configure cloud-init to run every time the virtual machine boots by editing the virtual machine, and making changes to the Initial Run tab in the advanced options view. The same changes can be made to a template, so that any virtual machine created from the template will always run cloud-init at boot.
cloud-init can be used to automate the configuration of virtual machines in a variety of scenarios. Some uses include:
Customizing virtual machines using a standard template: Use the Use Cloud-Init/Sysprep options in the Initial Run tab of the New Template and Edit Template windows to specify options for customizing virtual machines that are based on this template.
Customizing virtual machines using "Initial Run": Administrators can use the cloud-init options in the Initial Run section of the Run Once window to initialize a virtual machine.
This could be used to override settings set by a template.
As soon as the cloud-init package is installed on your Linux virtual machine, you can use that machine to create a template with cloud-init enabled.
Configure the template so that the advanced option Initial Run has the setting Use Cloud-Init/Sysprep selected. This enables additional configuration options for setting host name, time zone, authentication and network properties, and for running customer cloud-init scripts.
There are two easy ways to apply Initial Run settings to the template.
The template inherits any settings from the Initial Run configuration of the original virtual machine, just like it inherits other characteristics of the virtual machine. However, this means you have to change the base virtual machine's settings and then create the template.
You can create the template normally, and then use Edit Template to change the Initial Run settings for cloud-init. The original virtual machine will not have these settings applied, but machines created from the template will.
If the cloud-init settings are in the template, then when you create a new virtual machine, those Initial Run settings are applied to the virtual machine by default. You also have the option of overriding those settings from the New Virtual Machine window when you create the VM from the template.
The following graphic shows the Initial Run tab from the Edit Template window.
The Initial Run cloud-init settings, displayed in the Edit Template window, are the same as those in the Edit Virtual Machine window, and the Run Virtual Machine(s) window, when you choose to make one-time changes with the menu option.
A static host name for the virtual machine is set when the machines boots. Set the initial run for an individual virtual machine, rather than as part of the template itself. Often, the host name is set in combination with configuring a static IP address, where the host name and IP address match DNS records.
Authentication allows you to configure a user with a password and public keys, authorized to allow SSH logins. This could create a new user, or reconfigure an existing one, in the virtual machine. Be aware that this user (whether new or existing) is given full sudo access without needing to enter a sudo password. To create a regular user, use the Custom Script section.
Networks allows you to make networking changes.
This includes specifying DNS information to be used in /etc/resolv.conf file, as well as configuring network interfaces to have either static or dynamic addresses.
Custom Script allows you to run a custom cloud-init script in cloud-config YAML format.
While cloud-init uses YAML, and the syntax looks similar to Ansible, cloud-init is not Ansible.
For example, the following custom script will create a user named developer2 with a password of redhat.
If you forget to add the lock_passwd: false line, or you accidentally use lock_password instead of lock_passwd, you cannot simply add the line and use the script again.
You must either log in to the machine, delete the user, and then run the custom script again, or you must unlock the user manually.
The following portion of a cloud-init custom script creates a regular user, named developer2, with a password of redhat:
users:
- name: developer2
passwd: $6$l.uq5YSZ/aebb.SN$S/KjOZQFn.3bZcmlgBRGF7fIEefBPCHD.k46IW0dKx/XK.I0DmZQBKGgCIxg7mykIIzzmW02JyZwXgORfHWBE.
lock_passwd: false
The following portion of a cloud-init custom script creates a .vimrc file for user developer2:
write_files:
- path: /home/developer2/.vimrc
content: |
set ai et ts=2 sts=2 sw=2
owner: developer2:developer2
mode: '0664'Be aware that spacing and indentation is very important to YAML, and that incorrect indentation can cause the script to fail. Therefore, it may be easier to create the script in a separate text editor and then copy and paste the script into the Custom Script field. See http://cloudinit.readthedocs.io for cloud-config examples.
Some changes made by cloud-init are persistent, even when applied by starting the virtual machine using Run Once. This includes setting the host name, configuring a time zone, adding users, setting or changing user passwords, and actions performed by custom scripts.
Network settings are not persistent.
Even if you persistently specify cloud-init settings by editing a virtual machine, network settings are not applied unless you start the virtual machine using , and enable Use Cloud-Init in the Initial Run settings.
To persistently configure network settings, use the write_files module as part of a custom script.
New virtual machines can be deployed using a cloud-init enabled template. You could just create and run a new virtual machine from the template, and the cloud-init directives included in the template would be used to customize the resulting virtual machine. Alternatively, you can override the template's cloud-init settings to further customize the resulting virtual machine. For example, use cloud-init to configure the virtual machine, including host name, creating additional users, changing the network configuration, and specifying a custom cloud-init script to run at boot.
Unless you are making a very simple change, it is often a better idea to edit the settings for the virtual machine, rather than specifying the settings through . When advanced options display while editing a virtual machine, select the Initial Run tab. Select the box for Use Cloud-Init/Sysprep, and specify the cloud-init settings.
Apply the settings made when editing a virtual machine by starting the virtual machine using . From the Run Virtual Machine(s) dialog, click the + icon next to Initial Run, and then make sure that the check box in the Use Cloud-Init line is selected. You will see that the cloud-init settings are populated based on the changes that you made when you edited the virtual machine. Making additional changes here will not modify the Initial Run settings, which are accessible when you edit a virtual machine. If you find that you made a mistake with your cloud-init settings, rather than having to type everything in again, you can edit the virtual machine to make slight adjustments to the Initial Run settings. Then restart the machine again, using .
Further information is available in the "Using Cloud-Init to Automate the Configuration of Virtual Machines" chapter of the Virtual Machine Management Guide for Red Hat Virtualization at https://access.redhat.com/documentation/en-us/red_hat_virtualization/4.3/html-single/virtual_machine_management_guide/index#Using_Cloud-Init_to_Automate_the_Configuration_of_Virtual_Machines