-
Notifications
You must be signed in to change notification settings - Fork 311
/
windows-server.pkr.hcl
571 lines (522 loc) · 21.8 KB
/
windows-server.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
# Copyright 2023-2024 Broadcom. All rights reserved.
# SPDX-License-Identifier: BSD-2
/*
DESCRIPTION:
Microsoft Windows Server 2025 build definition.
Packer Plugin for VMware vSphere: 'vsphere-iso' builder.
*/
// BLOCK: packer
// The Packer configuration.
packer {
required_version = ">= 1.10.0"
required_plugins {
vsphere = {
source = "github.com/hashicorp/vsphere"
version = "1.2.4"
}
git = {
source = "github.com/ethanmdavidson/git"
version = ">= 0.6.2"
}
ansible = {
source = "github.com/hashicorp/ansible"
version = ">= 1.1.0"
}
}
}
// BLOCK: data
// Defines the data sources.
data "git-repository" "cwd" {}
// BLOCK: locals
// Defines the local variables.
locals {
build_by = "Built by: HashiCorp Packer ${packer.version}"
build_date = formatdate("YYYY-MM-DD hh:mm ZZZ", timestamp())
build_version = data.git-repository.cwd.head
build_description = "Version: ${local.build_version}\nBuilt on: ${local.build_date}\n${local.build_by}"
iso_paths = {
content_library = "${var.common_iso_content_library}/${var.iso_content_library_item}/${var.iso_file}",
datastore = "[${var.common_iso_datastore}] ${var.iso_datastore_path}/${var.iso_file}"
tools = "[] /vmimages/tools-isoimages/${var.vm_guest_os_family}.iso"
}
manifest_date = formatdate("YYYY-MM-DD hh:mm:ss", timestamp())
manifest_path = "${path.cwd}/manifests/"
manifest_output = "${local.manifest_path}${local.manifest_date}.json"
ovf_export_path = "${path.cwd}/artifacts/"
vm_name_datacenter_core = "${var.vm_guest_os_family}-${var.vm_guest_os_name}-${var.vm_guest_os_version}-${var.vm_guest_os_edition_datacenter}-${var.vm_guest_os_experience_core}-${local.build_version}"
vm_name_datacenter_desktop = "${var.vm_guest_os_family}-${var.vm_guest_os_name}-${var.vm_guest_os_version}-${var.vm_guest_os_edition_datacenter}-${var.vm_guest_os_experience_desktop}-${local.build_version}"
vm_name_standard_core = "${var.vm_guest_os_family}-${var.vm_guest_os_name}-${var.vm_guest_os_version}-${var.vm_guest_os_edition_standard}-${var.vm_guest_os_experience_core}-${local.build_version}"
vm_name_standard_desktop = "${var.vm_guest_os_family}-${var.vm_guest_os_name}-${var.vm_guest_os_version}-${var.vm_guest_os_edition_standard}-${var.vm_guest_os_experience_desktop}-${local.build_version}"
bucket_name = replace("${var.vm_guest_os_family}-${var.vm_guest_os_name}-${var.vm_guest_os_version}", ".", "")
bucket_description = "${var.vm_guest_os_family} ${var.vm_guest_os_name} ${var.vm_guest_os_version}"
}
// BLOCK: source
// Defines the builder configuration blocks.
source "vsphere-iso" "windows-server-standard-core" {
// vCenter Server Endpoint Settings and Credentials
vcenter_server = var.vsphere_endpoint
username = var.vsphere_username
password = var.vsphere_password
insecure_connection = var.vsphere_insecure_connection
// vSphere Settings
datacenter = var.vsphere_datacenter
cluster = var.vsphere_cluster
host = var.vsphere_host
datastore = var.vsphere_datastore
folder = var.vsphere_folder
resource_pool = var.vsphere_resource_pool
set_host_for_datastore_uploads = var.vsphere_set_host_for_datastore_uploads
// Virtual Machine Settings
vm_name = local.vm_name_standard_core
guest_os_type = var.vm_guest_os_type
firmware = var.vm_firmware
CPUs = var.vm_cpu_count
cpu_cores = var.vm_cpu_cores
CPU_hot_plug = var.vm_cpu_hot_add
RAM = var.vm_mem_size
RAM_hot_plug = var.vm_mem_hot_add
cdrom_type = var.vm_cdrom_type
disk_controller_type = var.vm_disk_controller_type
storage {
disk_size = var.vm_disk_size
disk_thin_provisioned = var.vm_disk_thin_provisioned
}
network_adapters {
network = var.vsphere_network
network_card = var.vm_network_card
}
vm_version = var.common_vm_version
remove_cdrom = var.common_remove_cdrom
reattach_cdroms = var.vm_cdrom_count
tools_upgrade_policy = var.common_tools_upgrade_policy
notes = local.build_description
// Removable Media Settings
iso_paths = var.common_iso_content_library_enabled ? [local.iso_paths.content_library, local.iso_paths.tools] : [local.iso_paths.datastore, local.iso_paths.tools]
cd_files = [
"${path.cwd}/scripts/${var.vm_guest_os_family}/"
]
cd_content = {
"autounattend.xml" = templatefile("${abspath(path.root)}/data/autounattend.pkrtpl.hcl", {
build_username = var.build_username
build_password = var.build_password
vm_inst_os_eval = var.vm_inst_os_eval
vm_inst_os_language = var.vm_inst_os_language
vm_inst_os_keyboard = var.vm_inst_os_keyboard
vm_inst_os_image = var.vm_inst_os_image_standard_core
vm_inst_os_key = var.vm_inst_os_key_standard
vm_guest_os_language = var.vm_guest_os_language
vm_guest_os_keyboard = var.vm_guest_os_keyboard
vm_guest_os_timezone = var.vm_guest_os_timezone
})
}
// Boot and Provisioning Settings
http_port_min = var.common_http_port_min
http_port_max = var.common_http_port_max
boot_order = var.vm_boot_order
boot_wait = var.vm_boot_wait
boot_command = var.vm_boot_command
ip_wait_timeout = var.common_ip_wait_timeout
ip_settle_timeout = var.common_ip_settle_timeout
shutdown_command = var.vm_shutdown_command
shutdown_timeout = var.common_shutdown_timeout
// Communicator Settings and Credentials
communicator = "winrm"
winrm_username = var.build_username
winrm_password = var.build_password
winrm_port = var.communicator_port
winrm_timeout = var.communicator_timeout
// Template and Content Library Settings
convert_to_template = var.common_template_conversion
dynamic "content_library_destination" {
for_each = var.common_content_library_enabled ? [1] : []
content {
library = var.common_content_library
description = local.build_description
ovf = var.common_content_library_ovf
destroy = var.common_content_library_destroy
skip_import = var.common_content_library_skip_export
}
}
// OVF Export Settings
dynamic "export" {
for_each = var.common_ovf_export_enabled ? [1] : []
content {
name = local.vm_name_standard_core
force = var.common_ovf_export_overwrite
options = [
"extraconfig"
]
output_directory = "${local.ovf_export_path}/${local.vm_name_standard_core}"
}
}
}
source "vsphere-iso" "windows-server-standard-dexp" {
// vCenter Server Endpoint Settings and Credentials
vcenter_server = var.vsphere_endpoint
username = var.vsphere_username
password = var.vsphere_password
insecure_connection = var.vsphere_insecure_connection
// vSphere Settings
datacenter = var.vsphere_datacenter
cluster = var.vsphere_cluster
host = var.vsphere_host
datastore = var.vsphere_datastore
folder = var.vsphere_folder
resource_pool = var.vsphere_resource_pool
set_host_for_datastore_uploads = var.vsphere_set_host_for_datastore_uploads
// Virtual Machine Settings
vm_name = local.vm_name_standard_desktop
guest_os_type = var.vm_guest_os_type
firmware = var.vm_firmware
CPUs = var.vm_cpu_count
cpu_cores = var.vm_cpu_cores
CPU_hot_plug = var.vm_cpu_hot_add
RAM = var.vm_mem_size
RAM_hot_plug = var.vm_mem_hot_add
cdrom_type = var.vm_cdrom_type
disk_controller_type = var.vm_disk_controller_type
storage {
disk_size = var.vm_disk_size
disk_controller_index = 0
disk_thin_provisioned = var.vm_disk_thin_provisioned
}
network_adapters {
network = var.vsphere_network
network_card = var.vm_network_card
}
vm_version = var.common_vm_version
remove_cdrom = var.common_remove_cdrom
reattach_cdroms = var.vm_cdrom_count
tools_upgrade_policy = var.common_tools_upgrade_policy
notes = local.build_description
// Removable Media Settings
iso_paths = var.common_iso_content_library_enabled ? [local.iso_paths.content_library, local.iso_paths.tools] : [local.iso_paths.datastore, local.iso_paths.tools]
cd_files = [
"${path.cwd}/scripts/${var.vm_guest_os_family}/"
]
cd_content = {
"autounattend.xml" = templatefile("${abspath(path.root)}/data/autounattend.pkrtpl.hcl", {
build_username = var.build_username
build_password = var.build_password
vm_inst_os_eval = var.vm_inst_os_eval
vm_inst_os_language = var.vm_inst_os_language
vm_inst_os_keyboard = var.vm_inst_os_keyboard
vm_inst_os_image = var.vm_inst_os_image_standard_desktop
vm_inst_os_key = var.vm_inst_os_key_standard
vm_guest_os_language = var.vm_guest_os_language
vm_guest_os_keyboard = var.vm_guest_os_keyboard
vm_guest_os_timezone = var.vm_guest_os_timezone
})
}
// Boot and Provisioning Settings
http_port_min = var.common_http_port_min
http_port_max = var.common_http_port_max
boot_order = var.vm_boot_order
boot_wait = var.vm_boot_wait
boot_command = var.vm_boot_command
ip_wait_timeout = var.common_ip_wait_timeout
ip_settle_timeout = var.common_ip_settle_timeout
shutdown_command = var.vm_shutdown_command
shutdown_timeout = var.common_shutdown_timeout
// Communicator Settings and Credentials
communicator = "winrm"
winrm_username = var.build_username
winrm_password = var.build_password
winrm_port = var.communicator_port
winrm_timeout = var.communicator_timeout
// Template and Content Library Settings
convert_to_template = var.common_template_conversion
dynamic "content_library_destination" {
for_each = var.common_content_library_enabled ? [1] : []
content {
library = var.common_content_library
description = local.build_description
ovf = var.common_content_library_ovf
destroy = var.common_content_library_destroy
skip_import = var.common_content_library_skip_export
}
}
// OVF Export Settings
dynamic "export" {
for_each = var.common_ovf_export_enabled ? [1] : []
content {
name = local.vm_name_standard_desktop
force = var.common_ovf_export_overwrite
options = [
"extraconfig"
]
output_directory = "${local.ovf_export_path}/${local.vm_name_standard_desktop}"
}
}
}
source "vsphere-iso" "windows-server-datacenter-core" {
// vCenter Server Endpoint Settings and Credentials
vcenter_server = var.vsphere_endpoint
username = var.vsphere_username
password = var.vsphere_password
insecure_connection = var.vsphere_insecure_connection
// vSphere Settings
datacenter = var.vsphere_datacenter
cluster = var.vsphere_cluster
host = var.vsphere_host
datastore = var.vsphere_datastore
folder = var.vsphere_folder
resource_pool = var.vsphere_resource_pool
set_host_for_datastore_uploads = var.vsphere_set_host_for_datastore_uploads
// Virtual Machine Settings
vm_name = local.vm_name_datacenter_core
guest_os_type = var.vm_guest_os_type
firmware = var.vm_firmware
CPUs = var.vm_cpu_count
cpu_cores = var.vm_cpu_cores
CPU_hot_plug = var.vm_cpu_hot_add
RAM = var.vm_mem_size
RAM_hot_plug = var.vm_mem_hot_add
cdrom_type = var.vm_cdrom_type
disk_controller_type = var.vm_disk_controller_type
storage {
disk_size = var.vm_disk_size
disk_controller_index = 0
disk_thin_provisioned = var.vm_disk_thin_provisioned
}
network_adapters {
network = var.vsphere_network
network_card = var.vm_network_card
}
vm_version = var.common_vm_version
remove_cdrom = var.common_remove_cdrom
reattach_cdroms = var.vm_cdrom_count
tools_upgrade_policy = var.common_tools_upgrade_policy
notes = local.build_description
// Removable Media Settings
iso_paths = var.common_iso_content_library_enabled ? [local.iso_paths.content_library, local.iso_paths.tools] : [local.iso_paths.datastore, local.iso_paths.tools]
cd_files = [
"${path.cwd}/scripts/${var.vm_guest_os_family}/"
]
cd_content = {
"autounattend.xml" = templatefile("${abspath(path.root)}/data/autounattend.pkrtpl.hcl", {
build_username = var.build_username
build_password = var.build_password
vm_inst_os_eval = var.vm_inst_os_eval
vm_inst_os_language = var.vm_inst_os_language
vm_inst_os_keyboard = var.vm_inst_os_keyboard
vm_inst_os_language = var.vm_inst_os_language
vm_inst_os_keyboard = var.vm_inst_os_keyboard
vm_inst_os_image = var.vm_inst_os_image_datacenter_core
vm_inst_os_key = var.vm_inst_os_key_datacenter
vm_guest_os_language = var.vm_guest_os_language
vm_guest_os_keyboard = var.vm_guest_os_keyboard
vm_guest_os_timezone = var.vm_guest_os_timezone
})
}
// Boot and Provisioning Settings
http_port_min = var.common_http_port_min
http_port_max = var.common_http_port_max
boot_order = var.vm_boot_order
boot_wait = var.vm_boot_wait
boot_command = var.vm_boot_command
ip_wait_timeout = var.common_ip_wait_timeout
ip_settle_timeout = var.common_ip_settle_timeout
shutdown_command = var.vm_shutdown_command
shutdown_timeout = var.common_shutdown_timeout
// Communicator Settings and Credentials
communicator = "winrm"
winrm_username = var.build_username
winrm_password = var.build_password
winrm_port = var.communicator_port
winrm_timeout = var.communicator_timeout
// Template and Content Library Settings
convert_to_template = var.common_template_conversion
dynamic "content_library_destination" {
for_each = var.common_content_library_enabled ? [1] : []
content {
library = var.common_content_library
description = local.build_description
ovf = var.common_content_library_ovf
destroy = var.common_content_library_destroy
skip_import = var.common_content_library_skip_export
}
}
// OVF Export Settings
dynamic "export" {
for_each = var.common_ovf_export_enabled ? [1] : []
content {
name = local.vm_name_datacenter_core
force = var.common_ovf_export_overwrite
options = [
"extraconfig"
]
output_directory = "${local.ovf_export_path}/${local.vm_name_datacenter_core}"
}
}
}
source "vsphere-iso" "windows-server-datacenter-dexp" {
// vCenter Server Endpoint Settings and Credentials
vcenter_server = var.vsphere_endpoint
username = var.vsphere_username
password = var.vsphere_password
insecure_connection = var.vsphere_insecure_connection
// vSphere Settings
datacenter = var.vsphere_datacenter
cluster = var.vsphere_cluster
host = var.vsphere_host
datastore = var.vsphere_datastore
folder = var.vsphere_folder
resource_pool = var.vsphere_resource_pool
set_host_for_datastore_uploads = var.vsphere_set_host_for_datastore_uploads
// Virtual Machine Settings
vm_name = local.vm_name_datacenter_desktop
guest_os_type = var.vm_guest_os_type
firmware = var.vm_firmware
CPUs = var.vm_cpu_count
cpu_cores = var.vm_cpu_cores
CPU_hot_plug = var.vm_cpu_hot_add
RAM = var.vm_mem_size
RAM_hot_plug = var.vm_mem_hot_add
cdrom_type = var.vm_cdrom_type
disk_controller_type = var.vm_disk_controller_type
storage {
disk_size = var.vm_disk_size
disk_controller_index = 0
disk_thin_provisioned = var.vm_disk_thin_provisioned
}
network_adapters {
network = var.vsphere_network
network_card = var.vm_network_card
}
vm_version = var.common_vm_version
remove_cdrom = var.common_remove_cdrom
reattach_cdroms = var.vm_cdrom_count
tools_upgrade_policy = var.common_tools_upgrade_policy
notes = local.build_description
// Removable Media Settings
iso_paths = var.common_iso_content_library_enabled ? [local.iso_paths.content_library, local.iso_paths.tools] : [local.iso_paths.datastore, local.iso_paths.tools]
cd_files = [
"${path.cwd}/scripts/${var.vm_guest_os_family}/"
]
cd_content = {
"autounattend.xml" = templatefile("${abspath(path.root)}/data/autounattend.pkrtpl.hcl", {
build_username = var.build_username
build_password = var.build_password
vm_inst_os_eval = var.vm_inst_os_eval
vm_inst_os_language = var.vm_inst_os_language
vm_inst_os_keyboard = var.vm_inst_os_keyboard
vm_inst_os_image = var.vm_inst_os_image_datacenter_desktop
vm_inst_os_key = var.vm_inst_os_key_datacenter
vm_guest_os_language = var.vm_guest_os_language
vm_guest_os_keyboard = var.vm_guest_os_keyboard
vm_guest_os_timezone = var.vm_guest_os_timezone
})
}
// Boot and Provisioning Settings
http_port_min = var.common_http_port_min
http_port_max = var.common_http_port_max
boot_order = var.vm_boot_order
boot_wait = var.vm_boot_wait
boot_command = var.vm_boot_command
ip_wait_timeout = var.common_ip_wait_timeout
ip_settle_timeout = var.common_ip_settle_timeout
shutdown_command = var.vm_shutdown_command
shutdown_timeout = var.common_shutdown_timeout
// Communicator Settings and Credentials
communicator = "winrm"
winrm_username = var.build_username
winrm_password = var.build_password
winrm_port = var.communicator_port
winrm_timeout = var.communicator_timeout
// Template and Content Library Settings
convert_to_template = var.common_template_conversion
dynamic "content_library_destination" {
for_each = var.common_content_library_enabled ? [1] : []
content {
library = var.common_content_library
description = local.build_description
ovf = var.common_content_library_ovf
destroy = var.common_content_library_destroy
skip_import = var.common_content_library_skip_export
}
}
// OVF Export Settings
dynamic "export" {
for_each = var.common_ovf_export_enabled ? [1] : []
content {
name = local.vm_name_datacenter_desktop
force = var.common_ovf_export_overwrite
options = [
"extraconfig"
]
output_directory = "${local.ovf_export_path}/${local.vm_name_datacenter_desktop}"
}
}
}
// BLOCK: build
// Defines the builders to run, provisioners, and post-processors.
build {
sources = [
"source.vsphere-iso.windows-server-standard-core",
"source.vsphere-iso.windows-server-standard-dexp",
"source.vsphere-iso.windows-server-datacenter-core",
"source.vsphere-iso.windows-server-datacenter-dexp"
]
provisioner "ansible" {
user = var.build_username
galaxy_file = "${path.cwd}/ansible/windows-requirements.yml"
galaxy_force_with_deps = true
use_proxy = false
playbook_file = "${path.cwd}/ansible/windows-playbook.yml"
roles_path = "${path.cwd}/ansible/roles"
ansible_env_vars = [
"ANSIBLE_CONFIG=${path.cwd}/ansible/ansible.cfg"
]
extra_arguments = [
"--extra-vars", "use_proxy=false",
"--extra-vars", "ansible_connection=winrm",
"--extra-vars", "ansible_user='${var.build_username}'",
"--extra-vars", "ansible_password='${var.build_password}'",
"--extra-vars", "ansible_port='${var.communicator_port}'",
"--extra-vars", "build_username='${var.build_username}'",
]
}
post-processor "manifest" {
output = local.manifest_output
strip_path = true
strip_time = true
custom_data = {
build_username = var.build_username
build_date = local.build_date
build_version = local.build_version
common_data_source = var.common_data_source
common_vm_version = var.common_vm_version
vm_cpu_cores = var.vm_cpu_cores
vm_cpu_count = var.vm_cpu_count
vm_disk_size = var.vm_disk_size
vm_disk_thin_provisioned = var.vm_disk_thin_provisioned
vm_firmware = var.vm_firmware
vm_guest_os_type = var.vm_guest_os_type
vm_mem_size = var.vm_mem_size
vm_network_card = var.vm_network_card
vsphere_cluster = var.vsphere_cluster
vsphere_host = var.vsphere_host
vsphere_datacenter = var.vsphere_datacenter
vsphere_datastore = var.vsphere_datastore
vsphere_endpoint = var.vsphere_endpoint
vsphere_folder = var.vsphere_folder
}
}
dynamic "hcp_packer_registry" {
for_each = var.common_hcp_packer_registry_enabled ? [1] : []
content {
bucket_name = local.bucket_name
description = local.bucket_description
bucket_labels = {
"os_family" : var.vm_guest_os_family,
"os_name" : var.vm_guest_os_name,
"os_version" : var.vm_guest_os_version,
}
build_labels = {
"build_version" : local.build_version,
"packer_version" : packer.version,
}
}
}
}