关于 azure-image-builder
这项技能使用 Packer 的 `azure-arm` 构建器创建 Azure 托管映像和 Azure Compute Gallery 映像,面向 Azure 用户,介绍模板、服务主体或托管身份验证及构建问题排查。
主要功能
- 01托管映像构建模板
- 02Azure Compute Gallery 映像
- 03服务主体身份验证
- 04托管身份验证
- 05Packer 初始化与构建命令
适用场景
- 01构建 Azure 托管映像
- 02将映像发布到 Azure Compute Gallery
- 03为 Azure 构建配置身份验证
- 04排查预配超时
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ hashicorp/ agent-skills --skill azure-image-builder安装后在 Claude Code 中输入/azure-image-builder确认已加载。
详细介绍
Azure Image Builder
使用 Packer 的 azure-arm builder 构建 Azure 托管映像和 Azure Compute Gallery 映像。
注意: 构建 Azure 映像会产生费用(计算、存储和数据传输)。根据预配过程和操作系统的不同,构建通常需要 15-45 分钟。
基本托管映像
packer {
required_plugins {
azure = {
source = "github.com/hashicorp/azure"
version = "~> 2.0"
}
}
}
variable "client_id" {
type = string
sensitive = true
}
variable "client_secret" {
type = string
sensitive = true
}
variable "subscription_id" {
type = string
}
variable "tenant_id" {
type = string
}
variable "resource_group" {
type = string
default = "packer-images-rg"
}
locals {
timestamp = regex_replace(timestamp(), "[- TZ:]", "")
}
source "azure-arm" "ubuntu" {
client_id = var.client_id
client_secret = var.client_secret
subscription_id = var.subscription_id
tenant_id = var.tenant_id
managed_image_resource_group_name = var.resource_group
managed_image_name = "my-app-${local.timestamp}"
os_type = "Linux"
image_publisher = "Canonical"
image_offer = "0001-com-ubuntu-server-jammy"
image_sku = "22_04-lts-gen2"
location = "East US"
vm_size = "Standard_B2s"
azure_tags = {
Name = "my-app"
BuildDate = local.timestamp
}
}
build {
sources = ["source.azure-arm.ubuntu"]
provisioner "shell" {
inline = [
"sudo apt-get update",
"sudo apt-get upgrade -y",
]
}
}
Azure Compute Gallery
source "azure-arm" "ubuntu" {
client_id = var.client_id
client_secret = var.client_secret
subscription_id = var.subscription_id
tenant_id = var.tenant_id
os_type = "Linux"
image_publisher = "Canonical"
image_offer = "0001-com-ubuntu-server-jammy"
image_sku = "22_04-lts-gen2"
location = "East US"
vm_size = "Standard_B2s"
shared_image_gallery_destination {
resource_group = "gallery-rg"
gallery_name = "myImageGallery"
image_name = "ubuntu-webapp"
image_version = "1.0.${formatdate("YYYYMMDD", timestamp())}"
replication_regions = ["East US", "West US 2"]
storage_account_type = "Standard_LRS"
}
}
身份验证
服务主体
# Create service principal
az ad sp create-for-rbac \
--name "packer-sp" \
--role Contributor \
--scopes /subscriptions/<subscription-id>
# Set environment variables
export ARM_CLIENT_ID="<client-id>"
export ARM_CLIENT_SECRET="<client-secret>"
export ARM_SUBSCRIPTION_ID="<subscription-id>"
export ARM_TENANT_ID="<tenant-id>"
托管标识
source "azure-arm" "ubuntu" {
use_azure_cli_auth = true
subscription_id = var.subscription_id
# ... rest of configuration
}
构建命令
# Set authentication
export ARM_CLIENT_ID="your-client-id"
export ARM_CLIENT_SECRET="your-client-secret"
export ARM_SUBSCRIPTION_ID="your-subscription-id"
export ARM_TENANT_ID="your-tenant-id"
# Initialize plugins
packer init .
# Validate template
packer validate .
# Build image
packer build .
常见问题
身份验证失败
- 验证服务主体凭据
- 确保资源组具有 Contributor 角色
- 检查订阅 ID 和租户 ID
Compute Gallery 版本已存在
- 映像版本不可变
- 使用唯一的版本号,并包含日期或构建编号
- 无法覆盖已有版本
预配期间超时
- 检查构建 VM 的网络连接
- 确认 NSG 规则允许所需流量
- 如有需要,延长超时时间
参考资料
评论
支持 Markdown