你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

快速入门:使用 Azure CLI 定义和分配 Azure 蓝图

重要

2026 年 7 月 11 日,蓝图(预览版)将弃用。 将现有蓝图定义和分配迁移到模板规格部署堆栈。 蓝图项目将转换为 ARM JSON 模板或用于定义部署堆栈的 Bicep 文件。 若要了解如何将项目创作为 ARM 资源,请参阅:

本教程介绍如何使用 Azure 蓝图来执行某些与在组织中创建、发布和分配蓝图相关的常见任务。 此技能可帮助你定义常见的模式,以便基于 Azure 资源管理器 (ARM) 模板、策略和安全性开发可重用且可快速部署的配置。

先决条件

  • 如果没有 Azure 订阅,请在开始之前创建一个免费帐户
  • 如果你之前未用过 Azure 蓝图,请通过 Azure CLI 使用 az provider register --namespace Microsoft.Blueprint 注册资源提供程序。

Azure Cloud Shell

Azure 托管 Azure Cloud Shell(一个可通过浏览器使用的交互式 shell 环境)。 可以将 Bash 或 PowerShell 与 Cloud Shell 配合使用来使用 Azure 服务。 可以使用 Cloud Shell 预安装的命令来运行本文中的代码,而不必在本地环境中安装任何内容。

若要启动 Azure Cloud Shell,请执行以下操作:

选项 示例/链接
选择代码或命令块右上角的“试用”。 选择“试用”不会自动将代码或命令复制到 Cloud Shell。 Screenshot that shows an example of Try It for Azure Cloud Shell.
转到 https://shell.azure.com 或选择启动 Cloud Shell 按钮可在浏览器中打开 Cloud Shell。 Button to launch Azure Cloud Shell.
选择 Azure 门户右上角菜单栏上的 Cloud Shell 按钮。 Screenshot that shows the Cloud Shell button in the Azure portal

若要使用 Azure Cloud Shell,请执行以下操作:

  1. 启动 Cloud Shell。

  2. 选择代码块(或命令块)上的“复制”按钮以复制代码或命令。

  3. 在 Windows 和 Linux 上选择 Ctrl+Shift+V,或在 macOS 上选择 Cmd+Shift+V 将代码或命令粘贴到 Cloud Shell 会话中。

  4. 选择“Enter”运行代码或命令。

添加蓝图扩展

若要使 Azure CLI 能够管理蓝图定义和分配,则必须添加该扩展。 此扩展适用于可以使用 Azure CLI 的任何位置。 包括 Windows 10 上的 BashCloud Shell(独立版本和门户内部版本)、Azure CLI Docker 映像或本地安装的扩展。

  1. 请确保安装最新的 Azure CLI(至少为 2.0.76)。 若尚未安装,请遵循这些说明

  2. 在所选的 Azure CLI 环境中,使用以下命令导入该扩展:

    # Add the Blueprint extension to the Azure CLI environment
    az extension add --name blueprint
    
  3. 验证该扩展是否已安装以及是否为预期的版本(至少为 0.1.0):

    # Check the extension list (note that you might have other extensions installed)
    az extension list
    
    # Run help for extension options
    az blueprint -h
    

创建蓝图

定义符合性的标准模式的第一步是根据可用资源构建蓝图。 让我们创建一个名为 MyBlueprint 的蓝图,用于配置订阅的角色和策略分配。 然后,在资源组上添加资源组、ARM 模板和角色分配。

备注

使用 Azure CLI 时,首先创建 blueprint 对象。 对于每个要添加的具有参数的项目,需要在初始蓝图上提前定义该参数。

  1. 创建初始 blueprint 对象。 parameters 参数采用包含所有蓝图级别参数的 JSON 文件。 在分配过程中设置参数,在后续步骤中添加的项目将使用这些参数。

    • JSON 文件 - blueprintparms.json

      {
         "storageAccountType": {
             "type": "string",
             "defaultValue": "Standard_LRS",
             "allowedValues": [
                 "Standard_LRS",
                 "Standard_GRS",
                 "Standard_ZRS",
                 "Premium_LRS"
             ],
             "metadata": {
                 "displayName": "storage account type.",
                 "description": null
             }
         },
         "tagName": {
             "type": "string",
             "metadata": {
                 "displayName": "The name of the tag to provide the policy assignment.",
                 "description": null
             }
         },
         "tagValue": {
             "type": "string",
             "metadata": {
                 "displayName": "The value of the tag to provide the policy assignment.",
                 "description": null
             }
         },
         "contributors": {
             "type": "array",
             "metadata": {
                 "description": "List of AAD object IDs that is assigned Contributor role at the subscription",
                 "strongType": "PrincipalId"
             }
         },
         "owners": {
             "type": "array",
             "metadata": {
                 "description": "List of AAD object IDs that is assigned Owner role at the resource group",
                 "strongType": "PrincipalId"
             }
         }
      }
      
    • Azure CLI 命令

      # Login first with az login if not using Cloud Shell
      
      # Create the blueprint object
      az blueprint create \
         --name 'MyBlueprint' \
         --description 'This blueprint sets tag policy and role assignment on the subscription, creates a ResourceGroup, and deploys a resource template and role assignment to that ResourceGroup.' \
         --parameters blueprintparms.json
      

      注意

      导入蓝图定义时,请使用文件名 blueprint.json。 调用 az blueprint import 时使用此文件名。

      默认情况下,会在默认订阅中创建蓝图对象。 若要指定管理组,请使用参数 managementgroup。 若要指定订阅,请使用参数 subscription

  2. 将存储项目的资源组添加到定义中。

    az blueprint resource-group add \
       --blueprint-name 'MyBlueprint' \
       --artifact-name 'storageRG' \
       --description 'Contains the resource template deployment and a role assignment.'
    
  3. 在订阅中添加角色分配。 在下面的示例中,主体标识被授予指定的角色,配置为蓝图分配过程中所设置的参数。 此示例使用 GUID 为 b24988ac-6180-42a0-ab88-20f7382dd24cContributor 内置角色。

    az blueprint artifact role create \
       --blueprint-name 'MyBlueprint' \
       --artifact-name 'roleContributor' \
       --role-definition-id '/providers/Microsoft.Authorization/roleDefinitions/b24988ac-6180-42a0-ab88-20f7382dd24c' \
       --principal-ids "[parameters('contributors')]"
    
  4. 在订阅中添加策略分配。 此示例使用 GUID 为 49c88fc8-6fd1-46fd-a676-f12d1d3a4c71Apply tag and its default value to resource groups 内置策略。

    • JSON 文件 - artifacts\policyTags.json

      {
         "tagName": {
            "value": "[parameters('tagName')]"
         },
         "tagValue": {
            "value": "[parameters('tagValue')]"
         }
      }
      
    • Azure CLI 命令

      az blueprint artifact policy create \
         --blueprint-name 'MyBlueprint' \
         --artifact-name 'policyTags' \
         --policy-definition-id '/providers/Microsoft.Authorization/policyDefinitions/49c88fc8-6fd1-46fd-a676-f12d1d3a4c71' \
         --display-name 'Apply tag and its default value to resource groups' \
         --description 'Apply tag and its default value to resource groups' \
         --parameters artifacts\policyTags.json
      

      注意

      在 Mac 上使用 az blueprint 时,对于包含路径的参数值,请将 \ 替换为 /。 在这种情况下,parameters 的值变为 artifacts/policyTags.json

  5. (通过重复使用 storageAccountType_ parameter)在订阅中为存储标记添加另一个策略分配。 此附加的策略分配项目演示了蓝图上定义的参数可由多个项目使用。 在示例中,使用 storageAccountType 在资源组上设置标记。 此值提供有关下一步骤中创建的存储帐户的信息。 此示例使用 GUID 为 49c88fc8-6fd1-46fd-a676-f12d1d3a4c71Apply tag and its default value to resource groups 内置策略。

    • JSON 文件 - artifacts\policyStorageTags.json

      {
         "tagName": {
            "value": "StorageType"
         },
         "tagValue": {
            "value": "[parameters('storageAccountType')]"
         }
      }
      
    • Azure CLI 命令

      az blueprint artifact policy create \
         --blueprint-name 'MyBlueprint' \
         --artifact-name 'policyStorageTags' \
         --policy-definition-id '/providers/Microsoft.Authorization/policyDefinitions/49c88fc8-6fd1-46fd-a676-f12d1d3a4c71' \
         --display-name 'Apply storage tag to resource group' \
         --description 'Apply storage tag and the parameter also used by the template to resource groups' \
         --parameters artifacts\policyStorageTags.json
      

      注意

      在 Mac 上使用 az blueprint 时,对于包含路径的参数值,请将 \ 替换为 /。 在这种情况下,parameters 的值变为 artifacts/policyStorageTags.json

  6. 在资源组下添加模板。 ARM 模板的 template 参数包含模板的标准 JSON 组件。 该模板还通过将 storageAccountTypetagNametagValue 蓝图参数传递给模板来重用它们。 通过使用参数 parameters 和在使用键值对的模板 JSON 中注入值,模板可以使用蓝图参数。 蓝图和模板参数名称可能相同。

    • JSON ARM 模板文件 - artifacts\templateStorage.json

      {
          "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
          "contentVersion": "1.0.0.0",
          "parameters": {
              "storageAccountTypeFromBP": {
                  "type": "string",
                  "metadata": {
                      "description": "Storage Account type"
                  }
              },
              "tagNameFromBP": {
                  "type": "string",
                  "defaultValue": "NotSet",
                  "metadata": {
                      "description": "Tag name from blueprint"
                  }
              },
              "tagValueFromBP": {
                  "type": "string",
                  "defaultValue": "NotSet",
                  "metadata": {
                      "description": "Tag value from blueprint"
                  }
              }
          },
          "variables": {
              "storageAccountName": "[concat(uniquestring(resourceGroup().id), 'standardsa')]"
          },
          "resources": [{
              "type": "Microsoft.Storage/storageAccounts",
              "name": "[variables('storageAccountName')]",
              "apiVersion": "2016-01-01",
              "tags": {
                  "[parameters('tagNameFromBP')]": "[parameters('tagValueFromBP')]"
              },
              "location": "[resourceGroup().location]",
              "sku": {
                  "name": "[parameters('storageAccountTypeFromBP')]"
              },
              "kind": "Storage",
              "properties": {}
          }],
          "outputs": {
              "storageAccountSku": {
                  "type": "string",
                  "value": "[variables('storageAccountName')]"
              }
          }
      }
      
    • JSON ARM 模板参数文件 - artifacts\templateStorageParams.json

      {
         "storageAccountTypeFromBP": {
            "value": "[parameters('storageAccountType')]"
         },
         "tagNameFromBP": {
            "value": "[parameters('tagName')]"
         },
         "tagValueFromBP": {
            "value": "[parameters('tagValue')]"
         }
      }
      
    • Azure CLI 命令

      az blueprint artifact template create \
         --blueprint-name 'MyBlueprint' \
         --artifact-name 'templateStorage' \
         --template artifacts\templateStorage.json \
         --parameters artifacts\templateStorageParams.json \
         --resource-group-art 'storageRG'
      

      注意

      在 Mac 上使用 az blueprint 时,对于包含路径的参数值,请将 \ 替换为 /。 在这种情况下,template 的值变为 artifacts/templateStorage.jsonparameters 变为 artifacts/templateStorageParams.json

  7. 在资源组下添加角色分配。 与上一角色分配项类似,以下示例对 Owner 角色使用定义标识符,并向其提供不同于蓝图参数的另一参数。 此示例使用 GUID 为 8e3af657-a8ff-443c-a75c-2fe8c4bcb635Owner 内置角色。

    az blueprint artifact role create \
       --blueprint-name 'MyBlueprint' \
       --artifact-name 'roleOwner' \
       --role-definition-id '/providers/Microsoft.Authorization/roleDefinitions/8e3af657-a8ff-443c-a75c-2fe8c4bcb635' \
       --principal-ids "[parameters('owners')]" \
       --resource-group-art 'storageRG'
    

发布蓝图

现在已将项目添加到蓝图中,可以将其发布了。 发布后,即可将蓝图分配到订阅。

az blueprint publish --blueprint-name 'MyBlueprint' --version '{BlueprintVersion}'

{BlueprintVersion} 的值是由字母、数字和连字符组成的字符串(不含空格或其他特殊字符)。 最大长度为 20 个字符。 使用唯一且具有参考性的内容,如 v20200605-135541

分配蓝图

在使用 Azure CLI 发布蓝图后,即可将其分配给订阅。 将创建的蓝图分配到管理组层次结构下的一个订阅。 如果蓝图保存到某个订阅,则只能将其分配给该订阅。 blueprint-name 参数指定要分配的蓝图。 若要提供 namelocationidentitylockblueprint 参数,请在 az blueprint assignment create 命令上使用匹配的 Azure CLI 参数,或在 parameters JSON 文件中提供这些参数。

  1. 通过将蓝图部署分配到订阅,运行它。 由于 contributorsowners 参数要求主体的 objectIds 数组被授予角色分配,请使用 Azure Active Directory 图形 API 来收集 objectIds,以供自己的用户、组或服务主体在 parameters 中使用。

    • JSON 文件 - blueprintAssignment.json

      {
         "storageAccountType": {
             "value": "Standard_GRS"
         },
         "tagName": {
             "value": "CostCenter"
         },
         "tagValue": {
             "value": "ContosoIT"
         },
         "contributors": {
             "value": [
                 "7be2f100-3af5-4c15-bcb7-27ee43784a1f",
                 "38833b56-194d-420b-90ce-cff578296714"
             ]
         },
         "owners": {
             "value": [
                 "44254d2b-a0c7-405f-959c-f829ee31c2e7",
                 "316deb5f-7187-4512-9dd4-21e7798b0ef9"
             ]
         }
      }
      
    • Azure CLI 命令

      az blueprint assignment create \
         --name 'assignMyBlueprint' \
         --location 'westus' \
         --resource-group-value artifact_name=storageRG name=StorageAccount location=eastus \
         --parameters blueprintAssignment.json
      
    • 用户分配的托管标识

      蓝图分配也可使用用户分配的托管标识。 在这种情况下,identity-type 参数设置为 UserAssigneduser-assigned-identities 参数指定标识。 将 {userIdentity} 替换为用户分配的托管标识的名称。

      az blueprint assignment create \
         --name 'assignMyBlueprint' \
         --location 'westus' \
         --identity-type UserAssigned \
         --user-assigned-identities {userIdentity} \
         --resource-group-value artifact_name=storageRG name=StorageAccount location=eastus \
         --parameters blueprintAssignment.json
      

      用户分配的托管标识可以位于分配蓝图的用户具有访问权限的任何订阅和资源组中。

      重要

      Azure 蓝图不管理用户分配的托管标识。 用户负责分配足够的角色和权限,否则蓝图分配会失败。

清理资源

可以从订阅中删除蓝图。 通常会在不再需要项目资源时将其删除。 删除蓝图时,作为该蓝图的一部分分配的项目将保留。 若要删除蓝图分配,请使用 az blueprint assignment delete 命令:

az blueprint assignment delete --name 'assignMyBlueprint'

后续步骤

在本快速入门中,你使用 Azure CLI 创建、分配和删除了蓝图。 若要详细了解 Azure 蓝图,请继续学习蓝图生命周期文章。