Search

7.9. Using Cloud-Init to Automate the Configuration of Virtual Machines

download PDF

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.9.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.9.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

  1. Log in to the virtual machine.
  2. Enable the repositories:

    • For Red Hat Enterprise Linux 6:

      # subscription-manager repos \
          --enable=rhel-6-server-rpms \
          --enable=rhel-6-server-rh-common-rpms
    • For Red Hat Enterprise Linux 7:

      # subscription-manager repos \
          --enable=rhel-7-server-rpms \
          --enable=rhel-7-server-rh-common-rpms
    • For Red Hat Enterprise Linux 8, you normally do not need to enable repositories to install Cloud-Init. The Cloud-Init package is part of the AppStream repo, rhel-8-for-x86_64-appstream-rpms, which is enabled by default in Red Hat Enterprise Linux 8.
  3. Install the cloud-init package and dependencies:

    # dnf install cloud-init
    Note

    For versions of Red Hat Enterprise Linux earlier than version 8, use the command yum install cloud-init instead of dnf install cloud-init.

7.9.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

  1. Click Compute Templates and select a template.
  2. Click Edit.
  3. Click Show Advanced Options
  4. Click the Initial Run tab and select the Use Cloud-Init/Sysprep check box.
  5. Enter a host name in the VM Hostname text field.
  6. Select the Configure Time Zone check box and select a time zone from the Time Zone drop-down list.
  7. 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.
  8. 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.

  9. Expand the Custom Script section and enter any custom scripts in the Custom Script text area.
  10. Click OK.

You can now provision new virtual machines using this template.

7.9.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.

  1. Click Compute Virtual Machines and select a virtual machine.
  2. Click the Run drop-down button and select Run Once.
  3. Expand the Initial Run section and select the Cloud-Init check box.
  4. Enter a host name in the VM Hostname text field.
  5. Select the Configure Time Zone check box and select a time zone from the Time Zone drop-down menu.
  6. 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.
  7. Enter any SSH keys to be added to the authorized hosts file on the virtual machine in the SSH Authorized Keys text area.
  8. Select the Regenerate SSH Keys check box to regenerate SSH keys for the virtual machine.
  9. Enter any DNS servers in the DNS Servers text field.
  10. Enter any DNS search domains in the DNS Search Domains text field.
  11. 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.

  12. 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.
  13. 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.

Red Hat logoGithubRedditYoutubeTwitter

Learn

Try, buy, & sell

Communities

About Red Hat Documentation

We help Red Hat users innovate and achieve their goals with our products and services with content they can trust.

Making open source more inclusive

Red Hat is committed to replacing problematic language in our code, documentation, and web properties. For more details, see the Red Hat Blog.

About Red Hat

We deliver hardened solutions that make it easier for enterprises to work across platforms and environments, from the core datacenter to the network edge.

© 2024 Red Hat, Inc.