-
Notifications
You must be signed in to change notification settings - Fork 175
/
main.tf
279 lines (256 loc) · 13.2 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
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
data "vsphere_datacenter" "dc" {
name = var.dc
}
data "vsphere_datastore_cluster" "datastore_cluster" {
count = var.datastore_cluster != "" ? 1 : 0
name = var.datastore_cluster
datacenter_id = data.vsphere_datacenter.dc.id
}
data "vsphere_datastore" "datastore" {
count = var.datastore != "" && var.datastore_cluster == "" ? 1 : 0
name = var.datastore
datacenter_id = data.vsphere_datacenter.dc.id
}
data "vsphere_datastore" "disk_datastore" {
count = var.disk_datastore != "" ? 1 : 0
name = var.disk_datastore
datacenter_id = data.vsphere_datacenter.dc.id
}
data "vsphere_resource_pool" "pool" {
count = var.vmrp != "" ? 1 : 0
name = var.vmrp
datacenter_id = data.vsphere_datacenter.dc.id
}
data "vsphere_network" "network" {
count = length(var.network)
name = var.network_delimiter != null ? split(var.network_delimiter,keys(var.network)[count.index])[1] : keys(var.network)[count.index]
datacenter_id = data.vsphere_datacenter.dc.id
}
data "vsphere_virtual_machine" "template" {
count = var.content_library == null ? 1 : 0
name = var.vmtemp
datacenter_id = data.vsphere_datacenter.dc.id
}
data "vsphere_content_library" "library" {
count = var.content_library != null ? 1 : 0
name = var.content_library
depends_on = [var.tag_depends_on]
}
data "vsphere_content_library_item" "library_item_template" {
count = var.content_library != null ? 1 : 0
library_id = data.vsphere_content_library.library[0].id
type = "ovf"
name = var.vmtemp
depends_on = [var.tag_depends_on]
}
data "vsphere_tag_category" "category" {
count = var.tags != null ? length(var.tags) : 0
name = keys(var.tags)[count.index]
depends_on = [var.tag_depends_on]
}
data "vsphere_tag" "tag" {
count = var.tags != null ? length(var.tags) : 0
name = var.tags[keys(var.tags)[count.index]]
category_id = data.vsphere_tag_category.category[count.index].id
depends_on = [var.tag_depends_on]
}
data "vsphere_folder" "folder" {
count = var.vmfolder != null ? 1 : 0
path = "/${data.vsphere_datacenter.dc.name}/vm/${var.vmfolder}"
depends_on = [var.vm_depends_on]
}
locals {
interface_count = length(var.ipv4submask) #Used for Subnet handeling
template_disk_count = var.content_library == null ? length(data.vsphere_virtual_machine.template[0].disks) : 0
}
// Cloning a Linux or Windows VM from a given template.
resource "vsphere_virtual_machine" "vm" {
count = var.instances
depends_on = [var.vm_depends_on]
name = "${var.staticvmname != null ? var.staticvmname : format("${var.vmname}${var.vmnameformat}", count.index + var.vmstartcount)}${var.fqdnvmname == true ? ".${var.domain}" : ""}"
resource_pool_id = var.vmrp != "" ? data.vsphere_resource_pool.pool[0].id : var.vmrpid
folder = var.vmfolder
tags = var.tag_ids != null ? var.tag_ids : data.vsphere_tag.tag[*].id
custom_attributes = var.custom_attributes
annotation = var.annotation
extra_config = var.extra_config
firmware = var.content_library == null && var.firmware == null ? data.vsphere_virtual_machine.template[0].firmware : var.firmware
efi_secure_boot_enabled = var.content_library == null && var.efi_secure_boot == null ? data.vsphere_virtual_machine.template[0].efi_secure_boot_enabled : var.efi_secure_boot
enable_disk_uuid = var.content_library == null && var.enable_disk_uuid == null ? data.vsphere_virtual_machine.template[0].enable_disk_uuid : var.enable_disk_uuid
storage_policy_id = var.storage_policy_id
datastore_cluster_id = var.datastore_cluster != "" ? data.vsphere_datastore_cluster.datastore_cluster[0].id : null
datastore_id = var.datastore != "" ? data.vsphere_datastore.datastore[0].id : null
num_cpus = var.cpu_number
num_cores_per_socket = var.num_cores_per_socket
cpu_hot_add_enabled = var.cpu_hot_add_enabled
cpu_hot_remove_enabled = var.cpu_hot_remove_enabled
cpu_reservation = var.cpu_reservation
cpu_share_level = var.cpu_share_level
cpu_share_count = var.cpu_share_level == "custom" ? var.cpu_share_count : null
memory_reservation = var.memory_reservation
memory = var.ram_size
memory_hot_add_enabled = var.memory_hot_add_enabled
memory_share_level = var.memory_share_level
memory_share_count = var.memory_share_level == "custom" ? var.memory_share_count : null
guest_id = var.content_library == null ? data.vsphere_virtual_machine.template[0].guest_id : null
scsi_bus_sharing = var.scsi_bus_sharing
scsi_type = var.scsi_type != "" ? var.scsi_type : (var.content_library == null ? data.vsphere_virtual_machine.template[0].scsi_type : null)
scsi_controller_count = max(
max(0, flatten([
for item in values(var.data_disk) : [
for elem, val in item :
elem == "data_disk_scsi_controller" ? val : 0
]])...) + 1,
ceil((max(0, flatten([
for item in values(var.data_disk) : [
for elem, val in item :
elem == "unit_number" ? val : 0
]])...) + 1) / 15),
var.scsi_controller)
wait_for_guest_net_routable = var.wait_for_guest_net_routable
wait_for_guest_ip_timeout = var.wait_for_guest_ip_timeout
wait_for_guest_net_timeout = var.wait_for_guest_net_timeout
ignored_guest_ips = var.ignored_guest_ips
dynamic "network_interface" {
for_each = keys(var.network) #data.vsphere_network.network[*].id #other option
content {
network_id = data.vsphere_network.network[network_interface.key].id
adapter_type = var.network_type != null ? var.network_type[network_interface.key] : (var.content_library == null ? data.vsphere_virtual_machine.template[0].network_interface_types[0] : null)
}
}
// Disks defined in the original template
dynamic "disk" {
for_each = var.content_library == null ? data.vsphere_virtual_machine.template[0].disks : []
iterator = template_disks
content {
label = length(var.disk_label) > 0 ? var.disk_label[template_disks.key] : "disk${template_disks.key}"
size = var.disk_size_gb != null ? var.disk_size_gb[template_disks.key] : data.vsphere_virtual_machine.template[0].disks[template_disks.key].size
unit_number = var.scsi_controller != null ? var.scsi_controller * 15 + template_disks.key : template_disks.key
thin_provisioned = data.vsphere_virtual_machine.template[0].disks[template_disks.key].thin_provisioned
eagerly_scrub = data.vsphere_virtual_machine.template[0].disks[template_disks.key].eagerly_scrub
datastore_id = var.disk_datastore != "" ? data.vsphere_datastore.disk_datastore[0].id : null
storage_policy_id = length(var.template_storage_policy_id) > 0 ? var.template_storage_policy_id[template_disks.key] : null
io_reservation = length(var.io_reservation) > 0 ? var.io_reservation[template_disks.key] : null
io_share_level = length(var.io_share_level) > 0 ? var.io_share_level[template_disks.key] : "normal"
io_share_count = length(var.io_share_level) > 0 && var.io_share_level[template_disks.key] == "custom" ? var.io_share_count[template_disks.key] : null
}
}
// Disk for template from Content Library
dynamic "disk" {
for_each = var.content_library == null ? [] : [1]
iterator = template_disks
content {
label = length(var.disk_label) > 0 ? var.disk_label[template_disks.key] : "disk${template_disks.key}"
size = var.disk_size_gb[template_disks.key]
unit_number = var.scsi_controller != null ? var.scsi_controller * 15 + template_disks.key : template_disks.key
// thin_provisioned = data.vsphere_virtual_machine.template[0].disks[template_disks.key].thin_provisioned
// eagerly_scrub = data.vsphere_virtual_machine.template[0].disks[template_disks.key].eagerly_scrub
datastore_id = var.disk_datastore != "" ? data.vsphere_datastore.disk_datastore[0].id : null
storage_policy_id = length(var.template_storage_policy_id) > 0 ? var.template_storage_policy_id[template_disks.key] : null
io_reservation = length(var.io_reservation) > 0 ? var.io_reservation[template_disks.key] : null
io_share_level = length(var.io_share_level) > 0 ? var.io_share_level[template_disks.key] : "normal"
io_share_count = length(var.io_share_level) > 0 && var.io_share_level[template_disks.key] == "custom" ? var.io_share_count[template_disks.key] : null
disk_mode = length(var.disk_mode) > 0 ? var.disk_mode[template_disks.key] : null
}
}
// Additional disks defined by Terraform config
dynamic "disk" {
for_each = var.data_disk
iterator = terraform_disks
content {
label = terraform_disks.key
size = lookup(terraform_disks.value, "size_gb", null)
unit_number = (
lookup(
terraform_disks.value,
"unit_number",
-1
) < 0 ? (
lookup(
terraform_disks.value,
"data_disk_scsi_controller",
0
) > 0 ? (
(terraform_disks.value.data_disk_scsi_controller * 15) +
index(keys(var.data_disk), terraform_disks.key) +
(var.scsi_controller == tonumber(terraform_disks.value["data_disk_scsi_controller"]) ? local.template_disk_count : 0)
) : (
index(keys(var.data_disk), terraform_disks.key) + local.template_disk_count
)
) : (
tonumber(terraform_disks.value["unit_number"])
)
)
thin_provisioned = lookup(terraform_disks.value, "thin_provisioned", "true")
eagerly_scrub = lookup(terraform_disks.value, "eagerly_scrub", "false")
datastore_id = lookup(terraform_disks.value, "datastore_id", null)
storage_policy_id = lookup(terraform_disks.value, "storage_policy_id", null)
io_reservation = lookup(terraform_disks.value, "io_reservation", null)
io_share_level = lookup(terraform_disks.value, "io_share_level", "normal")
io_share_count = lookup(terraform_disks.value, "io_share_level", null) == "custom" ? lookup(terraform_disks.value, "io_share_count") : null
disk_mode = lookup(terraform_disks.value, "disk_mode", null)
disk_sharing = lookup(terraform_disks.value, "disk_sharing", null)
attach = lookup(terraform_disks.value, "attach", null)
path = lookup(terraform_disks.value, "path", null)
}
}
clone {
template_uuid = var.content_library == null ? data.vsphere_virtual_machine.template[0].id : data.vsphere_content_library_item.library_item_template[0].id
linked_clone = var.linked_clone
timeout = var.timeout
customize {
dynamic "linux_options" {
for_each = var.is_windows_image ? [] : [1]
content {
host_name = var.staticvmname != null ? var.staticvmname : format("${var.vmname}${var.vmnameformat}", count.index + var.vmstartcount)
domain = var.domain
hw_clock_utc = var.hw_clock_utc
}
}
dynamic "windows_options" {
for_each = var.is_windows_image ? [1] : []
content {
computer_name = var.staticvmname != null ? var.staticvmname : format("${var.vmname}${var.vmnameformat}", count.index + var.vmstartcount)
admin_password = var.local_adminpass
workgroup = var.workgroup
join_domain = var.windomain
domain_admin_user = var.domain_admin_user
domain_admin_password = var.domain_admin_password
organization_name = var.orgname
run_once_command_list = var.run_once
auto_logon = var.auto_logon
auto_logon_count = var.auto_logon_count
time_zone = var.time_zone
product_key = var.productkey
full_name = var.full_name
}
}
dynamic "network_interface" {
for_each = keys(var.network)
content {
ipv4_address = split("/", var.network[keys(var.network)[network_interface.key]][count.index])[0]
ipv4_netmask = var.network[keys(var.network)[network_interface.key]][count.index] == "" ? null : (
length(split("/", var.network[keys(var.network)[network_interface.key]][count.index])) == 2 ? (
split("/", var.network[keys(var.network)[network_interface.key]][count.index])[1]
) : (
length(var.ipv4submask) == 1 ? var.ipv4submask[0] : var.ipv4submask[network_interface.key]
)
)
}
}
dns_server_list = var.dns_server_list
dns_suffix_list = var.dns_suffix_list
ipv4_gateway = var.vmgateway
}
}
// Advanced options
hv_mode = var.hv_mode
ept_rvi_mode = var.ept_rvi_mode
nested_hv_enabled = var.nested_hv_enabled
enable_logging = var.enable_logging
cpu_performance_counters_enabled = var.cpu_performance_counters_enabled
swap_placement_policy = var.swap_placement_policy
latency_sensitivity = var.latency_sensitivity
shutdown_wait_timeout = var.shutdown_wait_timeout
force_power_off = var.force_power_off
}