-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.tf
More file actions
168 lines (155 loc) · 3.54 KB
/
main.tf
File metadata and controls
168 lines (155 loc) · 3.54 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
resource "libvirt_volume" "boot" {
name = "${var.name}-${substr(sha256(var.boot_image_url), 0, 8)}.qcow2"
pool = var.storage_pool
capacity = var.boot_disk_size
target = {
format = {
type = "qcow2"
}
}
create = {
content = {
url = var.boot_image_url
}
}
}
resource "libvirt_volume" "extra" {
count = length(var.extra_volumes)
name = var.extra_volumes[count.index].name
pool = var.storage_pool
capacity = var.extra_volumes[count.index].size
}
resource "libvirt_cloudinit_disk" "commoninit" {
name = "${var.name}_commoninit"
meta_data = templatefile(var.cloudinit_meta_data_template, var.cloudinit_meta_data_vars)
user_data = templatefile(var.cloudinit_user_data_template, var.cloudinit_user_data_vars)
network_config = templatefile(var.cloudinit_network_config_template, var.cloudinit_network_config_vars)
}
resource "libvirt_volume" "cloudinit" {
name = "${var.name}_cloudinit.iso"
pool = var.storage_pool
create = {
content = {
url = libvirt_cloudinit_disk.commoninit.path
}
}
}
resource "libvirt_domain" "vm" {
name = var.name
type = "kvm"
description = var.description
vcpu = var.vcpu
memory = var.memory
memory_unit = "MiB"
running = true
cpu = {
mode = "host-passthrough"
}
os = {
type = "hvm"
type_arch = "x86_64"
type_machine = "q35"
boot_devices = [{ dev = "hd" }]
}
devices = {
# Disk ordering: boot disk, extra volumes, then cdrom LAST
# libvirt returns disks sorted by bus type (virtio before sata/ide)
# so we must define them in that order to avoid provider inconsistency errors
disks = concat(
[
{
driver = {
name = "qemu"
type = "qcow2"
}
source = {
volume = {
pool = var.storage_pool
volume = libvirt_volume.boot.name
}
}
target = {
dev = "vda"
bus = "virtio"
}
}
],
[
for idx, vol in libvirt_volume.extra : {
source = {
volume = {
pool = var.storage_pool
volume = vol.name
}
}
target = {
dev = "vd${substr("bcdefghij", idx, 1)}"
bus = "virtio"
}
}
],
[
{
source = {
volume = {
pool = var.storage_pool
volume = libvirt_volume.cloudinit.name
}
}
target = {
dev = "sda"
bus = "sata"
}
device = "cdrom"
}
]
)
interfaces = [
{
model = {
type = "virtio"
}
source = {
network = {
network = "default"
}
}
},
{
model = {
type = "virtio"
}
source = {
bridge = {
bridge = var.bridge_name
}
}
}
]
graphics = [
{
vnc = {
auto_port = true
listen = "0.0.0.0"
listeners = [
{
address = {
address = "0.0.0.0"
}
}
]
}
}
]
}
lifecycle {
# Force replacement if the boot volume changes
replace_triggered_by = [
libvirt_volume.boot
]
# Ignore graphics changes due to similar provider bugs.
ignore_changes = [
devices.graphics,
]
}
}