7.8. Using Cloud-Init to Automate the Configuration of Virtual Machines
7.8. Using Cloud-Init to Automate the Configuration of Virtual Machines Cloud-Init is a tool for automating the initial setup of virtual machines such as configuring the host name, network interfaces, and authorized keys. It can be used when provisioning virtual machines that have been deployed based on a template to avoid conflicts on the network. To use this tool, the cloud-init package must first be installed on the virtual machine. Once installed, the Cloud-Init service starts during the boot process to search for instructions on what to configure. You can then use options in the Run Once window to provide these instructions one time only, or options in the New Virtual Machine , Edit Virtual Machine and Edit Template windows to provide these instructions every time the virtual machine starts. Note Alternatively, you can configure Cloud-Init with Ansible , Python , Java , or Ruby . 7.8.1. Cloud-Init Use Case Scenarios Cloud-Init can be used to automate the configuration of virtual machines in a variety of scenarios. Several common scenarios are as follows: Virtual Machines Created Based on Templates You can use the Cloud-Init options in the Initial Run section of the Run Once window to initialize a virtual machine that was created based on a template. This allows you to customize the virtual machine the first time that virtual machine is started. Virtual Machine Templates You can use the Use Cloud-Init/Sysprep options in the Initial Run tab of the Edit Template window to specify options for customizing virtual machines created based on that template. Virtual Machine Pools You can use the Use Cloud-Init/Sysprep options in the Initial Run tab of the New Pool window to specify options for customizing virtual machines taken from that virtual machine pool. This allows you to specify a set of standard settings that will be applied every time a virtual machine is taken from that virtual machine pool. You can inherit or override the options specified for the template on which the virtual machine is based, or specify options for the virtual machine pool itself. 7.8.2. Installing Cloud-Init This procedure describes how to install Cloud-Init on a virtual machine. Once Cloud-Init is installed, you can create a template based on this virtual machine. Virtual machines created based on this template can leverage Cloud-Init functions, such as configuring the host name, time zone, root password, authorized keys, network interfaces, DNS service, etc on boot. Installing Cloud-Init Log in to the virtual machine. Enable the repositories: For Red Hat Enterprise Linux 6: For Red Hat Enterprise Linux 7: Install the cloud-init package and dependencies: 7.8.3. Using Cloud-Init to Prepare a Template As long as the cloud-init package is installed on a Linux virtual machine, you can use the virtual machine to make a cloud-init enabled template. Specify a set of standard settings to be included in a template as described in the following procedure or, alternatively, skip the Cloud-Init settings steps and configure them when creating a virtual machine based on this template. Note While the following procedure outlines how to use Cloud-Init when preparing a template, the same settings are also available in the New Virtual Machine , Edit Template , and Run Once windows. Using Cloud-Init to Prepare a Template Click Compute Templates and select a template. Click Edit . Click Show Advanced Options Click the Initial Run tab and select the Use Cloud-Init/Sysprep check box. Enter a host name in the VM Hostname text field. Select the Configure Time Zone check box and select a time zone from the Time Zone drop-down list. Expand the Authentication section. Select the Use already configured password check box to use the existing credentials, or clear that check box and enter a root password in the Password and Verify Password text fields to specify a new root password. Enter any SSH keys to be added to the authorized hosts file on the virtual machine in the SSH Authorized Keys text area. Select the Regenerate SSH Keys check box to regenerate SSH keys for the virtual machine. Expand the Networks section. Enter any DNS servers in the DNS Servers text field. Enter any DNS search domains in the DNS Search Domains text field. Select the In-guest Network Interface check box and use the + Add new and - Renove selected buttons to add or remove network interfaces to or from the virtual machine. Important You must specify the correct network interface name and number (for example, eth0 , eno3 , enp0s ). Otherwise, the virtual machine's interface connection will be up, but it will not have the cloud-init network configuration. Expand the Custom Script section and enter any custom scripts in the Custom Script text area. Click OK . You can now provision new virtual machines using this template. 7.8.4. Using Cloud-Init to Initialize a Virtual Machine Use Cloud-Init to automate the initial configuration of a Linux virtual machine. You can use the Cloud-Init fields to configure a virtual machine's host name, time zone, root password, authorized keys, network interfaces, and DNS service. You can also specify a custom script, a script in YAML format, to run on boot. The custom script allows for additional Cloud-Init configuration that is supported by Cloud-Init but not available in the Cloud-Init fields. For more information on custom script examples, see Cloud config examples . Using Cloud-Init to Initialize a Virtual Machine This procedure starts a virtual machine with a set of Cloud-Init settings. If the relevant settings are included in the template the virtual machine is based on, review the settings, make changes where appropriate, and click OK to start the virtual machine. Click Compute Virtual Machines and select a virtual machine. Click the Run drop-down button and select Run Once . Expand the Initial Run section and select the Cloud-Init check box. Enter a host name in the VM Hostname text field. Select the Configure Time Zone check box and select a time zone from the Time Zone drop-down menu. Select the Use already configured password check box to use the existing credentials, or clear that check box and enter a root password in the Password and Verify Password text fields to specify a new root password. Enter any SSH keys to be added to the authorized hosts file on the virtual machine in the SSH Authorized Keys text area. Select the Regenerate SSH Keys check box to regenerate SSH keys for the virtual machine. Enter any DNS servers in the DNS Servers text field. Enter any DNS search domains in the DNS Search Domains text field. Select the Network check box and use the + and - buttons to add or remove network interfaces to or from the virtual machine. Important You must specify the correct network interface name and number (for example, eth0 , eno3 , enp0s ). Otherwise, the virtual machine's interface connection will be up, but the cloud-init network configuration will not be defined in it. Enter a custom script in the Custom Script text area. Make sure the values specified in the script are appropriate. Otherwise, the action will fail. Click OK . Note To check if a virtual machine has Cloud-Init installed, select a virtual machine and click the Applications sub-tab. Only shown if the guest agent is installed.
subscription-manager repos --enable=rhel-6-server-rpms --enable=rhel-6-server-rh-common-rpms subscription-manager repos --enable=rhel-7-server-rpms --enable=rhel-7-server-rh-common-rpms yum install cloud-init