Skip to content
Hands-on IT IT Drills

Day 4: Templates and Contextualization

Goal: Write your own template that provides every VM with a hostname, key, network, and startup script, and ask for values at start. · Duration: approx. 60 min · Prerequisite: Day 3

Briefing

  • A template is a text file in OpenNebula’s own syntax: simple attributes (MEMORY = "256") and vectors in square brackets (DISK = [ … ]).
  • Contextualization means: at start, the VM gets a small CD drive with data about itself, such as IP address, SSH key, hostname, and a startup script. The one-context package inside the VM reads it and sets everything up. The Alpine from the Marketplace already includes it.
  • Values in the template can contain variables: $NAME is the VM’s name, $USER[SSH_PUBLIC_KEY] is the user’s key, $GRUSS is a custom input.
  • USER_INPUTS ask for values at instantiation, as a form in Sunstone, via --user-inputs in the CLI.
 Template alpine ── instantiate ──► VM
   CONTEXT = [ SET_HOSTNAME = "$NAME", … ]        CD „CONTEXT“ (sr0)
                                                    └─ one-context in der VM:
                                                       Hostname, Netz, Schlüssel, Skript

Drill 1: The template

mkdir -p ~/one-lab/tag04
cd ~/one-lab/tag04
cat > alpine.tpl <<'EOF'
NAME = "alpine"
CPU = "0.5"
VCPU = "1"
MEMORY = "256"
DISK = [ IMAGE = "Alpine Linux 3.20", IMAGE_UNAME = "oneadmin" ]
NIC = [ NETWORK = "vnet", NETWORK_UNAME = "oneadmin" ]
GRAPHICS = [ TYPE = "vnc", LISTEN = "0.0.0.0" ]
USER_INPUTS = [ GRUSS = "O|text|Begrüßung beim Anmelden||Hallo aus OpenNebula" ]
CONTEXT = [
  NETWORK = "YES",
  SSH_PUBLIC_KEY = "$USER[SSH_PUBLIC_KEY]",
  SET_HOSTNAME = "$NAME",
  GRUSS = "$GRUSS",
  START_SCRIPT = "echo \"$GRUSS\" > /etc/motd" ]
EOF
onetemplate create alpine.tpl

Expected:

ID: 2
Line Meaning
CPU = "0.5", VCPU = "1" the VM sees one core and is charged half a core by the scheduler
DISK, NIC image and network by name, *_UNAME says who owns them
USER_INPUTS O optional, text type, then description, empty range, default value
NETWORK = "YES" one-context sets up IP, gateway, and DNS from OpenNebula
SET_HOSTNAME hostname of the VM, here its name in OpenNebula
START_SCRIPT a script that one-context runs as root at start

The single quotes around 'EOF' are important: the shell should not replace $NAME and $GRUSS itself; OpenNebula does that.

Save your progress? Sign up for free

Drill 2: Start with input

onetemplate instantiate alpine --name kontext --user-inputs GRUSS="Servus vom Trainingslager"

Expected (the ID will differ):

VM ID: 8

Without --user-inputs, the CLI asks interactively for each value. For scripts, you therefore always pass them.

After a minute:

IP=$(onevm show kontext | grep -o 'ETH0_IP="[0-9.]*' | cut -d'"' -f2)
ssh root@$IP "hostname; cat /etc/motd"

Expected (shortened):

kontext
Servus vom Trainingslager

The hostname comes from SET_HOSTNAME, the greeting from the startup script. On Day 1, the VM still called itself localhost.

Save your progress? Sign up for free

Drill 3: What the VM receives

OpenNebula replaces all variables before writing the context to the CD:

onevm show kontext | sed -n '/^CONTEXT=/,/]/p' | grep -E 'ETH0_IP=|ETH0_GATEWAY|GRUSS|SET_HOSTNAME|START_SCRIPT'

Expected:

  ETH0_GATEWAY="192.168.150.1",
  ETH0_IP="192.168.150.11",
  GRUSS="Servus vom Trainingslager",
  SET_HOSTNAME="kontext",
  START_SCRIPT="echo \"Servus vom Trainingslager\" > /etc/motd",

OpenNebula added the ETH0_* values from the virtual network because NETWORK = "YES" is set. Inside the VM, this is available as a CD and as a file:

ssh root@$IP "blkid | grep CONTEXT; grep -E 'GRUSS|SET_HOSTNAME' /run/one-context/one_env"

Expected:

/dev/sr0: BLOCK_SIZE="2048" UUID="2026-11-12-19-12-02-00" LABEL="CONTEXT" TYPE="iso9660"
export GRUSS="Servus vom Trainingslager"
export SET_HOSTNAME="kontext"

/run/one-context/one_env is meant for your own scripts: . /run/one-context/one_env, and all context values are available as environment variables. Day 6 uses this.

Save your progress? Sign up for free

Checkpoint

onetemplate list | grep alpine

Expected: a line alpine.

diff ~/one-lab/tag04/alpine.tpl ~/one-lab/code/loesung/tag04/alpine.tpl

Expected: no output.

Challenge

A startup script on a single line quickly becomes unreadable. Create a second template alpine-start that runs a multi-line script from a file: install htop and write “fertig” to /root/start.log.

Note: START_SCRIPT_BASE64 takes a script in Base64. That way, neither quotes nor line breaks get in the way.

Solution

cat > start.sh <<'EOF'
#!/bin/sh
apk add -q htop
echo fertig > /root/start.log
EOF
sed -e 's/^NAME = "alpine"/NAME = "alpine-start"/' \
  -e 's|  START_SCRIPT = .*|  START_SCRIPT_BASE64 = "SKRIPT" ]|' alpine.tpl > alpine-start.tpl
sed -i "s|SKRIPT|$(base64 -w0 start.sh)|" alpine-start.tpl
tail -1 alpine-start.tpl

Expected:

  START_SCRIPT_BASE64 = "IyEvYmluL3NoCmFwayBhZGQgLXEgaHRvcAplY2hvIGZlcnRpZyA+IC9yb290L3N0YXJ0LmxvZwo=" ]
onetemplate create alpine-start.tpl
onetemplate instantiate alpine-start --name start --user-inputs GRUSS=Hallo

After a little over a minute:

IP=$(onevm show start | grep -o 'ETH0_IP="[0-9.]*' | cut -d'"' -f2)
ssh root@$IP "cat /root/start.log; which htop"

Expected:

fertig
/usr/bin/htop

The package comes from the internet. If it does not work, check the DNS fix from Day 1.

Clean up:

onevm terminate --hard kontext
onevm terminate --hard start

Save your progress? Sign up for free

Enterprise Note

  • cloud-init instead of one-context: Ready-made cloud images from Ubuntu or Debian come with cloud-init. OpenNebula speaks that too, via CONTEXT=[ USER_DATA = … ]. If you use images from the Marketplace, one-context is already on board.
  • Version templates: Templates are text files. They belong in Git, and a pipeline applies them with onetemplate update. It is even more convenient with Terraform (Day 12).
  • Secrets in the context: Anything in the context can be read by anyone who can see the VM, and inside the VM by any process that reads the CD drive. Passwords only belong there if they are short-lived. It is better for the VM to fetch them at start from a vault such as OpenBao.