Are you interested in using Cloud-init to create a Virtual Machine on OpenStack with custom user data?. Cloud-init allows you to automate your instance configurations in OpenStack. It is widely used in Cloud platforms to automate task such as OS upgrade, setting up of users, application dependencies, or running custom scripts during the first OS boot up.
In this post we shall consider simple Cloud-Init script but the process should be similar for more complex scenarios. The prerequisites to this are:
- Privileged access to OpenStack environment – meaning you can use Networking, Storage and Computer services
- Working OpenStack Metadata Service.
- Installed, configured and functional OpenStack CLI. It should have valid OpenStack credentials.
- Internet access from the instance (Network used should have Public IP or Nat)
To test your OpenStack CLI, run:
openstack versions show
Create Cloud-init user data
Login to your OpenStack and create or import Cloud-init userdata.
vim cloud-init.yaml
The script used in this article has the following contents. You can get all cloud-init templates we have created in our Git repository.
#cloud-config
#
# Above first line indicates that the file is a Cloud-Init configuration file. Don't remove it
# Set the hostname for the instance
hostname: server1.cloudspinx.com
manage_etc_hosts: true
# Update the package list
package_update: true
# Upgrade all installed packages to their latest versions
package_upgrade: true
# List of packages to install
packages:
- bash-completion # Programmable completion for the bash shell
- git # Distributed version control system
- vim # Text editor
- wget # Network downloader
- curl # Tool to transfer data from or to a server
- python3 # Python 3 interpreter
- python3-pip # Package installer for Python 3
# Reboot after package install/update if necessary
package_reboot_if_required: true
Customize and the script and save it.
Create VM Instance on OpenStack with cloud-init data
The user data we just created will be accessed by instance through the metadata service or config drive. In our previous article we looked at how you can fix instances not able to access metadata service via 169.254.169.254.
With cloud-init data ready, we can then create a VM instance on OpenStack using openstack
CLI while passing the cloud-init.yaml
file as the instance user data.
openstack server create \
--flavor <flavor_name_or_id> \
--network <network_name_or_id> \
--key-name <keypair_name> \
--image <image_name_or_id> \
--security-group <security_group_name_or_id> \
--user-data cloud-init.yaml \
<instance_name>
See example below.
openstack server create \
--flavor linux-basic-vps \
--network public \
--key-name jkmutai \
--image Ubuntu-24.04 \
--security-group allow_all \
--user-data cloud-init.yaml \
web-server1
Printed output from our openstack command execution.
+--------------------------------------+-----------------------------------------------------+
| Field | Value |
+--------------------------------------+-----------------------------------------------------+
| OS-DCF:diskConfig | MANUAL |
| OS-EXT-AZ:availability_zone | |
| OS-EXT-SRV-ATTR:host | None |
| OS-EXT-SRV-ATTR:hypervisor_hostname | None |
| OS-EXT-SRV-ATTR:instance_name | |
| OS-EXT-STS:power_state | NOSTATE |
| OS-EXT-STS:task_state | scheduling |
| OS-EXT-STS:vm_state | building |
| OS-SRV-USG:launched_at | None |
| OS-SRV-USG:terminated_at | None |
| accessIPv4 | |
| accessIPv6 | |
| addresses | |
| adminPass | RsbaaHHf9fmc |
| config_drive | |
| created | 2024-07-03T21:11:33Z |
| flavor | linux-basic-vps (10) |
| hostId | |
| id | bef15516-dd9e-4af4-aa63-0700e7a897d1 |
| image | Ubuntu-24.04 (35ee8729-26b7-4e93-b045-71b13c574c73) |
| key_name | jkmutai |
| name | web-server1 |
| os-extended-volumes:volumes_attached | [] |
| progress | 0 |
| project_id | 3dfc8a0aec3d409696daa2c825b85a3f |
| properties | |
| security_groups | name='default' |
| status | BUILD |
| updated | 2024-07-03T21:11:33Z |
| user_id | a381926a111e43d288f927baf92e78c8 |
+--------------------------------------+-----------------------------------------------------+
See console logs as the VM instance boots up.
$ openstack console log show web-server1
.....
[[0;32m OK [0m] Finished [0;1;39msystemd-update-utmp-runle���e[0m - Record Runlevel Change in UTMP.
[ 15.323582] cloud-init[977]: Cloud-init v. 24.1.3-0ubuntu3.3 running 'modules:final' at Wed, 03 Jul 2024 21:31:33 +0000. Up 15.28 seconds.
Starting [0;1;39mapt-news.service[0m - Update APT News...
Starting [0;1;39mesm-cache.service[0m - Update the local ESM caches...
[ 15.496271] cloud-init[977]: Get:1 http://security.ubuntu.com/ubuntu noble-security InRelease [126 kB]
[ 15.701488] cloud-init[977]: Get:2 http://nova.clouds.archive.ubuntu.com/ubuntu noble InRelease [256 kB]
[ 15.765138] cloud-init[977]: Get:3 http://security.ubuntu.com/ubuntu noble-security/main amd64 Packages [189 kB]
[ 15.815936] cloud-init[977]: Get:4 http://security.ubuntu.com/ubuntu noble-security/main Translation-en [48.6 kB]
[ 15.820250] cloud-init[977]: Get:5 http://security.ubuntu.com/ubuntu noble-security/universe amd64 Packages [65.1 kB]
[ 15.824394] cloud-init[977]: Get:6 http://security.ubuntu.com/ubuntu noble-security/universe Translation-en [23.0 kB]
[ 15.826805] cloud-init[977]: Get:7 http://security.ubuntu.com/ubuntu noble-security/restricted amd64 Packages [139 kB]
[ 15.830291] cloud-init[977]: Get:8 http://security.ubuntu.com/ubuntu noble-security/restricted Translation-en [27.0 kB]
[ 15.833656] cloud-init[977]: Get:9 http://security.ubuntu.com/ubuntu noble-security/multiverse amd64 Packages [10.6 kB]
[ 15.836262] cloud-init[977]: Get:10 http://security.ubuntu.com/ubuntu noble-security/multiverse Translation-en [2808 B]
[ 16.389425] cloud-init[977]: Get:11 http://nova.clouds.archive.ubuntu.com/ubuntu noble-updates InRelease [126 kB]
[ 16.643511] cloud-init[977]: Get:12 http://nova.clouds.archive.ubuntu.com/ubuntu noble-backports InRelease [126 kB]
[ 16.909014] cloud-init[977]: Get:13 http://nova.clouds.archive.ubuntu.com/ubuntu noble-updates/main amd64 Packages [215 kB]
[ 17.097068] cloud-init[977]: Get:14 http://nova.clouds.archive.ubuntu.com/ubuntu noble-updates/main Translation-en [58.9 kB]
[ 17.117546] cloud-init[977]: Get:15 http://nova.clouds.archive.ubuntu.com/ubuntu noble-updates/universe amd64 Packages [112 kB]
[ 17.182950] cloud-init[977]: Get:16 http://nova.clouds.archive.ubuntu.com/ubuntu noble-updates/universe Translation-en [40.8 kB]
[ 17.196583] cloud-init[977]: Get:17 http://nova.clouds.archive.ubuntu.com/ubuntu noble-updates/restricted amd64 Packages [139 kB]
[ 17.244063] cloud-init[977]: Get:18 http://nova.clouds.archive.ubuntu.com/ubuntu noble-updates/restricted Translation-en [27.0 kB]
[ 17.252808] cloud-init[977]: Get:19 http://nova.clouds.archive.ubuntu.com/ubuntu noble-updates/multiverse amd64 Packages [14.1 kB]
[ 17.257035] cloud-init[977]: Get:20 http://nova.clouds.archive.ubuntu.com/ubuntu noble-updates/multiverse Translation-en [3608 B]
[ 17.261176] cloud-init[977]: Get:21 http://nova.clouds.archive.ubuntu.com/ubuntu noble-backports/universe amd64 Packages [8728 B]
[ 17.268074] cloud-init[977]: Get:22 http://nova.clouds.archive.ubuntu.com/ubuntu noble-backports/universe Translation-en [10.0 kB]
Starting [0;1;39mpackagekit.service[0m - PackageKit Daemon...
[[0;32m OK [0m] Started [0;1;39mpackagekit.service[0m - PackageKit Daemon.
Ubuntu 24.04 LTS chats ttyS0
chats login: [ 21.728190] cloud-init[977]: Fetched 1768 kB in 2s (936 kB/s)
[ 22.432018] cloud-init[977]: Reading package lists...
[ 22.464159] cloud-init[977]: Reading package lists...
[ 22.652964] cloud-init[977]: Building dependency tree...
[ 22.655095] cloud-init[977]: Reading state information...
[ 22.876018] cloud-init[977]: Calculating upgrade...
....
To list running instances and get assigned IP address use:
$ openstack server list
+--------------------------------------+-------------+--------+-----------------------+--------------+-----------------+
| ID | Name | Status | Networks | Image | Flavor |
+--------------------------------------+-------------+--------+-----------------------+--------------+-----------------+
| bef15516-dd9e-4af4-aa63-0700e7a897d1 | web-server1 | ACTIVE | public=192.168.10.231 | Ubuntu-24.04 | linux-basic-vps |
+--------------------------------------+-------------+--------+-----------------------+--------------+-----------------+
Co
$ ssh [email protected]
Welcome to Ubuntu 24.04 LTS (GNU/Linux 6.8.0-36-generic x86_64)
* Documentation: https://help.ubuntu.com
* Management: https://landscape.canonical.com
* Support: https://ubuntu.com/pro
System information as of Wed Jul 3 21:33:10 UTC 2024
System load: 0.0 Processes: 99
Usage of /: 13.6% of 18.33GB Users logged in: 0
Memory usage: 8% IPv4 address for ens3: 192.168.10.231
Swap usage: 0%
Expanded Security Maintenance for Applications is not enabled.
0 updates can be applied immediately.
Enable ESM Apps to receive additional future security updates.
See https://ubuntu.com/esm or run: sudo pro status
The programs included with the Ubuntu system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.
Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by
applicable law.
ubuntu@web-server1:~$
And our instance has been created and configured to our desired needs using --user-data <user-data-file>
parameter at instance creation. We hope this article was helpful and feel free to contact CloudSpinx for support with any OpenStack related tasks.