-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
90 lines (77 loc) · 2.21 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
data "vsphere_resource_pool" "pool" {
name = "Resources"
datacenter_id = var.datacenter
}
data "vsphere_virtual_machine" "template" {
name = var.vm_template
datacenter_id = var.datacenter
}
data "template_cloudinit_config" "config" {
gzip = false
base64_encode = true
dynamic part {
for_each = var.dhcp == true && var.user_data == "" ? [1] : []
content {
content_type = "text/x-shellscript"
content = "echo 'Empty cloud-init."
}
}
dynamic part {
for_each = var.dhcp != true ? [1] : []
content {
filename = "init.cfg"
content_type = "text/cloud-config"
content = templatefile("${path.module}/templates/metadata.tpl", {
ipv4_address = var.ipv4_address
ipv4_netmask = var.ipv4_netmask
ipv4_gateway = var.ipv4_gateway
domain = var.domain
})
}
}
dynamic part {
for_each = var.user_data != "" ? [1] : []
content {
content_type = "text/x-shellscript"
content = templatefile(var.user_data, var.user_data_variables)
}
}
}
resource "vsphere_virtual_machine" "vm" {
name = var.name
num_cpus = var.cpus
memory = var.memory
resource_pool_id = data.vsphere_resource_pool.pool.id
datastore_id = var.datastore
network_interface {
network_id = var.network
adapter_type = data.vsphere_virtual_machine.template.network_interface_types[0]
}
scsi_type = data.vsphere_virtual_machine.template.scsi_type
guest_id = data.vsphere_virtual_machine.template.guest_id
disk {
label = "disk0"
size = var.disk_size
eagerly_scrub = data.vsphere_virtual_machine.template.disks.0.eagerly_scrub
thin_provisioned = data.vsphere_virtual_machine.template.disks.0.thin_provisioned
}
clone {
template_uuid = data.vsphere_virtual_machine.template.id
}
cdrom {
client_device = true
}
vapp {
properties = {
"instance-id" = var.name
"hostname" = var.name
"public-keys" = file(var.public_key)
"user-data" = var.dhcp == false || var.user_data != "" ? data.template_cloudinit_config.config.rendered : null
}
}
lifecycle {
ignore_changes = [
vapp
]
}
}