如何在Packer Azure HCL配置中指定源镜像生成1.0.2版本镜像?
基于Azure计算镜像库已有版本创建新镜像的Packer配置修改
问题说明
你当前的Packer配置使用Canonical公共Ubuntu镜像作为构建源,现在需要改为基于自己Azure计算镜像库中test-ubuntu-20.04的1.0.0版本,生成1.0.2版本的新镜像,核心是替换源镜像的配置方式。
修改方案
需要删除原配置中指向公共镜像的image_offer、image_publisher、image_sku参数,替换为shared_image_gallery_source块来指定私有镜像库的源镜像信息,同时更新目标版本号。
修改后的完整配置如下:
packer { required_plugins { azure = { version = ">= 1.0.0" source = "github.com/hashicorp/azure" } } } source "azure-arm" "example" { managed_image_name = "test-ubuntu-20.04img-${formatdate("DD-MMM-YYYY-hh-mm-ss", timestamp())}" managed_image_resource_group_name = var.gallery_resource_group managed_image_storage_account_type = "Standard_LRS" location = var.location # 使用Azure CLI认证 use_azure_cli_auth = true os_type = "Linux" vm_size = "Standard_B4ms" os_disk_size_gb = 64 # 配置源镜像:指向你现有计算镜像库中的1.0.0版本 shared_image_gallery_source { resource_group = var.gallery_resource_group gallery_name = var.gallery_name image_name = "test-ubuntu-20.04" image_version = "1.0.0" } # 配置目标镜像:生成1.0.2版本到镜像库 shared_image_gallery_destination { resource_group = var.gallery_resource_group gallery_name = var.gallery_name image_name = "test-ubuntu-20.04" image_version = "1.0.2" replication_regions = ["uaenorth"] storage_account_type = "Standard_LRS" } } build { sources = [ "source.azure-arm.example" ] provisioner "shell" { execute_command = "chmod +x {{ .Path }}; sudo {{ .Vars }} {{ .Path }}" script = "install.sh" } }
关键修改点解释
- 替换源镜像配置:用
shared_image_gallery_source块替代原公共镜像的三个参数,其中:resource_group:源镜像所在的资源组gallery_name:你的计算镜像库名称image_name:库中要作为源的镜像名称image_version:源镜像的版本号(这里是1.0.0)
- 更新目标版本:在
shared_image_gallery_destination中将image_version改为1.0.2,这是你要生成的新版本号
内容的提问来源于stack exchange,提问作者kumar
相关产品推荐
相关产品推荐

