Chapter 6. Rerunning cloud-init on a virtual machine
When additional configurations, such as modification in directives, modules, or datasources, are required for a virtual machine (VM) configured by the cloud-init
service, you can rerun cloud-init
.
Rerunning the cloud-init
process can result in data loss and highly discouraged for a production environment. Credentials such as SSH keys and password could be lost or replaced.
6.1. Modifying a VM created from a KVM Guest Image after cloud-init has run
When you launch a VM with the cloud-init
package installed and enabled, cloud-init
runs in its default state on the initial boot of the VM. If you want to change the setting of the VM, you can modify your cloud-init
configuration and rerun cloud-init
.
Procedure
- Log in to your VM.
-
Add or change directives, for example, modify the
cloud.cfg
file in the/etc/cloud
directory or add directives to the/etc/cloud/cloud.cfg.d
directory. -
Run the
cloud-init clean
command to clean directories so thatcloud-init
can rerun. Run the following commands as
root
to clean the VM data:rm -Rf /var/lib/cloud/instances/ rm -Rf /var/lib/cloud/instance rm -Rf /var/lib/cloud/data/
rm -Rf /var/lib/cloud/instances/ rm -Rf /var/lib/cloud/instance rm -Rf /var/lib/cloud/data/
Copy to Clipboard Copied! NoteYou can save the cleaned image as a template image and use that image for multiple VMs. The new VMs will use the updated configuration to run
cloud-init
.-
Rerun
cloud-init
or reboot the VM to implement the configuration changes you made. For details on re-runningcloud-init
, see Re-run cloud-init.
6.2. Modifying a VM for a specific datasource after cloud-init has run
You can modify your cloud-init
configuration before rerunning cloud-init
. Note that the exact steps you need to perform vary based on your datasource. The following procedure uses OpenStack as an example datasource.
Procedure
-
Create and launch an instance for the OpenStack Platform. For information about creating instances for OpenStack, see Creating an instance. In this example, the virtual machine (VM) includes
cloud-init
, which runs upon boot of the VM. -
Add or change directives. For example, modify the
user-data.file
file that is stored on the OpenStack HTTP server. Clean the virtual machine:
rm -rf /etc/resolv.conf /run/cloud-init userdel -rf cloud-user hostnamectl set-hostname localhost.localdomain rm /etc/NetworkManager/conf.d/99-cloud-init.conf
# rm -rf /etc/resolv.conf /run/cloud-init # userdel -rf cloud-user # hostnamectl set-hostname localhost.localdomain # rm /etc/NetworkManager/conf.d/99-cloud-init.conf
Copy to Clipboard Copied! NoteYou can save the cleaned image as a template image and use that image for multiple virtual machines. The new virtual machines run
cloud-init
, using your updatedcloud-init
configuration.-
Rerun
cloud-init
or reboot the VM to implement the configuration changes you made. For details on re-runningcloud-init
, see Re-run cloud-init.