You can inject user data to configure BMSs.

User data scripts of Linux BMSs are customized by using the open-source Cloud-Init architecture. This architecture uses BMS metadata as the data source for automatically configuring the BMSs. The script types are compatible with open-source Cloud-Init. For details about Cloud-Init, see http://cloudinit.readthedocs.io/en/latest/topics/format.html.
By default, the scripts are executed as user root.
- |
User-Data Script |
Cloud-Config Data |
|---|---|---|
Description |
Scripts, such as Shell and Python scripts, are used for custom configurations. |
Methods pre-defined in Cloud-Init, such as the Yum source and SSH key, are used for configuring certain BMS applications. |
Format |
A script must be started with #!, for example, #!/bin/bash and #!/usr/bin/env python. When the BMS is started for the first time, the script will be executed at the rc.local-like level, indicating a low priority in the boot sequence. |
The first line must be #cloud-config, and no space is allowed in front of it. |
Constraint |
Before Base64 encoding, the size of the script, including the first line, cannot exceed 32 KB. |
Before Base64 encoding, the size of the script, including the first line, cannot exceed 32 KB. |
Frequency |
The script is executed only once when the BMS is started for the first time. |
The execution frequency varies depending on the applications installed on the BMS. |
curl http://169.254.169.254/openstack/latest/user_data
This section describes how to inject scripts in different formats into Linux BMSs and view script execution results.
Example 1: Inject a User-Data script.
When creating a BMS, set User Data to As Text and enter the user data script content.
#!/bin/bash echo "Hello, the time is now $(date -R)" | tee /root/output.txt
After the BMS is created, start it and run the cat [file] command to check the script execution result.
[root@XXXXXXXX ~]# cat /root/output.txt Hello, the time is now Mon, 16 Jul 2016 16:03:18+0800
Example 2: Inject a Cloud-Config Data script.
When creating a BMS, set User Data to As Text and enter the user data script content.
#cloud-config bootcmd: - echo 192.168.1.130 us.archive.ubuntu.com >> /etc/hosts
After the BMS is created, start it and run the cat /etc/hosts command to check the script execution result.

User data scripts of Windows BMSs are customized by using the open-source Cloudbase-Init architecture. This architecture uses BMS metadata as the data source for initializing and automatically configuring the BMSs. The script types are compatible with open-source Cloudbase-Init. For details about Cloudbase-Init, see https://cloudbase-init.readthedocs.io/en/latest/userdata.html.
- |
Batch-Processing Program Script |
PowerShell Script |
|---|---|---|
Format |
The script must be started with rem cmd, which is the first line of the script. No space is allowed at the beginning of the first line. |
The script must be started with #ps1, which is the first line of the script. No space is allowed at the beginning of the first line. |
Constraint |
Before Base64 encoding, the size of the script, including the first line, cannot exceed 32 KB. |
Before Base64 encoding, the size of the script, including the first line, cannot exceed 32 KB. |
This section describes how to inject scripts in different formats into Windows BMSs and view script execution results.
Example 1: Inject a batch-processing program script.
When creating a BMS, set User Data to As Text and enter the user data script content.
rem cmd echo "Hello, BAT Test" > C:\1111.txt
After the BMS is created, start it and check the script execution result. In this example, a text file named 1111 is added to disk C:\.

To view the user data injected into the Windows BMS, log in at http://169.254.169.254/openstack/latest/user_data.

Example 2: Inject a PowerShell script.
When creating a BMS, set User Data to As Text and enter the user data script content.
#ps1 echo "Hello, Powershell Test" > C:\aaaa.txt
After the BMS is created, start it and check the script execution result. In this example, a text file named aaaa is added to disk C:\.

To view the user data injected into the Windows BMS, log in at http://169.254.169.254/openstack/latest/user_data.

This case illustrates how to inject user data so as to simplify BMS configuration.
In this example, vim is configured to enable syntax highlighting, display line numbers, and set the tab stop to 4. Configuration file .vimrc is created and injected into the /root/.vimrc directory during BMS creation. After the BMS is created, vim is automatically configured based on your requirements. This helps to improve BMS configuration efficiency, especially when you are creating ECSs in a batch.
The script is as follows:
#cloud-config
write_files:
- path: /root/.vimrc
content: |
syntax on
set tabstop=4
set number
This case illustrates how to inject user data so as to reset the password for logging in to a Linux BMS.
In this example, the password of user root will be reset to "******".
Parameter |
Requirements |
Example Value |
|---|---|---|
Password |
|
Test12$@ |
The script is as follows (retain the indentation in the following script):
#cloud-config
chpasswd:
list: |
root:******
expire: False
After the BMS is created, you can use the new password to log in to it. To ensure system security, change the password of user root after logging in to the BMS for the first time.
This case illustrates how to inject user data so as to create a user on a Windows BMS and set a password for the user.
In this example, the user's username is abc, its password is ******, and the user is added to the administrators user group.
Parameter |
Requirements |
Example Value |
|---|---|---|
Password |
|
Test12$@ |
The script is as follows:
rem cmd net user abc ****** /add net localgroup administrators abc /add
After the BMS is created, you can use its username and password to log in to it.
This case illustrates how to inject user data so as to update system software packages for a Linux BMS and enable the HTTPd service. After the user data is injected, you can use the HTTPd service.
The script is as follows:
#!/bin/bash yum update -y service httpd start chkconfig httpd on
This case illustrates how to inject user data so as to assign the user root permission for remotely logging in to a Linux BMS. After injecting the file, you can log in to the BMS as user root in SSH key authentication mode.
The script is as follows:
#cloud-config disable_root: false runcmd: - sed -i 's/^PermitRootLogin.*$/PermitRootLogin without-password/' /etc/ssh/sshd_config - sed -i '/^KexAlgorithms.*$/d' /etc/ssh/sshd_config - service sshd restart