-
Notifications
You must be signed in to change notification settings - Fork 1
/
vsphere.pkr.hcl
309 lines (289 loc) · 9.92 KB
/
vsphere.pkr.hcl
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
locals {
linux_notes = "Template based on ${ var.os_family }-${ var.os_version } with CIS hardening, built using packer on {{ isotime \"2006-01-02\" }} at {{isotime \"3:04PM\"}}"
win_notes = "Template based on ${ var.os_family } ${ var.os_version }, built using packer on {{ isotime \"2006-01-02\" }} at {{isotime \"3:04PM\"}}"
speedtest_notes = "Speedtest template based on ${ var.os_family }-${ var.os_version } LTS with CIS hardening, built using packer on {{ isotime \"2006-01-02\" }} at {{isotime \"3:04PM\"}}"
vm_name = "${ var.os_family }${ var.os_version }"
}
source "vsphere-iso" "windows" {
# vCenter settings
vcenter_server = var.vcenter_server
username = var.vcenter_username
password = var.vcenter_password
insecure_connection = var.vcenter_connection
cluster = var.vcenter_cluster
datacenter = var.vcenter_datacenter
host = var.vcenter_host
datastore = var.vcenter_datastore
convert_to_template = var.convert_to_template
# VM Settings
boot_wait = "5s"
ip_wait_timeout = "45m"
communicator = "winrm"
winrm_username = var.connection_username
winrm_password = var.connection_password
winrm_timeout = "4h"
winrm_port = "5985"
shutdown_command = "shutdown /s /t 5 /f /d p:4:1 /c \"Packer Shutdown\""
shutdown_timeout = "30m"
vm_version = var.vm_hardware_version
iso_paths = [
var.os_iso_path
]
iso_checksum = var.iso_checksum
vm_name = "build-${local.vm_name}"
guest_os_type = var.guest_os_type
disk_controller_type = ["pvscsi"] # Windows requires vmware tools drivers for pvscsi to work
network_adapters {
# For windows, the vmware tools network drivers are required to be connected by floppy before tools is installed
network = var.vm_network
network_card = var.nic_type
}
storage {
disk_size = var.root_disk_size
disk_thin_provisioned = true
}
CPUs = var.num_cpu
cpu_cores = var.num_cores
CPU_hot_plug = true
RAM = var.vm_ram
RAM_hot_plug = true
floppy_files = [
"./boot_config/${var.os_version}/Autounattend.xml",
"./scripts/winrm.bat",
"./scripts/Install-VMWareTools.ps1",
"./drivers/"
]
content_library_destination {
datastore = var.vcenter_datastore
description = local.linux_notes
host = var.vcenter_host
library = var.vcenter_content_library
name = "template_${local.vm_name}"
ovf = "true"
}
notes = local.win_notes
}
source "vsphere-iso" "ubuntu" {
# vCenter settings
vcenter_server = var.vcenter_server
username = var.vcenter_username
password = var.vcenter_password
insecure_connection = var.vcenter_connection
cluster = var.vcenter_cluster
datacenter = var.vcenter_datacenter
host = var.vcenter_host
datastore = var.vcenter_datastore
convert_to_template = var.convert_to_template
# VM Settings
ip_wait_timeout = "45m"
ssh_username = var.connection_username
ssh_password = var.connection_password
ssh_timeout = "20m"
ssh_port = "22"
ssh_handshake_attempts = "100"
shutdown_timeout = "15m"
vm_version = var.vm_hardware_version
iso_paths = [var.os_iso_path]
iso_checksum = var.iso_checksum
vm_name = "build-${local.vm_name}"
guest_os_type = var.guest_os_type
disk_controller_type = ["pvscsi"]
network_adapters {
network = var.vm_network
network_card = var.nic_type
}
storage {
disk_size = var.root_disk_size
disk_thin_provisioned = true
}
CPUs = var.num_cpu
cpu_cores = var.num_cores
CPU_hot_plug = true
RAM = var.vm_ram
RAM_hot_plug = true
boot_wait = "5s"
boot_command = var.boot_command
## new things to watch
notes = local.linux_notes
cd_files = [
"./boot_config/${var.os_family}/meta-data",
"./boot_config/${var.os_family}/user-data",
"./boot_config/${var.os_family}/preseed.cfg"
]
cd_label = "cidata"
floppy_files = ["./boot_config/${var.os_family}/preseed.cfg"]
content_library_destination {
datastore = var.vcenter_datastore
description = local.linux_notes
host = var.vcenter_host
library = var.vcenter_content_library
name = "template_${local.vm_name}"
ovf = "true"
}
}
source "vsphere-iso" "rocky" {
# vCenter settings
vcenter_server = var.vcenter_server
username = var.vcenter_username
password = var.vcenter_password
insecure_connection = var.vcenter_connection
cluster = var.vcenter_cluster
datacenter = var.vcenter_datacenter
host = var.vcenter_host
datastore = var.vcenter_datastore
convert_to_template = var.convert_to_template
# VM Settings
ip_wait_timeout = "45m"
ssh_username = var.connection_username
ssh_password = var.connection_password
ssh_timeout = "20m"
ssh_port = "22"
ssh_handshake_attempts = "100"
shutdown_timeout = "15m"
vm_version = var.vm_hardware_version
iso_paths = [var.os_iso_path]
iso_checksum = var.iso_checksum
vm_name = "build-${local.vm_name}"
guest_os_type = var.guest_os_type
disk_controller_type = ["pvscsi"]
network_adapters {
network = var.vm_network
network_card = var.nic_type
}
storage {
disk_size = var.root_disk_size
disk_thin_provisioned = true
}
CPUs = var.num_cpu
cpu_cores = var.num_cores
CPU_hot_plug = true
RAM = var.vm_ram
RAM_hot_plug = true
boot_wait = "5s"
boot_command = var.boot_command
## new things to watch
notes = local.linux_notes
cd_files = [
"./boot_config/${var.os_family}/ks.cfg",
]
content_library_destination {
datastore = var.vcenter_datastore
description = local.linux_notes
host = var.vcenter_host
library = var.vcenter_content_library
name = "template_${local.vm_name}"
ovf = "true"
}
cd_label = "cidata"
}
## windows build
build {
# Windows builds
sources = [
"source.vsphere-iso.windows",
]
provisioner "powershell" {
pause_before = "1m"
elevated_user = var.connection_username
elevated_password = var.connection_password
scripts = [
"scripts/Disable-UAC.ps1"
]
}
provisioner "powershell" {
pause_before = "1m"
elevated_user = var.connection_username
elevated_password = var.connection_password
scripts = [
"scripts/setup.ps1"
]
}
provisioner "powershell" {
pause_before = "1m"
elevated_user = var.connection_username
elevated_password = var.connection_password
scripts = [
"scripts/convertEval.ps1"
]
}
provisioner "windows-restart" {
restart_timeout = "30m"
}
provisioner "powershell" {
pause_before = "1m"
elevated_user = var.connection_username
elevated_password = var.connection_password
scripts = [
"scripts/win-update.ps1"
]
}
provisioner "windows-restart" {
restart_timeout = "30m"
}
provisioner "powershell" {
pause_before = "5m"
elevated_user = var.connection_username
elevated_password = var.connection_password
scripts = [
"scripts/win-update.ps1"
]
}
provisioner "windows-restart" {
restart_timeout = "30m"
}
provisioner "powershell" {
elevated_user = var.connection_username
elevated_password = var.connection_password
pause_before = "1m"
scripts = [
"scripts/Remove-UpdateCache.ps1",
"scripts/Invoke-Defrag.ps1",
"scripts/cleanup.ps1",
"scripts/Reset-EmptySpace.ps1"
]
}
}
## ubuntu build
build {
sources = [
"source.vsphere-iso.ubuntu",
]
provisioner "shell" {
execute_command = "sudo -S bash {{ .Path }}"
script = "./boot_config/ubuntu/baseInit.sh"
}
provisioner "ansible-local" {
playbook_dir = "cis-hardening/ubuntu-ansible"
playbook_file = "cis-hardening/ubuntu-ansible/playbook.yml"
role_paths = ["cis-hardening/ubuntu-cis-hardening"]
}
provisioner "ansible" {
playbook_file = "/etc/ansible/playbook/roster.yml"
}
provisioner "shell" {
execute_command = "sudo -S bash {{ .Path }}"
script = "./boot_config/ubuntu/post_cleanup.sh"
}
}
## rocky build
build {
sources = [
"source.vsphere-iso.rocky",
]
provisioner "shell" {
execute_command = "sudo -S bash {{ .Path }}"
script = "./boot_config/${var.os_family}/baseInit.sh"
}
provisioner "ansible-local" {
playbook_dir = "cis-hardening/rocky-ansible"
playbook_file = "cis-hardening/rocky-ansible/playbook.yml"
role_paths = ["cis-hardening/rocky-cis-hardening"]
}
provisioner "ansible" {
playbook_file = "/etc/ansible/playbook/roster.yml"
}
provisioner "shell" {
execute_command = "sudo -S bash {{ .Path }}"
script = "./boot_config/${var.os_family}/post_cleanup.sh"
}
}