Skip to content

Commit 22edfbd

Browse files
authored
Merge pull request #203579 from ericd-mst-github/erd-build-image-with-packer
Updated packer content with HCL examples
2 parents 1492187 + 67a993d commit 22edfbd

File tree

2 files changed

+84
-3
lines changed

2 files changed

+84
-3
lines changed

articles/virtual-machines/linux/build-image-with-packer.md

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ You use the output from these two commands in the next step.
6262
## Define Packer template
6363
To build images, you create a template as a JSON file. In the template, you define builders and provisioners that carry out the actual build process. Packer has a [provisioner for Azure](https://www.packer.io/docs/builders/azure.html) that allows you to define Azure resources, such as the service principal credentials created in the preceding step.
6464

65-
Create a file named *ubuntu.json* and paste the following content. Enter your own values for the following:
65+
Create a file named *ubuntu.json* and paste the following content. Enter your own values for the following parameters:
6666

6767
| Parameter | Where to obtain |
6868
|-------------------------------------|----------------------------------------------------|
@@ -114,6 +114,40 @@ Create a file named *ubuntu.json* and paste the following content. Enter your ow
114114
}]
115115
}
116116
```
117+
You can also create a filed named *ubuntu.pkr.hcl* and paste the following content with your own values as used for the above parameters table.
118+
119+
```HCL
120+
source "azure-arm" "autogenerated_1" {
121+
azure_tags = {
122+
dept = "Engineering"
123+
task = "Image deployment"
124+
}
125+
client_id = "f5b6a5cf-fbdf-4a9f-b3b8-3c2cd00225a4"
126+
client_secret = "0e760437-bf34-4aad-9f8d-870be799c55d"
127+
image_offer = "UbuntuServer"
128+
image_publisher = "Canonical"
129+
image_sku = "16.04-LTS"
130+
location = "East US"
131+
managed_image_name = "myPackerImage"
132+
managed_image_resource_group_name = "myResourceGroup"
133+
os_type = "Linux"
134+
subscription_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx"
135+
tenant_id = "72f988bf-86f1-41af-91ab-2d7cd011db47"
136+
vm_size = "Standard_DS2_v2"
137+
}
138+
139+
build {
140+
sources = ["source.azure-arm.autogenerated_1"]
141+
142+
provisioner "shell" {
143+
execute_command = "chmod +x {{ .Path }}; {{ .Vars }} sudo -E sh '{{ .Path }}'"
144+
inline = ["apt-get update", "apt-get upgrade -y", "apt-get -y install nginx", "/usr/sbin/waagent -force -deprovision+user && export HISTSIZE=0 && sync"]
145+
inline_shebang = "/bin/sh -x"
146+
}
147+
148+
}
149+
```
150+
117151

118152
This template builds an Ubuntu 16.04 LTS image, installs NGINX, then deprovisions the VM.
119153

@@ -131,6 +165,12 @@ Build the image by specifying your Packer template file as follows:
131165
./packer build ubuntu.json
132166
```
133167

168+
You can also build the image by specifying the *ubuntu.pkr.hcl* file as follows:
169+
170+
```bash
171+
packer build ubuntu.pkr.hcl
172+
```
173+
134174
An example of the output from the preceding commands is as follows:
135175

136176
```output
@@ -196,7 +236,7 @@ It takes a few minutes for Packer to build the VM, run the provisioners, and cle
196236

197237

198238
## Create VM from Azure Image
199-
You can now create a VM from your Image with [az vm create](/cli/azure/vm). Specify the Image you created with the `--image` parameter. The following example creates a VM named *myVM* from *myPackerImage* and generates SSH keys if they do not already exist:
239+
You can now create a VM from your Image with [az vm create](/cli/azure/vm). Specify the Image you created with the `--image` parameter. The following example creates a VM named *myVM* from *myPackerImage* and generates SSH keys if they don't already exist:
200240

201241
```azurecli
202242
az vm create \

articles/virtual-machines/windows/build-image-with-packer.md

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ Packer authenticates with Azure using a service principal. An Azure service prin
4141
Create a service principal with [New-AzADServicePrincipal](/powershell/module/az.resources/new-azadserviceprincipal). The value for `-DisplayName` needs to be unique; replace with your own value as needed.
4242

4343
```azurepowershell
44-
$sp = New-AzADServicePrincipal -DisplayName "PackerSP$(Get-Random)"
44+
$sp = New-AzADServicePrincipal -DisplayName "PackerPrincipal" -role Contributor -scope /subscriptions/yyyyyyy-yyyy-yyyy-yyyy-yyyyyyyyyyy
4545
$plainPassword = (New-AzADSpCredential -ObjectId $sp.Id).SecretText
4646
```
4747

@@ -119,6 +119,42 @@ Create a file named *windows.json* and paste the following content. Enter your o
119119
}]
120120
}
121121
```
122+
You can also create a filed named *windows.pkr.hcl* and paste the following content with your own values as used for the above parameters table.
123+
124+
```HCL
125+
source "azure-arm" "autogenerated_1" {
126+
azure_tags = {
127+
dept = "Engineering"
128+
task = "Image deployment"
129+
}
130+
build_resource_group_name = "myPackerGroup"
131+
client_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx"
132+
client_secret = "ppppppp-pppp-pppp-pppp-ppppppppppp"
133+
communicator = "winrm"
134+
image_offer = "WindowsServer"
135+
image_publisher = "MicrosoftWindowsServer"
136+
image_sku = "2016-Datacenter"
137+
managed_image_name = "myPackerImage"
138+
managed_image_resource_group_name = "myPackerGroup"
139+
os_type = "Windows"
140+
subscription_id = "yyyyyyy-yyyy-yyyy-yyyy-yyyyyyyyyyy"
141+
tenant_id = "zzzzzzz-zzzz-zzzz-zzzz-zzzzzzzzzzzz"
142+
vm_size = "Standard_D2_v2"
143+
winrm_insecure = true
144+
winrm_timeout = "5m"
145+
winrm_use_ssl = true
146+
winrm_username = "packer"
147+
}
148+
149+
build {
150+
sources = ["source.azure-arm.autogenerated_1"]
151+
152+
provisioner "powershell" {
153+
inline = ["Add-WindowsFeature Web-Server", "while ((Get-Service RdAgent).Status -ne 'Running') { Start-Sleep -s 5 }", "while ((Get-Service WindowsAzureGuestAgent).Status -ne 'Running') { Start-Sleep -s 5 }", "& $env:SystemRoot\\System32\\Sysprep\\Sysprep.exe /oobe /generalize /quiet /quit", "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 } }"]
154+
}
155+
156+
}
157+
```
122158

123159
This template builds a Windows Server 2016 VM, installs IIS, then generalizes the VM with Sysprep. The IIS install shows how you can use the PowerShell provisioner to run additional commands. The final Packer image then includes the required software install and configuration.
124160

@@ -133,6 +169,11 @@ Build the image by opening a cmd prompt and specifying your Packer template file
133169
```
134170
./packer build windows.json
135171
```
172+
You can also build the image by specifying the *windows.pkr.hcl* file as follows:
173+
174+
```
175+
packer build windows.pkr.hcl
176+
```
136177

137178
An example of the output from the preceding commands is as follows:
138179

0 commit comments

Comments
 (0)