-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
83 lines (68 loc) · 2.43 KB
/
main.tf
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
locals {
expanded_list_of_vms = flatten([
for index, vm in var.lab_vms : [
for num in range(vm.quantity) : {
vm = vm
# derived_name - if quantity is 1, don't append a number. Just use the default name. Example: "customname"
# if quantity is not 1, then append a number. example "customname-3"
derived_name = vm.quantity == 1 ? vm.name : join("-", [vm.name, num + 1])
# meta_data - See above comment on clarification on how we append numberrs
meta_data = {
"instance-id" : vm.quantity == 1 ? vm.meta_data.instance-id : join("-", [vm.meta_data.instance-id, num + 1])
"local-hostname" : vm.quantity == 1 ? vm.meta_data.local-hostname : join("-", [vm.meta_data.local-hostname, num + 1])
}
network_config = vm.network_configs[num]
}
]
])
loop_vms = {
for index, vm in local.expanded_list_of_vms :
vm.derived_name => vm
}
}
resource "libvirt_pool" "lab_cluster" {
name = var.libvirt_pool_name
type = "dir"
path = var.libvirt_pool_dir
}
resource "libvirt_network" "lab_network" {
name = var.libvirt_network_name
mode = "bridge"
bridge = var.bridge_device
}
resource "libvirt_volume" "cloud_image" {
name = "cloud_image.qcow2"
pool = libvirt_pool.lab_cluster.name
source = var.cloud_image
format = "qcow2"
}
resource "libvirt_volume" "lab_volume" {
for_each = local.loop_vms
name = "${each.value.derived_name}.qcow2"
pool = libvirt_pool.lab_cluster.name
base_volume_id = libvirt_volume.cloud_image.id
base_volume_pool = libvirt_pool.lab_cluster.name
format = "qcow2"
}
resource "libvirt_cloudinit_disk" "cloud_init" {
for_each = local.loop_vms
name = "${each.value.derived_name}.iso"
meta_data = yamlencode(each.value.meta_data)
user_data = join("\n", ["#cloud-config", yamlencode(each.value.vm.user_data)])
network_config = yamlencode(each.value.network_config)
pool = libvirt_pool.lab_cluster.name
}
resource "libvirt_domain" "lab_vms" {
for_each = local.loop_vms
name = each.value.derived_name
vcpu = each.value.vm.vcpu
memory = each.value.vm.ram
disk {
volume_id = libvirt_volume.lab_volume[each.value.derived_name].id
}
network_interface {
network_id = libvirt_network.lab_network.id
hostname = each.value.derived_name
}
cloudinit = libvirt_cloudinit_disk.cloud_init[each.value.derived_name].id
}