Skip to content

Use User Data for Initial Configuration

cloud-init can be leveraged to provision resources by using user data scripts. This chapter will guide you through the process of using user data to perform initial configurations, including practical examples to illustrate its usage.

Introduction to User Data

User data scripts are used to automate the configuration of instances at boot time. These scripts can be provided in various formats such as shell scripts, cloud-config, and more. Cloud-Init interprets these scripts and performs the specified tasks, such as installing packages, configuring services, and setting up network configurations.

How to Use User Data with Cloud-Init

To use user data with cloud-init in OpenStack, you need to provide the user data script when launching an instance. This can be done through the OpenStack dashboard (Horizon) or using the OpenStack CLI.

Example 1: Configure a Web Server

In this example, we will demonstrate how to install Apache web server and configure a basic HTML page on an OpenStack instance using cloud-init.

  • Navigate to Project > Compute > Instances and click Launch Instance.
  • In the Details tab, provide the necessary instance details such as Instance Name, Flavor, and Network.
  • Switch to the Configuration tab and add Customization Script (Customization Script is analogous to User Data in other systems):
#!/bin/bash
# Update package list and install Apache
apt-get update
apt-get install -y apache2

# Enable Apache to start on boot
systemctl enable apache2

# Start Apache service
systemctl start apache2

# Create a simple web page
echo "<html><body><h1>Hello from Cloud-Init</h1></body></html>" > /var/www/html/index.html
  • Click Launch Instance to start the instance.

Once the instance is up and running, it will have Apache installed and serving a basic HTML page.

  • Save the following script as webserver-user-data.sh:
#!/bin/bash
# Update package list and install Apache
apt-get update
apt-get install -y apache2

# Enable Apache to start on boot
systemctl enable apache2

# Start Apache service
systemctl start apache2

# Create a simple web page
echo "<html><body><h1>Hello from Cloud-Init</h1></body></html>" > /var/www/html/index.html
  • Launch an Instance with user data.

Before running the command to launch the instance, you need to replace placeholders with actual values:

  • <network_id>: The ID of the network to which the instance should be connected. This is required to provide the instance with networking capabilities.

  • <key_name>: The name of the SSH key pair to be used for accessing the instance. This key should already be uploaded to your OpenStack environment.

  • <instance_name>: A unique name for the instance you are creating.

Using the OpenStack CLI, you can launch an instance with this user data script:

openstack server create --flavor c001r002 --image Ubuntu-22.04 (Switch Cloud) --nic net-id=<network_id> --key-name <key_name> --user-data ./webserver-user-data.sh <instance_name>

Example 2: Set Up a Database Server using YAML syntax

In this example, we will provision an instance and configure it as a MySQL database server using a cloud-config script.

Please follow the steps from the Example 1, but replace the user data script with the code example below:

  • User data script:
#cloud-config
package_update: true
packages:
- mysql-server

runcmd:
- systemctl enable mysql
- systemctl start mysql
- mysql -e "CREATE USER 'user'@'%' IDENTIFIED BY 'password';"
- mysql -e "GRANT ALL PRIVILEGES ON *.* TO 'user'@'%' WITH GRANT OPTION;"
- mysql -e "FLUSH PRIVILEGES;"
  • Save the following script as dbserver-user-data.yaml:
#cloud-config
package_update: true
packages:
- mysql-server

runcmd:
- systemctl enable mysql
- systemctl start mysql
- mysql -e "CREATE USER 'user'@'%' IDENTIFIED BY 'password';"
- mysql -e "GRANT ALL PRIVILEGES ON *.* TO 'user'@'%' WITH GRANT OPTION;"
- mysql -e "FLUSH PRIVILEGES;"
  • Launch an Instance with user sata using OpenStack CLI:
openstack server create --flavor c001r002 --image Ubuntu-22.04 (Switch Cloud) --nic net-id=<NETWORK_ID> --key-name <KEY_NAME> --user-data ./dbserver-user-data.yaml <INSTANCE_NAME>