-
Notifications
You must be signed in to change notification settings - Fork 508
Expand file tree
/
Copy pathtwo_vms_with_cloudinit.tf
More file actions
390 lines (335 loc) · 7.69 KB
/
two_vms_with_cloudinit.tf
File metadata and controls
390 lines (335 loc) · 7.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
# Example: Two Virtual Machines with Cloud-Init Configuration
#
# This example demonstrates provisioning two Ubuntu VMs with:
# - Automatic IP assignment via DHCP on the default network
# - Root password configured via cloud-init
# - SSH server enabled and accessible
# - Using Ubuntu 22.04 LTS cloud image
terraform {
required_providers {
libvirt = {
source = "dmacvicar/libvirt"
}
}
}
provider "libvirt" {
uri = "qemu:///system"
}
# Download Ubuntu 22.04 (Jammy) cloud image
# Ubuntu cloud images have excellent cloud-init support
resource "libvirt_volume" "ubuntu_base" {
name = "ubuntu-jammy-base.qcow2"
pool = "default"
target = {
format = {
type = "qcow2"
}
}
create = {
content = {
# Ubuntu 22.04 LTS (Jammy Jellyfish) cloud image
url = "https://cloud-images.ubuntu.com/jammy/current/jammy-server-cloudimg-amd64.img"
}
}
}
# Create boot disk for VM1 (uses base image as backing store)
resource "libvirt_volume" "vm1_disk" {
name = "vm1-disk.qcow2"
pool = "default"
target = {
format = {
type = "qcow2"
}
}
# Start with 2GB, will grow as needed
capacity = 2147483648 # 2GB in bytes
backing_store = {
path = libvirt_volume.ubuntu_base.path
format = {
type = "qcow2"
}
}
}
# Download Alpine Linux 3.22 cloud image
resource "libvirt_volume" "alpine_base" {
name = "alpine-3.22-base.qcow2"
pool = "default"
target = {
format = {
type = "qcow2"
}
}
create = {
content = {
# Alpine Linux 3.22 generic cloud image (BIOS, cloudinit)
url = "https://dl-cdn.alpinelinux.org/alpine/v3.22/releases/cloud/generic_alpine-3.22.2-x86_64-bios-cloudinit-r0.qcow2"
}
}
}
# Create boot disk for VM2 (uses Alpine base image as backing store)
resource "libvirt_volume" "vm2_disk" {
name = "vm2-disk.qcow2"
pool = "default"
target = {
format = {
type = "qcow2"
}
}
capacity = 2147483648 # 2GB in bytes
backing_store = {
path = libvirt_volume.alpine_base.path
format = {
type = "qcow2"
}
}
}
# Cloud-init configuration for VM1
resource "libvirt_cloudinit_disk" "vm1_init" {
name = "vm1-cloudinit"
# User-data: Configure root password, enable SSH, install packages
user_data = <<-EOF
#cloud-config
# Set root password to "password" (change this!)
chpasswd:
list: |
root:password
expire: false
# Enable SSH password authentication
ssh_pwauth: true
# Install and enable SSH server
packages:
- openssh-server
# Optional: Add SSH public key for key-based auth (more secure)
# ssh_authorized_keys:
# - ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC0/Ho... your-key-here
# Set timezone
timezone: UTC
# Final message on console
final_message: "VM1 is ready! SSH: ssh root@<IP>"
EOF
# Meta-data: Instance identification
meta_data = <<-EOF
instance-id: vm1-001
local-hostname: ubuntu-vm1
EOF
# Network config: Use DHCP (default behavior)
network_config = <<-EOF
version: 2
ethernets:
eth0:
dhcp4: true
EOF
}
# Cloud-init configuration for VM2
resource "libvirt_cloudinit_disk" "vm2_init" {
name = "vm2-cloudinit"
user_data = <<-EOF
#cloud-config
chpasswd:
list: |
root:password
expire: false
ssh_pwauth: true
packages:
- openssh-server
timezone: UTC
final_message: "VM2 is ready! SSH: ssh root@<IP>"
EOF
meta_data = <<-EOF
instance-id: vm2-001
local-hostname: ubuntu-vm2
EOF
network_config = <<-EOF
version: 2
ethernets:
eth0:
dhcp4: true
EOF
}
# Upload cloud-init ISO for VM1 to a volume
resource "libvirt_volume" "vm1_cloudinit" {
name = "vm1-cloudinit.iso"
pool = "default"
# Format will be auto-detected as "iso"
create = {
content = {
url = libvirt_cloudinit_disk.vm1_init.path
}
}
}
# Upload cloud-init ISO for VM2 to a volume
resource "libvirt_volume" "vm2_cloudinit" {
name = "vm2-cloudinit.iso"
pool = "default"
# Format will be auto-detected as "iso"
create = {
content = {
url = libvirt_cloudinit_disk.vm2_init.path
}
}
}
# Virtual Machine 1
resource "libvirt_domain" "vm1" {
name = "ubuntu-vm1"
memory = 1048576 # 1 GB in KiB (1024 * 1024)
vcpu = 1
type = "kvm"
# Boot configuration
os = {
type = "hvm"
type_arch = "x86_64"
type_machine = "q35"
}
# Attached disks
devices = {
disks = [
# Main system disk
{
source = {
volume = {
pool = libvirt_volume.vm1_disk.pool
volume = libvirt_volume.vm1_disk.name
}
}
target = {
bus = "virtio"
dev = "vda"
}
driver = {
type = "qcow2"
}
},
# Cloud-init config disk (will be detected automatically)
{
device = "cdrom"
source = {
volume = {
pool = libvirt_volume.vm1_cloudinit.pool
volume = libvirt_volume.vm1_cloudinit.name
}
}
target = {
bus = "sata"
dev = "sda"
}
}
]
# Network interface on default network (DHCP)
interfaces = [
{
type = "network"
model = { type = "virtio" }
source = {
network = {
network = "default"
}
}
}
]
# Graphics console (VNC)
graphics = [
{
vnc = {
auto_port = true
listen = "127.0.0.1"
}
}
]
}
# Start the VM automatically
running = true
}
# Virtual Machine 2
resource "libvirt_domain" "vm2" {
name = "ubuntu-vm2"
memory = 1048576 # 1 GB in KiB (1024 * 1024)
vcpu = 1
type = "kvm"
os = {
type = "hvm"
type_arch = "x86_64"
type_machine = "q35"
}
devices = {
disks = [
{
source = {
volume = {
pool = libvirt_volume.vm2_disk.pool
volume = libvirt_volume.vm2_disk.name
}
}
target = {
bus = "virtio"
dev = "vda"
}
driver = {
type = "qcow2"
}
},
{
device = "cdrom"
source = {
volume = {
pool = libvirt_volume.vm2_cloudinit.pool
volume = libvirt_volume.vm2_cloudinit.name
}
}
target = {
bus = "sata"
dev = "sda"
}
}
]
interfaces = [
{
type = "network"
model = { type = "virtio" }
source = {
network = {
network = "default"
}
}
}
]
graphics = [
{
vnc = {
auto_port = true
listen = "127.0.0.1"
}
}
]
}
running = true
}
# Outputs to help locate the VMs
output "vm1_id" {
value = libvirt_domain.vm1.id
description = "Domain ID for VM1"
}
output "vm2_id" {
value = libvirt_domain.vm2.id
description = "Domain ID for VM2"
}
output "instructions" {
value = <<-EOF
Virtual machines have been created!
To find IP addresses assigned by DHCP:
sudo virsh domifaddr ubuntu-vm1
sudo virsh domifaddr ubuntu-vm2
Or check the DHCP leases:
sudo virsh net-dhcp-leases default
To connect via SSH (once you know the IP):
ssh root@<IP-ADDRESS>
Password: password
To view VM console:
sudo virsh console ubuntu-vm1
sudo virsh console ubuntu-vm2
To connect via VNC:
1. Find VNC port: sudo virsh domdisplay ubuntu-vm1
2. Connect with VNC client to that address
Note: It may take 30-60 seconds after boot for cloud-init to complete
and the SSH server to be available.
EOF
}