-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
275 lines (230 loc) · 11.7 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
# Create VM
resource "vcd_vapp_vm" "vm" {
vapp_name = var.vapp
name = var.name
catalog_name = var.template.catalog
template_name = var.template.name
memory = var.ram * 1024
cpus = var.cpus
cpu_cores = var.cores_per_socket
storage_profile = var.storage_profile
computer_name = var.name
override_template_disk {
size_in_mb = var.system_disk_size * 1024
bus_type = var.system_disk_bus
bus_number = 0
unit_number = 0
}
dynamic "network" {
for_each = var.nics
content {
type = "org"
name = network.value["network"]
ip_allocation_mode = network.value["ip"] != "" ? "MANUAL" : "POOL"
ip = network.value["ip"] != "" ? network.value["ip"] : null
}
}
customization {
enabled = true
change_sid = true
allow_local_admin_password = true
auto_generate_password = var.local_admin_password != null ? false : true
admin_password = var.local_admin_password != null ? var.local_admin_password : null
join_domain = var.domain_fqdn != null ? true : false
join_domain_name = var.domain_fqdn != null ? var.domain_fqdn : null
join_domain_user = var.domain_fqdn != null ? var.domain_user : null
join_domain_password = var.domain_fqdn != null ? var.domain_password : null
join_domain_account_ou = var.domain_fqdn != null ? var.ou != "" ? var.ou : null : null
}
}
# Add VM data disks
resource "vcd_vm_internal_disk" "disk" {
count = length(var.data_disks)
vapp_name = vcd_vapp_vm.vm.vapp_name
vm_name = vcd_vapp_vm.vm.name
bus_type = "paravirtual"
size_in_mb = var.data_disks[count.index].size * 1024
bus_number = 1
unit_number = count.index
storage_profile = var.data_disks[count.index].storage_profile != "" ? var.data_disks[count.index].storage_profile : ""
}
# Insert media
resource "vcd_inserted_media" "media" {
count = var.media != null ? 1 : 0
depends_on = [ vcd_vm_internal_disk.disk ]
vapp_name = vcd_vapp_vm.vm.vapp_name
vm_name = vcd_vapp_vm.vm.name
catalog = var.media.catalog
name = var.media.name
eject_force = true
}
# Get random SSH port
resource "random_integer" "ssh-port" {
count = var.allow_external_ssh == true ? var.external_ssh_port == "" ? 1 : 0 : 0
min = 40000
max = 49999
}
# Get random RDP port
resource "random_integer" "rdp-port" {
count = var.allow_external_rdp == true ? var.external_rdp_port == "" ? 1 : 0 : 0
min = 50000
max = 59999
}
# SSH DNAT rule
resource "vcd_nsxv_dnat" "ssh-dnat-rule" {
count = var.allow_external_ssh == true ? 1 : 0
edge_gateway = data.vcd_edgegateway.edge.name
network_type = "ext"
network_name = tolist(data.vcd_edgegateway.edge.external_network)[0].name
original_address = data.vcd_edgegateway.edge.external_network_ips[0]
original_port = var.external_ssh_port != "" ? var.external_ssh_port : random_integer.ssh-port[0].result
translated_address = vcd_vapp_vm.vm.network[0].ip
translated_port = "22"
protocol = "tcp"
description = "SSH to ${vcd_vapp_vm.vm.name}"
}
# SSH firewall rule
resource "vcd_nsxv_firewall_rule" "ssh-firewall-rule" {
count = var.allow_external_ssh == true ? 1 : 0
edge_gateway = data.vcd_edgegateway.edge.name
name = "SSH to ${vcd_vapp_vm.vm.name}"
source {
ip_addresses = [trimspace(data.http.terraform-external-ip.body)]
}
destination {
ip_addresses = [data.vcd_edgegateway.edge.external_network_ips[0]]
}
service {
protocol = "tcp"
port = var.external_ssh_port != "" ? var.external_ssh_port : random_integer.ssh-port[0].result
}
}
# RDP DNAT rule
resource "vcd_nsxv_dnat" "rdp-rule" {
count = var.allow_external_rdp == true ? 1 : 0
edge_gateway = data.vcd_edgegateway.edge.name
network_type = "ext"
network_name = tolist(data.vcd_edgegateway.edge.external_network)[0].name
original_address = data.vcd_edgegateway.edge.external_network_ips[0]
original_port = var.external_rdp_port != "" ? var.external_rdp_port : random_integer.rdp-port[0].result
translated_address = vcd_vapp_vm.vm.network[0].ip
translated_port = "3389"
protocol = "tcp"
description = "RDP to ${vcd_vapp_vm.vm.name}"
}
# RDP firewall rule
resource "vcd_nsxv_firewall_rule" "rdp-firewall-rule" {
count = var.allow_external_rdp == true ? 1 : 0
edge_gateway = data.vcd_edgegateway.edge.name
name = "RDP to ${vcd_vapp_vm.vm.name}"
source {
ip_addresses = [trimspace(data.http.terraform-external-ip.body)]
}
destination {
ip_addresses = [data.vcd_edgegateway.edge.external_network_ips[0]]
}
service {
protocol = "tcp"
port = var.external_rdp_port != "" ? var.external_rdp_port : random_integer.rdp-port[0].result
}
}
# Set default shell for SSH
resource "null_resource" "set-default-shell" {
depends_on = [ vcd_vapp_vm.vm ]
provisioner "remote-exec" {
connection {
type = "ssh"
target_platform = "windows"
user = "Administrator"
password = vcd_vapp_vm.vm.customization[0].admin_password
host = var.allow_external_ssh == true ? var.external_ip != "" ? var.external_ip : data.vcd_edgegateway.edge.external_network_ips[0] : vcd_vapp_vm.vm.network[0].ip
port = var.allow_external_ssh == true ? var.external_ssh_port != "" ? var.external_ssh_port : random_integer.ssh-port[0].result : 22
script_path = "/Windows/Temp/terraform_%RAND%.cmd"
timeout = "15m"
}
inline = [
"reg add HKLM\\SOFTWARE\\OpenSSH /v DefaultShell /t REG_SZ /d C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe /f"
]
}
}
# Initial OS configuration
resource "null_resource" "initial-config" {
depends_on = [ null_resource.set-default-shell ]
provisioner "remote-exec" {
connection {
type = "ssh"
target_platform = "windows"
user = "Administrator"
password = vcd_vapp_vm.vm.customization[0].admin_password
host = var.allow_external_ssh == true ? var.external_ip != "" ? var.external_ip : data.vcd_edgegateway.edge.external_network_ips[0] : vcd_vapp_vm.vm.network[0].ip
port = var.allow_external_ssh == true ? var.external_ssh_port != "" ? var.external_ssh_port : random_integer.ssh-port[0].result : 22
script_path = "/Windows/Temp/terraform_%RAND%.ps1"
timeout = "15m"
}
inline = [
"$global:ProgressPreference = 'SilentlyContinue'",
"Get-Partition -DriveLetter C | Resize-Partition -Size (Get-PartitionSupportedSize -DriveLetter C).sizeMax -Confirm:$false -ErrorAction SilentlyContinue",
"Set-WmiInstance -InputObject (Get-WmiObject -Class Win32_volume -Filter 'DriveLetter = \"D:\"') -Arguments @{DriveLetter=([char]([int][char]\"D\" + ${length(var.data_disks)}) + \":\")}",
"New-ItemProperty -Path 'HKLM:\\SYSTEM\\CurrentControlSet\\Control\\Terminal Server' -Name 'fDenyTSConnections' -Value 0 -Force",
"Enable-NetFirewallRule -DisplayGroup 'Remote Desktop'",
"Enable-NetFirewallRule -DisplayName 'File and Printer Sharing (Echo Request - ICMPv4-In)'",
"Set-ItemProperty -Path 'HKLM:\\SOFTWARE\\Microsoft\\Active Setup\\Installed Components\\{A509B1A7-37EF-4b3f-8CFC-4F3A74704073}' -Name 'IsInstalled' -Value 0",
"Set-ItemProperty -Path 'HKLM:\\SOFTWARE\\Wow6432Node\\Microsoft\\.NetFramework\\v4.0.30319' -Name 'SchUseStrongCrypto' -Value '1' -Type DWord -Force -Confirm:$false",
"Set-ItemProperty -Path 'HKLM:\\SOFTWARE\\Microsoft\\.NetFramework\\v4.0.30319' -Name 'SchUseStrongCrypto' -Value '1' -Type DWord -Force -Confirm:$false",
"$global:ProgressPreference = 'Continue'"
]
}
}
# Data disk configuration
resource "null_resource" "disk-config" {
count = length(var.data_disks)
depends_on = [ null_resource.initial-config ]
provisioner "remote-exec" {
connection {
type = "ssh"
target_platform = "windows"
user = "Administrator"
password = vcd_vapp_vm.vm.customization[0].admin_password
host = var.allow_external_ssh == true ? var.external_ip != "" ? var.external_ip : data.vcd_edgegateway.edge.external_network_ips[0] : vcd_vapp_vm.vm.network[0].ip
port = var.allow_external_ssh == true ? var.external_ssh_port != "" ? var.external_ssh_port : random_integer.ssh-port[0].result : 22
script_path = "/Windows/Temp/terraform_%RAND%.ps1"
timeout = "15m"
}
inline = [
"$global:ProgressPreference = 'SilentlyContinue'",
"Get-Disk -Number (Get-WmiObject -Class Win32_DiskDrive | ?{$_.SCSIPort -ne '0' -and $_.SCSITargetId -eq ${count.index}}).Index | Initialize-Disk -PartitionStyle GPT -PassThru | New-Partition -DriveLetter ${var.data_disks[count.index].letter} -UseMaximumSize",
"Format-Volume -DriveLetter ${var.data_disks[count.index].letter} -FileSystem NTFS -AllocationUnitSize ${var.data_disks[count.index].block_size != "" ? var.data_disks[count.index].block_size * 1024 : 4096} -Confirm:$false",
"$global:ProgressPreference = 'Continue'"
]
}
}
# Install and configure Zabbix agent
resource "null_resource" "zabbix-agent" {
count = var.zabbix_server != "" ? 1 : 0
depends_on = [ null_resource.initial-config ]
provisioner "remote-exec" {
connection {
type = "ssh"
target_platform = "windows"
user = "Administrator"
password = vcd_vapp_vm.vm.customization[0].admin_password
host = var.allow_external_ssh == true ? var.external_ip != "" ? var.external_ip : data.vcd_edgegateway.edge.external_network_ips[0] : vcd_vapp_vm.vm.network[0].ip
port = var.allow_external_ssh == true ? var.external_ssh_port != "" ? var.external_ssh_port : random_integer.ssh-port[0].result : 22
script_path = "/Windows/Temp/terraform_%RAND%.ps1"
timeout = "15m"
}
inline = [
"$global:ProgressPreference = 'SilentlyContinue'",
"iwr https://cdn.zabbix.com/zabbix/binaries/stable/5.0/5.0.9/zabbix_agent-5.0.9-windows-amd64.zip -OutFile C:\\Windows\\Temp\\zabbix_agent-5.0.9-windows-amd64.zip",
"Expand-Archive C:\\Windows\\Temp\\zabbix_agent-5.0.9-windows-amd64.zip -DestinationPath C:\\zabbix -Force",
"(Get-Content C:\\zabbix\\conf\\zabbix_agentd.conf).Replace('LogFile=c:\\zabbix_agentd.log', 'LogFile=c:\\zabbix\\zabbix_agentd.log') | Set-Content -Path C:\\zabbix\\conf\\zabbix_agentd.conf",
"(Get-Content C:\\zabbix\\conf\\zabbix_agentd.conf).Replace('Server=127.0.0.1', 'Server=${var.zabbix_server}') | Set-Content -Path C:\\zabbix\\conf\\zabbix_agentd.conf",
"(Get-Content C:\\zabbix\\conf\\zabbix_agentd.conf).Replace('ServerActive=127.0.0.1', 'ServerActive=${var.zabbix_server}') | Set-Content -Path C:\\zabbix\\conf\\zabbix_agentd.conf",
"(Get-Content C:\\zabbix\\conf\\zabbix_agentd.conf).Replace('Hostname=Windows host', \"Hostname=$env:COMPUTERNAME\") | Set-Content -Path C:\\zabbix\\conf\\zabbix_agentd.conf",
"C:\\zabbix\\bin\\zabbix_agentd.exe --config c:\\zabbix\\conf\\zabbix_agentd.conf --install",
"New-NetFirewallRule -DisplayName 'Zabbix agent' -Direction Inbound -Protocol TCP -LocalPort 10050 -Action Allow",
"C:\\zabbix\\bin\\zabbix_agentd.exe --start",
"$global:ProgressPreference = 'Continue'"
]
}
}