-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathtemplate.pkr.hcl
executable file
·961 lines (867 loc) · 40.6 KB
/
template.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
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
#!/usr/bin/env packer build --force
#
# Author: Hari Sekhon
# Date: [% DATE # 2023-05-28 15:50:29 +0100 (Sun, 28 May 2023) %]
#
# vim:ts=2:sts=2:sw=2:et:filetype=conf
#
# https://github.com/HariSekhon/Templates
#
# License: see accompanying Hari Sekhon LICENSE file
#
# If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish
#
# https://www.linkedin.com/in/HariSekhon
#
# For real-world builds see this repo:
#
# https://github.com/HariSekhon/Packer
# Uses adjacent Redhat Kickstart, Debian Preseed & Ubuntu Autoinstaller
#
# 'packer' command must be run from the same directory as the provided adjacent files which are auto-served via HTTP:
#
# - Redhat Kickstart - anaconda-ks.cfg
# - Debian Preseed - preseed.cfg
# - Ubuntu AutoInstaller - autoinstall-user-data and meta-data files
# ============================================================================ #
# H a s h i C o r p P a c k e r
# ============================================================================ #
packer {
# Data sources only available in 1.7+
required_version = ">= 1.7.0, < 2.0.0"
required_plugins {
virtualbox = {
version = "~> 0.0"
source = "github.com/hashicorp/virtualbox"
}
#amazon = {
# version = "~> 1.2"
# source = "github.com/hashicorp/amazon"
#}
#googlecompute = {
# version = "~> 1.1"
# source = "github.com/hashicorp/googlecompute"
#}
#docker = {
# version = "~> 0.0"
# source = "github.com/hashicorp/docker"
#}
#vmware = {
# version = "~> 1.0"
# source = "github.com/hashicorp/vmware"
#}
# used to build remotely on ESXi
#vsphere = {
# version = "~> 1.1"
# source = "github.com/hashicorp/vsphere"
#}
qemu = {
version = "~> 1.1"
source = "github.com/hashicorp/qemu"
}
}
}
# ============================================================================ #
# L o c a l V a r i a b l e s
# ============================================================================ #
# https://developer.hashicorp.com/packer/docs/templates/hcl_templates/locals
# locals are like constants and unlike variables cannot be overridden at runtime
# can't be used within data sources as of Packer 1.8
locals {
foo = "bar"
#baz = "Foo is '${var.foo}'"
#default_name_prefix = "${var.project_name}-web"
#name_prefix = "${var.name_prefix != "" ? var.name_prefix : local.default_name_prefix}"
settings_file = "${path.cwd}/settings.txt" # path.cwd = 'packer' commands's $PWD
scripts = "${path.root}/scripts" # path.root = dirname(file.pkr.hcl)
root = path.root
# locals can access data sources but data sources cannot access locals, to prevent circular dependencies
#source_ami_id = data.amazon-ami.ubuntu.id
#source_ami_name = data.amazon-ami.ubuntu.name
#value = data.amazon-secretsmanager.NAME.value
#secret_string = data.amazon-secretsmanager.NAME.secret_string
#version_id = data.amazon-secretsmanager.NAME.version_id
#secret_value = jsondecode(data.amazon-secretsmanager.NAME.secret_string)["packer_test_key"]
# for Cloud builds for auditing and Cost Explorer breakdowns
tags = {
App = "MyApp" # XXX: Edit and add relevant tags
Environment = "Production"
BuildDate = "${timestamp()}"
}
# requires AWS profile / access key to be found, else errors out
#
# set second arg to the key if secret had multiple keys, else set to null
#secret = aws_secretsmanager("my_secret", null) # always pulls latest version AWSCURRENT, previous versions not supported
#my_version = "${consul_key("myservice/version")}"
# requires VAULT_TOKEN and VAULT_ADDR environment variables to be set
#
#foo2 = vault("/secret/data/hello", "foo")
}
# Use the singular local block if you need to mark a local as sensitive
local "mylocal" {
expression = "${var.docker_image}"
sensitive = true
}
# ============================================================================ #
# U s e r V a r i a b l e s
# ============================================================================ #
# https://developer.hashicorp.com/packer/docs/templates/hcl_templates/variables
# -var-files values.pkrvars.hcl containing foo = "value"
# *.auto.pkrvars.hcl
# PKR_VAR_foo=bar
variable "foo" {
type = string
default = "inline_bar"
}
variable "docker_image" {
type = string
default = "ubuntu:22.04"
#description = ""
#sensitive = true
}
variable "aws_region" {
default = env("AWS_DEFAULT_REGION")
}
#variable "http_proxy" {
# default = env("HTTP_PROXY")
#}
#
#variable "https_proxy" {
# default = env("HTTPS_PROXY")
#}
#
#variable "no_proxy" {
# default = env("NO_PROXY")
#}
variable "availability_zone_names" {
type = list(string)
default = [
"eu-west-2a",
"eu-west-2b",
"eu-west-2c"
]
}
variable "docker_ports" {
type = list(object({
internal = number
external = number
protocol = string
}))
default = [
{
internal = 8300
external = 8300
protocol = "tcp"
}
]
}
variable "image_id" {
type = string
default = "ami-....."
description = "The ID of the machine image (AMI) to use for the server."
validation {
condition = length(var.image_id) > 4 && substr(var.image_id, 0, 4) == "ami-"
error_message = "The image_id value must be a valid AMI ID, starting with \"ami-\"."
}
validation {
# regex(...) fails if it cannot find a match
condition = can(regex("^ami-", var.image_id))
error_message = "The image_id value must be a valid AMI ID, starting with \"ami-\"."
}
}
# complex data type and validation
variable "image_metadata" {
default = {
key : "value",
something : {
foo : "bar",
}
}
validation {
condition = length(var.image_metadata.key) > 4
error_message = "The image_metadata.key field must be more than 4 runes."
}
validation {
condition = can(var.image_metadata.something.foo)
error_message = "The image_metadata.something.foo field must exist."
}
validation {
condition = substr(var.image_metadata.something.foo, 0, 3) == "bar"
error_message = "The image_metadata.something.foo field must start with \"bar\"."
}
}
# ============================================================================ #
# D a t a S o u r c e s
# ============================================================================ #
# https://developer.hashicorp.com/packer/docs/templates/hcl_templates/datasources
# locals can access data sources but data sources cannot access locals, to prevent circular dependencies
# https://developer.hashicorp.com/packer/plugins/datasources/external/external
# https://developer.hashicorp.com/packer/integrations/hashicorp/amazon
# https://developer.hashicorp.com/packer/integrations/hashicorp/amazon/latest/components/data-source/ami
#data "amazon-ami" "ubuntu" {
# filters = {
# virtualization-type = "hvm"
# name = "ubuntu/images/*ubuntu-xenial-16.04-amd64-server-*"
# root-device-type = "ebs"
# }
# owners = ["099720109477"]
# most_recent = true
#}
# https://developer.hashicorp.com/packer/integrations/hashicorp/amazon/latest/components/data-source/secretsmanager
#data "amazon-secretsmanager" "NAME" {
# name = "packer_test_secret"
# key = "packer_test_key"
# version_stage = "example"
#}
# foo = data.http.NAME.body
#data "http" "NAME" {
# url = "https://checkpoint-api.hashicorp.com/v1/check/terraform"
#
# # Optional request headers
# request_headers = {
# Accept = "application/json"
# }
#}
# ============================================================================ #
# S o u r c e s
# ============================================================================ #
# Create multiple sources to build near identical images for different platforms
# Debian's installer is the fastest and most lightweight
# Ubuntu and Fedora are allocated more resources to try to prevent them stalling / crashing
# Fedora's anaconda installer is the most resource hungry and unrealiable when resource constrained
# https://developer.hashicorp.com/packer/integrations/hashicorp/qemu/latest/components/builder/qemu
source "qemu" "ubuntu" {
vm_name = "ubuntu" # name of the OVF file without the extension, default: packer-<buildname>-<epoch> eg. packer-ubuntu-1685455208
qemu_binary = "qemu-system-x86_64" # default: qemu-system-x86_64, change for ARM eg. Mac M1/M2
#use_default_display = true # might be needed on Mac to avoid errors about sdl not being available
accelerator = "kvm"
# VBoxManage list ostypes
#guest_os_type = "Ubuntu22_LTS_64"
#guest_os_type = "Ubuntu_64"
# Browse to http://releases.ubuntu.com/ and pick the latest LTS release
iso_url = "http://releases.ubuntu.com/jammy/ubuntu-22.04.2-live-server-amd64.iso"
iso_checksum = "5e38b55d57d94ff029719342357325ed3bda38fa80054f9330dc789cd2d43931"
# ARM
#iso_url = "https://cdimage.ubuntu.com/releases/22.04/release/ubuntu-22.04.2-live-server-arm64.iso"
#iso_checksum = "12eed04214d8492d22686b72610711882ddf6222b4dc029c24515a85c4874e95"
#cpus = 3 # default: 1
memory = 3072 # MB, default: 512 - too low RAM results in 'Kernel panic - not syncing: No working init found.'
disk_size = 40960 # default: 40960, uses MB if no suffix, but suffixes cause this comment to fail validation with this error: 'An argument definition must end with a newline.'
disk_additional_size = [] # add MiB sizes, disks will be called ${vm_name}-# where # is the incrementing integer
http_directory = "." # necessary for the user-data to be served out for autoinstall boot_command
#http_directory = "${path.root}" # doesn't work
# https://developer.hashicorp.com/packer/integrations/hashicorp/virtualbox/latest/components/builder/iso#boot-configuration
boot_wait = "5s" # default: 10s
boot_command = [
#"<tab><wait>",
#"ks=http://{{ .HTTPIP }}:{{ .HTTPPort }}/centos8-ks.cfg<enter>"
#"autoinstall ds=nocloud-net;s=http://{{ .HTTPIP }}:{{ .HTTPPort }}/ <enter>"
"c<wait>",
# XXX: must single quotes the ds=... arg to prevent grub from interpreting the semicolon as a terminator
# https://cloudinit.readthedocs.io/en/latest/reference/datasources/nocloud.html
"linux /casper/vmlinuz autoinstall 'ds=nocloud-net;s=http://{{.HTTPIP}}:{{.HTTPPort}}/' <enter><wait>",
"initrd /casper/initrd <enter><wait>",
"boot <enter>"
]
# =====================================
# Debug AutoInstaller failure to launch
# XXX: need to set communicator to none to buy time to default autoinstaller issues otherwise default SSHd gets started and Packer creds get rejected so kills the VM
#communicator = "none" # doesn't work to to allow a first manual install to collect /var/log/installer/autoinstall-user-data, must instead use -debug
#disable_shutdown = true
#shutdown_timeout = "2h" # prevent the VM from being killed after 5 mins waiting for shutdown
#guest_additions_mode = "disable" # must be disabled when using communicator = 'none'
#virtualbox_version_file = "" # must be an empty string when using communicator = 'none'
# =====================================
#guest_additions_mode = "upload"
#guest_additions_path = "VBoxGuestAdditions.iso"
# doesn't work to set this higher to allow a first manual install to collect /var/log/installer/autoinstall-user-data
# gets an SSH authentication error a couple minutes in and kills the VM regardless
ssh_timeout = "30m" # default: 5m - waits 5 mins for SSH to come up otherwise kills VM
ssh_username = "packer"
ssh_password = "packer"
# ensure filesystem is fsync'd
shutdown_command = "echo 'packer' | sudo -S shutdown -P now"
net_device = "virtio-net"
disk_interface = "virtio"
#format = "qcow2"
format = "ova"
#disk_compression = true # default: false
#rtc_time_base = "UTC"
#bundle_iso = false # keep the ISO attached
qemuargs = []
#output_directory = "" # default: 'output-BUILDNAME', must not already exist
#output_filename = "" # default: '${vm_name}'
}
# ============================================================================ #
# V i r t u a l B o x S o u r c e s
# ============================================================================ #
# WARNING: XXX: Do not build on ARM M1/M2 Macs using VirtualBox 7.0 - as of 2023 VirtualBox 7.0 Beta is extremely buggy, slow,
# results in "Aborted" VMs and so slow it even misses bootloader keystrokes - it is unworkable on ARM as of this date
# https://developer.hashicorp.com/packer/integrations/hashicorp/virtualbox/latest/components/builder/iso
source "virtualbox-iso" "ubuntu" {
vm_name = "ubuntu" # name of the OVF file without the extension, default: packer-<buildname>-<epoch> eg. packer-ubuntu-1685455208
# VBoxManage list ostypes
#guest_os_type = "Ubuntu22_LTS_64"
guest_os_type = "Ubuntu_64"
# Browse to http://releases.ubuntu.com/ and pick the latest LTS release
iso_url = "http://releases.ubuntu.com/jammy/ubuntu-22.04.2-live-server-amd64.iso"
iso_checksum = "5e38b55d57d94ff029719342357325ed3bda38fa80054f9330dc789cd2d43931"
# ARM - DO NOT USE EVEN ON M1/M2 Macs - as of VirtualBox 7.0 in 2023 it seems to be emulating x86_64, not arm64, and results in the following error:
#
# Could not read from the boot medium!
# Please insert a bootable medium and reboot.
#
# Switching back to x86_64 works but is even slower than on an older Intel Mac, probably due to the software emulation, and often ends up aborting due to the Beta nature of the 7.0 Virtualbox release for arm
#
#iso_url = "https://cdimage.ubuntu.com/releases/22.04/release/ubuntu-22.04.2-live-server-arm64.iso"
#iso_checksum = "12eed04214d8492d22686b72610711882ddf6222b4dc029c24515a85c4874e95"
cpus = 3 # default: 1
memory = 3072 # MB, default: 512 - too low RAM results in 'Kernel panic - not syncing: No working init found.'
disk_size = 40000 # default: 40000 MB = around 40GB
disk_additional_size = [] # add MiB sizes, disks will be called ${vm_name}-# where # is the incrementing integer
http_directory = "." # necessary for the user-data to be served out for autoinstall boot_command
#http_directory = "${path.root}" # doesn't work
# https://developer.hashicorp.com/packer/integrations/hashicorp/virtualbox/latest/components/builder/iso#boot-configuration
boot_wait = "5s" # default: 10s
boot_command = [
#"<tab><wait>",
#"ks=http://{{ .HTTPIP }}:{{ .HTTPPort }}/centos8-ks.cfg<enter>"
#"autoinstall ds=nocloud-net;s=http://{{ .HTTPIP }}:{{ .HTTPPort }}/ <enter>"
"c<wait>",
# XXX: must single quotes the ds=... arg to prevent grub from interpreting the semicolon as a terminator
# https://cloudinit.readthedocs.io/en/latest/reference/datasources/nocloud.html
"linux /casper/vmlinuz autoinstall 'ds=nocloud-net;s=http://{{.HTTPIP}}:{{.HTTPPort}}/' <enter><wait>",
"initrd /casper/initrd <enter><wait>",
"boot <enter>"
]
# =====================================
# Debug AutoInstaller failure to launch
# XXX: need to set communicator to none to buy time to default autoinstaller issues otherwise default SSHd gets started and Packer creds get rejected so kills the VM
#communicator = "none" # doesn't work to to allow a first manual install to collect /var/log/installer/autoinstall-user-data, must instead use -debug
#disable_shutdown = true
#shutdown_timeout = "2h" # prevent the VM from being killed after 5 mins waiting for shutdown
#guest_additions_mode = "disable" # must be disabled when using communicator = 'none'
#virtualbox_version_file = "" # must be an empty string when using communicator = 'none'
# =====================================
#guest_additions_mode = "upload"
#guest_additions_path = "VBoxGuestAdditions.iso"
# doesn't work to set this higher to allow a first manual install to collect /var/log/installer/autoinstall-user-data
# gets an SSH authentication error a couple minutes in and kills the VM regardless
ssh_timeout = "30m" # default: 5m - waits 5 mins for SSH to come up otherwise kills VM
ssh_username = "packer"
ssh_password = "packer"
# ensure filesystem is fsync'd
shutdown_command = "echo 'packer' | sudo -S shutdown -P now"
rtc_time_base = "UTC"
#virtualbox_version_file = ".vbox_version" # file created in $HOME directory to indicate which version of VirtualBox created this
bundle_iso = false # keep the ISO attached
# extra CLI customization
vboxmanage = [
#["modifyvm", "{{.Name}}", "--cpus", "2"],
#["modifyvm", "{{.Name}}", "--memory", "1024"],
# Error executing command: VBoxManage error: VBoxManage: error: Machine 'NAME' is not currently running.
# do this in shell-local later
#["sharedfolder", "add", "{{.Name}}", "--name", "vboxsf", "--hostpath", "~/vboxsf", "--automount", "--transient"],
["modifyvm", "{{.Name}}", "--nat-localhostreachable1", "on"], # XXX: workaround for auto-installer hanging at AppArmour Load
# doesn't work - just set global preferences to 300% scale
#["setextradata", "{{.Name}}", "CustomVideoMode1", "1400x1050x16"] # make resolution bigger
#["controlvm", "{{.Name}}", "setvideomodehint", "1400", "1050", "32"] # doesn't work because VM isn't running at this point, and would require a reboot
]
export_opts = [
"--manifest",
"--vsys", "0",
#"--description", "${var.vm_description}", # create variables if uncommenting these
#"--version", "${var.vm_version}"
]
format = "ova"
#output_directory = "" # default: 'output-BUILDNAME', must not already exist
#output_filename = "" # default: '${vm_name}'
}
# https://developer.hashicorp.com/packer/integrations/hashicorp/virtualbox/latest/components/builder/iso
source "virtualbox-iso" "debian" {
vm_name = "debian"
guest_os_type = "Debian_64"
# https://www.debian.org/CD/http-ftp/
iso_url = "https://cdimage.debian.org/debian-cd/current/amd64/iso-dvd/debian-11.7.0-amd64-DVD-1.iso" # 4.7GB
iso_checksum = "cfbb1387d92c83f49420eca06e2d11a23e5a817a21a5d614339749634709a32f"
#iso_url = "https://cdimage.debian.org/debian-cd/current/amd64/iso-cd/debian-11.7.0-amd64-netinst.iso" # 300MB
#iso_checksum = "eb3f96fd607e4b67e80f4fc15670feb7d9db5be50f4ca8d0bf07008cb025766b"
# ARM - DO NOT USE EVEN ON M1/M2 Macs - as of VirtualBox 7.0 in 2023 it seems to be emulating x86_64, not arm64, and results in the following error:
#
# Could not read from the boot medium!
# Please insert a bootable medium and reboot.
#
# Switching back to x86_64 works but is even slower than on an older Intel Mac, probably due to the software emulation, and often ends up aborting due to the Beta nature of the 7.0 Virtualbox release for arm
#
#iso_url = "https://cdimage.debian.org/debian-cd/current/arm64/iso-dvd/debian-11.7.0-arm64-DVD-1.iso" # 4.7GB
#iso_checksum = "3b0d304379b671d7b7091631765f87e1cbb96b9f03f8e9a595a2bf540c789f3f"
#iso_url = "https://cdimage.debian.org/debian-cd/current/arm64/iso-cd/debian-11.7.0-arm64-netinst.iso" # 300MB
#iso_checksum = "174caba674fe3172938439257156b9cb8940bb5fd5ddf124256e81ec00ec460d"
cpus = 2 # default: 1
memory = 2048 # MB, default: 512 - too low RAM results in 'Kernel panic - not syncing: No working init found.'
disk_size = 40000 # default: 40000 MB = around 40GB
disk_additional_size = [] # add MiB sizes, disks will be called ${vm_name}-# where # is the incrementing integer
http_directory = "." # necessary for the user-data to be served out for autoinstall boot_command
# https://developer.hashicorp.com/packer/integrations/hashicorp/virtualbox/latest/components/builder/iso#boot-configuration
boot_wait = "5s" # default: 10s
# Aliases useful with preseeding
# https://www.debian.org/releases/stable/amd64/apbs02.en.html
boot_command = [
"<down><wait>",
"<tab><wait>",
# preseed-md5=... add later
"fb=true auto=true url=http://{{.HTTPIP}}:{{.HTTPPort}}/preseed.cfg hostname={{.Name}} domain=local <enter>"
]
ssh_timeout = "30m" # default: 5m - waits 5 mins for SSH to come up otherwise kills VM
ssh_username = "packer"
ssh_password = "packer"
shutdown_command = "echo 'packer' | sudo -S shutdown -P now"
rtc_time_base = "UTC"
bundle_iso = false # keep the ISO attached
vboxmanage = [
["modifyvm", "{{.Name}}", "--nat-localhostreachable1", "on"], # XXX: can't access host HTTP server for preseed.cfg file without this
]
export_opts = [
"--manifest",
"--vsys", "0",
#"--description", "${var.vm_description}", # create variables if uncommenting these
#"--version", "${var.vm_version}"
]
format = "ova"
#output_directory = "" # default: 'output-BUILDNAME', must not already exist
#output_filename = "" # default: '${vm_name}'
}
# https://developer.hashicorp.com/packer/integrations/hashicorp/virtualbox/latest/components/builder/iso
source "virtualbox-iso" "fedora" {
vm_name = "fedora"
guest_os_type = "Fedora_64"
# https://alt.fedoraproject.org/alt/
iso_url = "https://download.fedoraproject.org/pub/fedora/linux/releases/38/Server/x86_64/iso/Fedora-Server-dvd-x86_64-38-1.6.iso"
iso_checksum = "09dee2cd626a269aefc67b69e63a30bd0baa52d4"
# ARM - DO NOT USE EVEN ON M1/M2 Macs - as of VirtualBox 7.0 in 2023 it seems to be emulating x86_64, not arm64, and results in the following error:
#
# Could not read from the boot medium!
# Please insert a bootable medium and reboot.
#
# Switching back to x86_64 works but is even slower than on an older Intel Mac, probably due to the software emulation, and often ends up aborting due to the Beta nature of the 7.0 Virtualbox release for arm
#
#iso_url = "https://download.fedoraproject.org/pub/fedora/linux/releases/38/Server/aarch64/iso/Fedora-Server-dvd-aarch64-38-1.6.iso" # 2.8GB
#iso_checksum = "4cdf077eddaeedf1180cdf3e14213da2abc10ceb"
cpus = 3 # default: 1
memory = 3072 # MB, default: 512 - too low RAM results in 'Kernel panic - not syncing: No working init found.'
disk_size = 40000 # default: 40000 MB = around 40GB
disk_additional_size = [] # add MiB sizes, disks will be called ${vm_name}-# where # is the incrementing integer
http_directory = "." # necessary for the user-data to be served out for autoinstall boot_command
# https://developer.hashicorp.com/packer/integrations/hashicorp/virtualbox/latest/components/builder/iso#boot-configuration
boot_wait = "5s" # default: 10s
# trigger GUI install - mouse isn't working, move to text-mode first install to collect a baseline anaconda-ks.cfg
#boot_command = [
# "<up><wait><enter>",
#]
# trigger text mode install
#boot_command = [
# "<up><wait>",
# "e",
# "<down><down><down><left>",
# # leave a space from last arg
# " inst.text <f10>" # trigger text mode install
#]
# Kickstart automated install
boot_command = [
"<up><wait>",
"e",
"<down><down><down><left>",
# leave a space from last arg
" inst.ks=http://{{.HTTPIP}}:{{.HTTPPort}}/anaconda-ks.cfg <f10>"
]
ssh_timeout = "30m" # default: 5m - waits 5 mins for SSH to come up otherwise kills VM
ssh_username = "packer"
ssh_password = "packer"
shutdown_command = "echo 'packer' | sudo -S shutdown -P now"
rtc_time_base = "UTC"
bundle_iso = false # keep the ISO attached
vboxmanage = [
["modifyvm", "{{.Name}}", "--nat-localhostreachable1", "on"], # XXX: can't access host HTTP server for kickstart file without this
]
export_opts = [
"--manifest",
"--vsys", "0",
#"--description", "${var.vm_description}", # create variables if uncommenting these
#"--version", "${var.vm_version}"
]
format = "ova"
#output_directory = "" # default: 'output-BUILDNAME', must not already exist
#output_filename = "" # default: '${vm_name}'
}
# ============================================================================ #
# https://developer.hashicorp.com/packer/integrations/hashicorp/virtualbox/latest/components/builder/ovf
#source "virtualbox-ovf" "ubuntu" {
# vm_name = "ubuntu"
# source_path = "source.ovf"
# ssh_username = "packer
# ssh_password = "packer"
# shutdown_command = "echo 'packer' | sudo -S shutdown -P now"
# virtualbox_version_file = ".vbox_version" # file created in $HOME directory to indicate which version of VirtualBox created this
# guest_additions_mode = "upload"
# guest_additions_path = "VBoxGuestAdditions.iso"
# # extra CLI customization
# #vboxmanage = [
# # ["modifyvm", "{{.Name}}", "--cpus", "2"],
# # ["modifyvm", "{{.Name}}", "--memory", "1024"],
# #]
# export_opts = [
# "--manifest",
# "--vsys", "0",
# #"--description", "${var.vm_description}", # create variables if uncommenting these
# #"--version", "${var.vm_version}"
# ]
# format = "ova"
# #output_directory = "" # default: 'output-BUILDNAME', must not already exist
# #output_filename = "" # default: '${vm_name}'
#}
# https://developer.hashicorp.com/packer/integrations/hashicorp/docker/latest/components/builder/docker
#source "docker" "NAME" {
# image = var.docker_image
# commit = true
#}
# https://developer.hashicorp.com/packer/integrations/hashicorp/vagrant/latest/components/builder/vagrant
#source "vagrant" "ubuntu" {
# source_path = "hashicorp/precise64"
# provider = "virtualbox"
#}
# https://developer.hashicorp.com/packer/integrations/hashicorp/amazon
# https://developer.hashicorp.com/packer/integrations/hashicorp/amazon/latest/components/data-source/ami
# https://developer.hashicorp.com/packer/integrations/hashicorp/amazon/latest/components/builder/ebs
#source "amazon-ebs" "NAME" {
# source_ami = locals.source_ami
# // ...
#}
# ============================================================================ #
# B u i l d
# ============================================================================ #
# https://developer.hashicorp.com/packer/integrations/hashicorp/virtualbox
# https://developer.hashicorp.com/packer/integrations/hashicorp/vagrant/latest/components/builder/vagrant
# https://developer.hashicorp.com/packer/integrations/hashicorp/vagrant/latest/components/post-processor/vagrant
# https://developer.hashicorp.com/packer/integrations/hashicorp/amazon
# https://developer.hashicorp.com/packer/integrations/hashicorp/azure
# https://developer.hashicorp.com/packer/integrations/hashicorp/googlecompute/latest/components/builder/googlecompute
# https://developer.hashicorp.com/packer/integrations/hashicorp/vmware
# https://developer.hashicorp.com/packer/integrations/hashicorp/vsphere/latest/components/builder/vsphere-iso
build {
name = "ubuntu"
# specify multiple sources defined above to build near identical images for different platforms
sources = [
"source.virtualbox-iso.ubuntu",
#"sources.virtualbox-ovf.ubuntu"
]
provisioner "file" {
source = "/var/log/installer/autoinstall-user-data"
# if you let this overwrite the real one, it'l break subsequent runs like so because it'll remove the early commands to stop the SSHd server daemon during installer:
# Error waiting for SSH: Packer experienced an authentication error when trying to connect via SSH. This can happen if your username/password are wrong. You may want to double-check your credentials as part of your debugging process. original error: ssh: handshake failed: ssh: unable to authenticate, attempted methods [none password], no supported methods remain
destination = "autoinstall-user-data.new"
direction = "download"
}
# doesn't help to put a breakpoint here because Packer insists on testing for SSH and as soon as it gets auth rejected destroys the VM anyway
#provisioner "breakpoint" {
# disable = false
# note = "this is a breakpoint to be able to inspect the VM contents"
#}
# https://developer.hashicorp.com/packer/docs/provisioners/file
#
#provisioner "file" {
# source = "app.tar.gz"
# destination = "/tmp/app.tar.gz"
#}
#
#provisioner "file" {
# sources = ["file1.txt", "file2.txt"]
# destination = "/etc/conf/"
#}
# Packer doesn't have permissions to make changes to root owned paths - do it via shell sudo
#
# Upload failed: scp: /etc/packer-version: Permission denied
#provisioner "file" {
# content = "Built using Packer version '${packer.version}'"
# destination = "/etc/packer-version"
#}
# https://developer.hashicorp.com/packer/integrations/hashicorp/ansible/latest/components/provisioner/ansible
#
#provisioner "ansible" {
# playbook_file = "./playbook.yml"
# ansible_env_vars = [
# "ANSIBLE_HOST_KEY_CHECKING=False",
# "ANSIBLE_SSH_ARGS='-o ForwardAgent=yes -o ControlMaster=auto -o ControlPersist=60s'",
# #"ANSIBLE_NOCOLOR=True"
# ]
#}
# https://developer.hashicorp.com/packer/docs/provisioners/breakpoint
#
# when -debug switch this pauses to be able to debug the build
#
#provisioner "breakpoint" {
# disable = false
# note = "this is a breakpoint to be able to inspect the VM contents"
#}
# https://developer.hashicorp.com/packer/docs/provisioners/shell-local
#
provisioner "shell-local" {
# https://developer.hashicorp.com/packer/docs/templates/hcl_templates/contextual-variables
# can access:
# source variables name and type
# build variables name, ID, Host, Port, User and Password (for passing to Ansible),
# ConnType (eg. "ssh"), SSHPublicKey, SSHPrivateKey, PackerRunUUID,
# PackerHTTPIP / PackerHTTPPort / PackerHTTPAddr (IP:PORT) of the http file server packer runs to serve files to the VM
inline = [
"env | grep PACKER || :",
"echo Build UUID ${build.PackerRunUUID}",
"echo Source '${source.name}' type '${source.type}'",
"echo Creating ~/vboxsf",
"mkdir -p -v ~/vboxsf",
"echo Adding shared folder to VM",
# errors out with 'line 11: no: No such file or directory'
#"VBoxManage sharedfolder add {{.Name}} --name vboxsf --hostpath ~/vboxsf --automount --transient",
#"VBoxManage sharedfolder add ubuntu --name vboxsf --hostpath ~/vboxsf --automount --transient",
"VBoxManage sharedfolder add $PACKER_BUILD_NAME --name vboxsf --hostpath ~/vboxsf --automount --transient",
]
# some common scripts found in https://github.com/HariSekhon/Packer
#script = "${local.scripts}/local_virtiofs.sh"
#script = "${local.scripts}/local_vboxsf.sh"
#environment_vars = [
# "HTTP_PROXY=${var.http_proxy}",
# "HTTPS_PROXY=${var.https_proxy}",
# "NO_PROXY=${var.no_proxy}"
#]
}
# https://developer.hashicorp.com/packer/docs/provisioners/shell
#
provisioner "shell" {
#script = "/path/to/script.sh"
#script = "./script.sh"
scripts = [
#"/path/to/script.sh",
#"./script.sh",
"${local.scripts}/script.sh",
# some common scripts found in https://github.com/HariSekhon/Packer
"${local.scripts}/version.sh",
#
#"${local.scripts}/install_vbox_additions.sh",
#"${local.scripts}/mount_vboxsf.sh",
#
#"${local.scripts}/mount_apple_virtiofs.sh",
#
#"${local.scripts}/collect_anaconda.sh",
#"${local.scripts}/collect_preseed.sh",
"${local.scripts}/collect_autoinstall_user_data.sh",
"${local.scripts}/final.sh"
]
environment_vars = [
"FOO=bar",
#"HTTP_PROXY=${var.http_proxy}",
#"HTTPS_PROXY=${var.https_proxy}",
#"NO_PROXY=${var.no_proxy}"
]
# overriding execute_command you must manually put {{ .Vars }} prefixed in the command otherwise you'll lose environment_vars
execute_command = "{{ .Vars }} echo 'packer' | sudo -S -E bash -euxo pipefail '{{ .Path }}'"
#inline = [
# "env",
# # pre-authorize sudo - or run whole shell as root using execute_command above
# #"echo 'packer' | sudo -S echo",
# "echo Built using Packer version '${packer.version}' | sudo tee /etc/packer-version",
# #"echo '${build.SSHPrivateKey}' > /tmp/packer-session.pem", # temporary SSH private key eg. git clone a private repo [email protected]:org/repo
# "sudo mkdir -pv /mnt/vboxsf",
# "echo Mounting /mnt/vboxsf",
# "sudo mount -t vboxsf vboxsf /mnt/vboxsf",
# # mount point is owned by root
# "sudo cp -fv /var/log/installer/autoinstall-user-data /mnt/vboxsf/",
#]
# max_retries = 5
# timeout = "5m"
}
# post-processors (plural) creates a serial post-processing where one post-processor's output is the next one's input
#post-processors {
# # vagrant post-processor cannot be used with vagrant builder as it'll clash
# post-processor "vagrant" {}
# post-processor "compress" {}
#}
# post-processor blocks run in parallel
#
post-processor "checksum" { # checksum image
checksum_types = ["md5", "sha512"] # checksum the artifact
keep_input_artifact = true # keep the artifact
output = "output-{{.BuildName}}/{{.BuildName}}.{{.ChecksumType}}" # default: packer_{{.BuildName}}_{{.BuilderType}}_{{.ChecksumType}}.checksum, at top level not in the directory with the .ova, and it keeps appending to it
}
# https://developer.hashicorp.com/packer/integrations/hashicorp/docker/latest/components/post-processor/docker-tag
#post-processor "docker-tag" {
# repository = "myrepo" # XXX: Edit
# tags = ["ubuntu", "mytag"]
# only = ["docker.ubuntu"]
#}
#post-processor "docker-tag" {
# repository = "learn-packer"
# tags = ["ubuntu-othersource", "packer-rocks"]
# only = ["docker.othersource"]
#}
# happen in serial
#post-processors {
# post-processor "docker-import" {
# repository = "swampdragons/testpush"
# tag = "0.7"
# }
# # https://developer.hashicorp.com/packer/integrations/hashicorp/docker/latest/components/post-processor/docker-push
# post-processor "docker-push" {}
#}
}
build {
name = "debian"
sources = ["source.virtualbox-iso.debian"]
# unable to generate this during install - see adjacent file preseed.cfg for details
#provisioner "file" {
# source = "/var/log/preseed.cfg"
# destination = "preseed.cfg.new"
# direction = "download"
#}
# https://developer.hashicorp.com/packer/docs/provisioners/shell-local
#
provisioner "shell-local" {
inline = [
"env | grep PACKER || :",
"echo Build UUID: ${build.PackerRunUUID}",
"echo Source '${source.name}' type '${source.type}'",
"echo Creating ~/vboxsf",
"mkdir -p -v ~/vboxsf",
"echo Adding shared folder to VM",
#"VBoxManage sharedfolder add debian --name vboxsf --hostpath ~/vboxsf --automount --transient",
"VBoxManage sharedfolder add $PACKER_BUILD_NAME --name vboxsf --hostpath ~/vboxsf --automount --transient",
]
# some common scripts found in https://github.com/HariSekhon/Packer
#script = "${local.scripts}/local_virtiofs.sh"
#script = "${local.scripts}/local_vboxsf.sh"
}
# https://developer.hashicorp.com/packer/docs/provisioners/shell
#
provisioner "shell" {
# overriding execute_command you must manually put {{ .Vars }} prefixed in the command otherwise you'll lose environment_vars
execute_command = "{{ .Vars }} echo 'packer' | sudo -S -E bash -euxo pipefail '{{ .Path }}'"
#inline = [
# "env",
# # pre-authorize sudo - or run whole shell as root using execute_command above
# #"echo 'packer' | sudo -S echo",
# "echo Built using Packer version '${packer.version}' | sudo tee /etc/packer-version",
# #"echo '${build.SSHPrivateKey}' > /tmp/packer-session.pem", # temporary SSH private key eg. git clone a private repo [email protected]:org/repo
# "sudo mkdir -pv /mnt/vboxsf",
# "echo Mounting /mnt/vboxsf",
# "sudo mount -t vboxsf vboxsf /mnt/vboxsf",
# # mount point is owned by root
# # preseed.cfg is not created - see adjacent file preseed.cfg for details
# #"sudo cp -fv /var/log/preseed.cfg /mnt/vboxsf/",
#]
scripts = [
# some common scripts found in https://github.com/HariSekhon/Packer
"${local.scripts}/version.sh",
#
#"${local.scripts}/install_vbox_additions.sh",
#"${local.scripts}/mount_vboxsf.sh",
#
#"${local.scripts}/mount_apple_virtiofs.sh",
#
#"${local.scripts}/collect_anaconda.sh",
"${local.scripts}/collect_preseed.sh",
#"${local.scripts}/collect_autoinstall_user_data.sh",
"${local.scripts}/final.sh"
]
# max_retries = 5
# timeout = "5m"
#environment_vars = [
# "HTTP_PROXY=${var.http_proxy}",
# "HTTPS_PROXY=${var.https_proxy}",
# "NO_PROXY=${var.no_proxy}"
#]
}
# post-processor blocks run in parallel
#
post-processor "checksum" { # checksum image
checksum_types = ["md5", "sha512"] # checksum the artifact
keep_input_artifact = true # keep the artifact
output = "output-{{.BuildName}}/{{.BuildName}}.{{.ChecksumType}}" # default: packer_{{.BuildName}}_{{.BuilderType}}_{{.ChecksumType}}.checksum, at top level not in the directory with the .ova, and it keeps appending to it
}
}
build {
name = "fedora"
sources = ["source.virtualbox-iso.fedora"]
# Packer gets permission denied, get it via sudo in shell
#provisioner "file" {
# source = "/root/anaconda-ks.cfg"
# destination = "anaconda-ks.cfg.new"
# direction = "download"
#}
# https://developer.hashicorp.com/packer/docs/provisioners/shell-local
#
provisioner "shell-local" {
inline = [
"env | grep PACKER || :",
"echo Build UUID ${build.PackerRunUUID}",
"echo Source '${source.name}' type '${source.type}'",
"echo Creating ~/vboxsf",
"mkdir -p -v ~/vboxsf",
"echo Adding shared folder to VM",
#"VBoxManage sharedfolder add fedora --name vboxsf --hostpath ~/vboxsf --automount --transient",
"VBoxManage sharedfolder add $PACKER_BUILD_NAME --name vboxsf --hostpath ~/vboxsf --automount --transient",
]
# some common scripts found in https://github.com/HariSekhon/Packer
#script = "${local.scripts}/local_virtiofs.sh"
#script = "${local.scripts}/local_vboxsf.sh"
#environment_vars = [
# "HTTP_PROXY=${var.http_proxy}",
# "HTTPS_PROXY=${var.https_proxy}",
# "NO_PROXY=${var.no_proxy}"
#]
}
# https://developer.hashicorp.com/packer/docs/provisioners/shell
#
provisioner "shell" {
# needed to test for existing of logs in /root to copy out to vboxsf shared folder
# overriding execute_command you must manually put {{ .Vars }} prefixed in the command otherwise you'll lose environment_vars
execute_command = "{{ .Vars }} echo 'packer' | sudo -S -E bash -euxo pipefail '{{ .Path }}'"
inline = [
"env",
# pre-authorize sudo - or run whole shell as root using execute_command above
#"echo 'packer' | sudo -S echo",
"echo Built using Packer version '${packer.version}' | sudo tee /etc/packer-version",
#"echo '${build.SSHPrivateKey}' > /tmp/packer-session.pem", # temporary SSH private key eg. git clone a private repo [email protected]:org/repo
"mkdir -pv /mnt/vboxsf",
"echo Mounting /mnt/vboxsf",
"mount -t vboxsf vboxsf /mnt/vboxsf",
# mount point is owned by root
"cp -fv /root/anaconda-ks.cfg /mnt/vboxsf/",
"for x in ks-pre.log ks-post.log; do if [ -f /root/$x ]; then cp -fv /root/$x /mnt/vboxsf/; fi; done"
]
scripts = [
# some common scripts found in https://github.com/HariSekhon/Packer
"${local.scripts}/version.sh",
#
#"${local.scripts}/install_vbox_additions.sh",
#"${local.scripts}/mount_vboxsf.sh",
#
#"${local.scripts}/mount_apple_virtiofs.sh",
#
"${local.scripts}/collect_anaconda.sh",
#"${local.scripts}/collect_preseed.sh",
#"${local.scripts}/collect_autoinstall_user_data.sh",
"${local.scripts}/final.sh"
]
# max_retries = 5
# timeout = "5m"
#environment_vars = [
# "HTTP_PROXY=${var.http_proxy}",
# "HTTPS_PROXY=${var.https_proxy}",
# "NO_PROXY=${var.no_proxy}"
#]
}
# post-processor blocks run in parallel
#
post-processor "checksum" { # checksum image
checksum_types = ["md5", "sha512"] # checksum the artifact
keep_input_artifact = true # keep the artifact
output = "output-{{.BuildName}}/{{.BuildName}}.{{.ChecksumType}}" # default: packer_{{.BuildName}}_{{.BuilderType}}_{{.ChecksumType}}.checksum, at top level not in the directory with the .ova, and it keeps appending to it
}
}