-
Notifications
You must be signed in to change notification settings - Fork 315
/
linux-almalinux.pkr.hcl
199 lines (180 loc) · 7.24 KB
/
linux-almalinux.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
/*
DESCRIPTION:
AlmaLinux OS 8 template using the Packer Builder for VMware vSphere (vsphere-iso).
*/
// BLOCK: packer
// The Packer configuration.
packer {
required_version = ">= 1.8.2"
required_plugins {
vsphere = {
version = ">= v1.0.6"
source = "github.com/hashicorp/vsphere"
}
}
}
// 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 = formatdate("YY.MM", timestamp())
build_description = "Version: v${local.build_version}\nBuilt on: ${local.build_date}\n${local.build_by}"
iso_paths = ["[${var.common_iso_datastore}] ${var.iso_path}/${var.iso_file}"]
iso_checksum = "${var.iso_checksum_type}:${var.iso_checksum_value}"
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/${local.vm_name}"
data_source_content = {
"/ks.cfg" = templatefile("${abspath(path.root)}/data/ks.pkrtpl.hcl", {
build_username = var.build_username
build_password = var.build_password
build_password_encrypted = var.build_password_encrypted
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
})
}
data_source_command = var.common_data_source == "http" ? "inst.ks=https://{{ .HTTPIP }}:{{ .HTTPPort }}/ks.cfg" : "inst.ks=cdrom:/ks.cfg"
vm_name = "${var.vm_guest_os_family}-${var.vm_guest_os_name}-${var.vm_guest_os_version}-v${local.build_version}"
}
// BLOCK: source
// Defines the builder configuration blocks.
source "vsphere-iso" "linux-almalinux" {
// 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
datastore = var.vsphere_datastore
folder = var.vsphere_folder
// Virtual Machine Settings
vm_name = local.vm_name
guest_os_type = var.vm_guest_os_type
firmware = var.vm_firmware
CPUs = var.vm_cpu_sockets
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
tools_upgrade_policy = var.common_tools_upgrade_policy
notes = local.build_description
// Removable Media Settings
iso_paths = local.iso_paths
iso_checksum = local.iso_checksum
http_content = var.common_data_source == "http" ? local.data_source_content : null
cd_content = var.common_data_source == "disk" ? local.data_source_content : null
// Boot and Provisioning Settings
http_ip = var.common_data_source == "http" ? var.common_http_ip : null
http_port_min = var.common_data_source == "http" ? var.common_http_port_min : null
http_port_max = var.common_data_source == "http" ? var.common_http_port_max : null
boot_order = var.vm_boot_order
boot_wait = var.vm_boot_wait
boot_command = [
"<up>",
"e",
"<down><down><end><wait>",
"text ${local.data_source_command}",
"<enter><wait><leftCtrlOn>x<leftCtrlOff>"
]
ip_wait_timeout = var.common_ip_wait_timeout
shutdown_command = "echo '${var.build_password}' | sudo -S -E shutdown -P now"
shutdown_timeout = var.common_shutdown_timeout
// Communicator Settings and Credentials
communicator = "ssh"
ssh_proxy_host = var.communicator_proxy_host
ssh_proxy_port = var.communicator_proxy_port
ssh_proxy_username = var.communicator_proxy_username
ssh_proxy_password = var.communicator_proxy_password
ssh_username = var.build_username
ssh_password = var.build_password
ssh_port = var.communicator_port
ssh_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_name != null ? [1] : []
content {
library = var.common_content_library_name
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 == true ? [1] : []
content {
name = local.vm_name
force = var.common_ovf_export_overwrite
options = [
"extraconfig"
]
output_directory = local.ovf_export_path
}
}
}
// BLOCK: build
// Defines the builders to run, provisioners, and post-processors.
build {
sources = ["source.vsphere-iso.linux-almalinux"]
provisioner "ansible" {
user = var.build_username
playbook_file = "${path.cwd}/ansible/main.yml"
roles_path = "${path.cwd}/ansible/roles"
ansible_env_vars = [
"ANSIBLE_CONFIG=${path.cwd}/ansible/ansible.cfg",
"ANSIBLE_PYTHON_INTERPRETER=/usr/libexec/platform-python"
]
extra_arguments = [
"--extra-vars", "display_skipped_hosts=false",
"--extra-vars", "BUILD_USERNAME=${var.build_username}",
"--extra-vars", "BUILD_SECRET='${var.build_key}'",
"--extra-vars", "ANSIBLE_USERNAME=${var.ansible_username}",
"--extra-vars", "ANSIBLE_SECRET='${var.ansible_key}'",
]
}
post-processor "manifest" {
output = local.manifest_output
strip_path = true
strip_time = true
custom_data = {
ansible_username = var.ansible_username
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_sockets = var.vm_cpu_sockets
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_datacenter = var.vsphere_datacenter
vsphere_datastore = var.vsphere_datastore
vsphere_endpoint = var.vsphere_endpoint
vsphere_folder = var.vsphere_folder
}
}
}