Azure中基于私有Packer镜像创建新镜像的配置方法问询
Great question! When working with Azure private images in Packer, you won't use the image_publisher, image_offer, or image_sku fields—those are exclusively for targeting public marketplace images. Instead, you'll use one of two straightforward approaches to point directly to your existing private image.
Option 1: Use the Private Image's Resource ID
You can reference your private image by its full Azure resource ID. This is the most explicit method, especially if you're working across multiple subscriptions or resource groups.
Here's an example HCL configuration snippet:
source "azure-arm" "private_image_base" { # Required Azure authentication fields (client_id, client_secret, tenant_id, subscription_id) os_type = "Linux" # Swap to "Windows" if your private image uses Windows location = "eastus" # Reference the private image via its resource ID image_id = "/subscriptions/your-subscription-id/resourceGroups/your-source-rg/providers/Microsoft.Compute/images/your-private-image-name" # Configuration for the new image you're building managed_image_resource_group_name = "your-output-rg" managed_image_name = "new-custom-packer-image" } build { sources = ["source.azure-arm.private_image_base"] # Add your provisioners (shell scripts, Ansible playbooks, etc.) here }
Option 2: Use Resource Group and Image Name
If your private image is in the same subscription as your Packer build, you can simplify the reference by specifying just the resource group name and the image name:
source "azure-arm" "private_image_base" { # Required Azure authentication fields os_type = "Linux" location = "eastus" # Reference the private image by resource group and name managed_image_resource_group_name = "your-source-rg" managed_image_name = "your-private-image-name" # Configuration for the output image managed_image_resource_group_name = "your-output-rg" managed_image_name = "new-custom-packer-image" }
Key Notes:
- Choose one option only: Don't include both
image_idand themanaged_image_resource_group_name/managed_image_namepair—pick the method that fits your workflow best. - Permissions matter: Ensure the service principal or identity you're using for Packer has at least the
Compute Image Readerrole on the resource group containing your private image. Without this, Packer won't be able to access the base image. - Skip marketplace fields: Remove any
image_publisher,image_offer, orimage_skulines from your configuration—they'll cause Packer to look for a public marketplace image, which will fail since you're targeting a private one.
内容的提问来源于stack exchange,提问作者James Liu

