-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwindows.pkr.hcl
261 lines (217 loc) · 8.8 KB
/
windows.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
packer {
required_plugins {
azure = {
version = ">= 1.0.0"
source = "github.com/hashicorp/azure"
}
# windows-update = {
# version = "0.14.0"
# source = "github.com/rgl/windows-update"
# }
}
}
variable "azure_client_id" {
type = string
default = "" # pr_infra_staging
description = "The Active Directory service principal associated with your builder."
}
variable "azure_client_secret" {
type = string
default = ""
description = "The password or secret for your service principal."
}
variable "azure_subscription_id" {
type = string
default = ""
description = "Subscription under which the build will be performed"
}
variable "azure_tenant_id" {
type = string
default = ""
description = "The Active Directory tenant identifier with which your client_id and subscription_id are associated. If not specified, tenant_id will be looked up using subscription_id."
}
variable "azure_vm_size" {
type = string
default = "Standard_B2s"
description = "Size of the VM used for building. This can be changed when you deploy a VM from your VHD."
}
variable "azure_location" {
type = string
default = ""
description = "Azure datacenter in which your VM will build."
}
variable "azure_image_publisher" {
type = string
default = "MicrosoftWindowsDesktop"
description = "Name of the publisher to use for your base image (Azure Marketplace Images only)."
}
variable "azure_image_offer" {
type = string
default = "windows-11"
description = "Name of the publisher's offer to use for your base image (Azure Marketplace Images only)."
}
variable "azure_image_sku" {
type = string
default = "win11-21h2-avd"
description = "SKU of the image offer to use for your base image (Azure Marketplace Images only)."
}
# When creating a VHD the following additional options are required:
# variable "azure_capture_container_name" {
# type = string
# default = "images"
# description = "Destination container name. Essentially the directory where your VHD will be organized in Azure."
# }
# variable "azure_capture_name_prefix" {
# type = string
# default = "packer"
# description = "VHD prefix. The final artifacts will be named PREFIX-osDisk.UUID and PREFIX-vmTemplate.UUID"
# }
# variable "azure_resource_group_name" {
# type = string
# default = "spr_infra_rg_packer"
# description = "Resource group under which the final artifact will be stored."
# }
# variable "azure_storage_account" {
# type = string
# default = "sprsapacker"
# description = "Storage account under which the final artifact will be stored."
# }
# When creating a managed image the following additional options are required:
variable "azure_managed_image_name" {
type = string
default = ""
description = "Specify the managed image name where the result of the Packer build will be saved. The image name must not exist ahead of time, and will not be overwritten. If this value is set, the value managed_image_resource_group_name must also be set."
}
variable "azure_resource_group_name" {
type = string
default = ""
description = "Specify the managed image resource group name where the result of the Packer build will be saved. The resource group must already exist. If this value is set, the value managed_image_name must also be set."
}
# Azure Share Image Gallery Settings
variable "azure_shared_image_gallery_name" {
type = string
default = ""
description = "Name of the shared image gallery"
}
variable "azure_shared_image_gallery_destination_image_version" {
type = string
default = "1.0.0"
}
variable "azure_shared_image_gallery_destination_storage_account_type" {
type = string
default = "Standard_LRS"
}
variable "azure_os_type" {
type = string
default = "Windows"
}
variable "azure_tags" {
type = map(string)
description = "(map[string]string) - Name/value pair tags to apply to every resource deployed i.e. Resource Group, VM, NIC, VNET, Public IP, KeyVault, etc. The user can define up to 15 tags. Tag names cannot exceed 512 characters, and tag values cannot exceed 256 characters."
default = {
source = "packer"
}
}
variable "winrm_username" {
type = string
default = "packer"
}
variable "ansible_playbook_file" {
type = string
default = "../ansible/playbook.yml"
}
variable "ansible_roles_path" {
type = string
default = "../ansible/roles"
}
variable "ansible_galaxy_file" {
type = string
default = "../ansible/requirements.yml"
}
variable "ansible_groups" {
type = list(string)
default = ["windows"]
description = "The groups into which the Ansible host should be placed. When unspecified, the host is not associated with any groups"
}
variable "ansible_inventory_directory" {
type = string
default = "../ansible"
description = "The directory in which to place the temporary generated Ansible inventory file."
}
source "azure-arm" "windows" {
client_id = var.azure_client_id
client_secret = var.azure_client_secret
subscription_id = var.azure_subscription_id
tenant_id = var.azure_tenant_id
# When creating a VHD the following additional options are required:
# capture_container_name = var.azure_capture_container_name
# capture_name_prefix = var.azure_capture_name_prefix
# resource_group_name = var.azure_resource_group_name
# storage_account = var.azure_storage_account
# When creating a managed image the following additional options are required:
managed_image_name = var.azure_managed_image_name
managed_image_resource_group_name = var.azure_resource_group_name
os_type = var.azure_os_type
image_publisher = var.azure_image_publisher
image_offer = var.azure_image_offer
image_sku = var.azure_image_sku
azure_tags = var.azure_tags
location = var.azure_location
vm_size = var.azure_vm_size
shared_image_gallery_destination {
subscription = var.azure_subscription_id
resource_group = var.azure_resource_group_name
gallery_name = var.azure_shared_image_gallery_name
image_name = var.azure_managed_image_name
image_version = var.azure_shared_image_gallery_destination_image_version
replication_regions = [var.azure_location]
storage_account_type = var.azure_shared_image_gallery_destination_storage_account_type
}
communicator = "winrm"
winrm_use_ssl = true
winrm_insecure = true
winrm_timeout = "1h"
winrm_username = var.winrm_username
}
build {
sources = ["sources.azure-arm.windows"]
# provisioner "windows-update" {
# search_criteria = "IsInstalled=0"
# filters = [
# "exclude:$_.Title -like '*Preview*'",
# "include:$true",
# ]
# update_limit = 25
# }
provisioner "powershell" {
inline = [
" while ((Get-Service RdAgent).Status -ne 'Running') { Start-Sleep -s 5 }",
" while ((Get-Service WindowsAzureGuestAgent).Status -ne 'Running') { Start-Sleep -s 5 }"
]
}
provisioner "powershell" {
elevated_user = build.User
elevated_password = build.Password
script = "./setup/openssh.ps1"
}
provisioner "ansible" {
playbook_file = var.ansible_playbook_file
user = build.User
use_proxy = false
galaxy_file = var.ansible_galaxy_file
groups = var.ansible_groups
inventory_directory = var.ansible_inventory_directory
roles_path = var.ansible_roles_path
ansible_ssh_extra_args = ["-o IdentitiesOnly=yes", "-o StrictHostKeyChecking=no", "-o UserKnownHostsFile=/dev/null"]
extra_arguments = [
"--extra-vars",
"ansible_user=${build.User} ansible_password=${build.Password} ansible_become_user=${build.User} ansible_port=22 ansible_shell_type=powershell ansible_become_method=runas ansible_connection=ssh ansible_ssh_retries=30"
]
}
provisioner "powershell" {
inline = [
"& $env:SystemRoot\\System32\\Sysprep\\Sysprep.exe /oobe /generalize /quiet /quit /mode:vm",
"while($true) { $imageState = Get-ItemProperty HKLM:\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Setup\\State | Select ImageState; if($imageState.ImageState -ne 'IMAGE_STATE_GENERALIZE_RESEAL_TO_OOBE') { Write-Output $imageState.ImageState; Start-Sleep -s 10 } else { break } }"
]
}
}