Day 12: Infrastructure as Code
Goal: Describe, create, change, and tear down the notes VM no longer via the CLI, but with OpenTofu and the OpenNebula provider. · Duration: approx. 60 min · Prerequisite: Day 10
Briefing
- OpenTofu is the open-source fork of Terraform, with
the same language (HCL) and the same providers. Everything in this
chapter works the same way with
terraform. - The provider
OpenNebula/opennebulatranslates resources such asopennebula_virtual_machineinto calls of the XML-RPC API (Day 13). - The workflow is always the same:
initdownloads the provider,planshows what would happen,applycarries it out,destroytears it down. OpenTofu remembers what exists in the state (terraform.tfstate).
main.tf ── tofu plan/apply ──► Provider ── XML-RPC :2633 ──► oned
│ └─► VM notes-tf
└─ terraform.tfstate (was gehört zu mir?)
Drill 1: Install OpenTofu
cd /tmp
curl -fsSLO https://github.com/opentofu/opentofu/releases/download/v1.13.9/tofu_1.13.9_linux_amd64.tar.gz
mkdir -p ~/.local/bin
tar xzf tofu_1.13.9_linux_amd64.tar.gz -C ~/.local/bin tofu
rm tofu_1.13.9_linux_amd64.tar.gzUbuntu adds ~/.local/bin to the PATH at
your next login. Log out and back in once, then:
tofu versionExpected:
OpenTofu v1.13.9
on linux_amd64
Save your progress? Sign up for free
Drill 2: The description
mkdir -p ~/one-lab/tag12
cd ~/one-lab/tag12
cat > main.tf <<'EOF'
terraform {
required_providers {
opennebula = {
source = "OpenNebula/opennebula"
version = "1.5.0"
}
}
}
provider "opennebula" {
endpoint = "http://localhost:2633/RPC2"
username = "oneadmin"
password = var.one_password
}
variable "one_password" {
type = string
sensitive = true
}
variable "db_password" {
type = string
sensitive = true
}
variable "api_key" {
type = string
sensitive = true
}
data "opennebula_image" "basis" {
name = "notes-basis"
}
data "opennebula_virtual_network" "vnet" {
name = "vnet"
}
data "opennebula_virtual_network" "intern" {
name = "intern"
}
resource "opennebula_virtual_machine" "notes" {
name = "notes-tf"
cpu = 0.5
vcpu = 1
memory = 384
disk {
image_id = data.opennebula_image.basis.id
}
nic {
network_id = data.opennebula_virtual_network.vnet.id
}
nic {
network_id = data.opennebula_virtual_network.intern.id
}
context = {
NETWORK = "YES"
SSH_PUBLIC_KEY = "$USER[SSH_PUBLIC_KEY]"
SET_HOSTNAME = "$NAME"
DB_HOST = "10.10.10.20"
DB_PASSWORD = var.db_password
API_KEY = var.api_key
NOTES_VERSION = "0.7.1"
START_SCRIPT_BASE64 = filebase64("${path.module}/../tag10/notes-schnell.sh")
}
}
output "ip" {
value = opennebula_virtual_machine.notes.nic[0].computed_ip
}
EOFdatablocks read what already exists: the image from Day 10 and the two networks. You do not need to know their IDs.- The startup script from Day 10 comes straight from the file via
filebase64(). No moresed, no moreSKRIPTplaceholder. sensitive = trueprevents OpenTofu from showing the values in output.
The values for the variables are in a separate file that OpenTofu reads on its own:
cat > lab.auto.tfvars <<'EOF'
one_password = "lab-one-pass-123"
db_password = "lab-db-pass-123"
api_key = "lab-api-key-123"
EOFSave your progress? Sign up for free
Drill 3: Init, plan, apply
tofu initExpected (shortened):
Initializing provider plugins...
- Finding opennebula/opennebula versions matching "1.5.0"...
- Installing opennebula/opennebula v1.5.0...
...
OpenTofu has been successfully initialized!
tofu plan | grep -E 'will be|Plan:'Expected:
# opennebula_virtual_machine.notes will be created
Plan: 1 to add, 0 to change, 0 to destroy.
tofu apply -auto-approve | tail -4Expected (the IP will differ):
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
Outputs:
ip = "192.168.150.43"
The OneFlow service’s database is still running, so the new VM connects to it:
IP=$(tofu output -raw ip)
until curl -s -m2 $IP:8080/version; do sleep 3; done; echo
curl -s $IP:8080/notes; echoExpected (shortened):
0.7.1
[{"id":"602722bb-05ab-4559-ac6e-47609ba9ab2b","text":"Hallo OpenNebula"}]
Save your progress? Sign up for free
Drill 4: Change
More memory, in code:
sed -i 's/memory = 384/memory = 512/' main.tf
tofu plan | grep -E 'will be|memory|Plan:'Expected:
# opennebula_virtual_machine.notes will be updated in-place
~ memory = 384 -> 512
Plan: 0 to add, 1 to change, 0 to destroy.
updated in-place: the VM stays the same. A
must be replaced at this point would mean delete and
recreate, and you would see that before the apply.
tofu apply -auto-approve | grep -E 'Modif|complete'
onevm list | grep notes-tfExpected:
opennebula_virtual_machine.notes: Modifying... [id=40]
opennebula_virtual_machine.notes: Modifications complete after 43s [id=40]
Apply complete! Resources: 0 added, 1 changed, 0 destroyed.
40 oneadmin oneadmin notes-tf runn 0.5 512M localhost 0d 00h02
43 seconds: the provider powered the VM off, resized it, and started it again, the same steps as on Day 7, just automated.
Save your progress? Sign up for free
Checkpoint
tofu state listExpected:
data.opennebula_image.basis
data.opennebula_virtual_network.intern
data.opennebula_virtual_network.vnet
opennebula_virtual_machine.notes
diff ~/one-lab/tag12/main.tf ~/one-lab/code/loesung/tag12/main.tfExpected: no output.
Challenge
What happens if someone deletes the VM notes-tf via the
CLI, bypassing OpenTofu, and how do you get it back? Afterwards, tear
down everything OpenTofu created.
Solution
onevm terminate --hard notes-tfHalf a minute later:
tofu plan | grep -E 'will be|Plan:'Expected:
# opennebula_virtual_machine.notes has been deleted
# opennebula_virtual_machine.notes will be created
Plan: 1 to add, 0 to change, 0 to destroy.
OpenTofu compares the state with reality, notices the missing VM, and
recreates it with apply. Infrastructure as code does not
repair itself this way, but every plan shows the drift.
Tear down:
tofu apply -auto-approve | grep complete
tofu destroy -auto-approve | tail -1Expected (shortened):
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
Destroy complete! Resources: 1 destroyed.
destroy only deletes what is in the state. The image
notes-basis and the networks were only read
(data) and remain.
Save your progress? Sign up for free
Enterprise Note
- State does not belong on the laptop: In a team, the state lives in a shared backend (S3, PostgreSQL, HTTP) with locking, so that two people do not change things at the same time.
- Passwords:
lab.auto.tfvarsin plaintext is for the lab. In production, values come from environment variables (TF_VAR_db_password) or a vault such as OpenBao, and the provider logs in with a login token. - What belongs in code: Templates, networks, security groups, groups, and quotas can also be described with the provider. That makes the entire cloud configuration from Day 3 to 8 versionable and reproducible.