快速入門:使用 Azure CLI 定義和指派 Azure 藍圖

重要

2026 年 7 月 11 日,藍圖(預覽版)即將淘汰。 將現有的藍圖定義和指派移轉至 範本規格部署堆疊。 藍圖成品會轉換成用來定義部署堆疊的 ARM JSON 範本或 Bicep 檔案。 若要瞭解如何將成品撰寫為 ARM 資源,請參閱:

在本教學課程中,您將瞭解如何使用 Azure 藍圖來執行一些與在組織內建立、發佈和指派藍圖相關的一些常見工作。 此技能可協助您定義一般模式,以根據 Azure Resource Manager (ARM) 範本、原則和安全性,開發可重複使用且快速部署的設定。

必要條件

  • 如果您沒有 Azure 訂用帳戶,請在開始前建立免費帳戶
  • 如果您之前未使用 Azure 藍圖,請透過 az provider register --namespace Microsoft.BlueprintAzure CLI 向 註冊資源提供者。

Azure Cloud Shell

Azure Cloud Shell 是裝載於 Azure 中的互動式殼層環境,可在瀏覽器中使用。 您可以使用 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 上的 Bash、 Cloud 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 時,會 先建立藍圖 物件。 若要新增具有參數的每個 成品 ,您可以事先在初始 藍圖上定義參數。

  1. 建立初始 藍圖 物件。 參數 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. 在訂用帳戶上新增角色指派。 在下列範例中,授與指定角色的主體身分識別會設定為藍圖指派期間所設定的參數。 此範例會 Contributor 使用內建角色,且 GUID 為 b24988ac-6180-42a0-ab88-20f7382dd24c

    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. 在訂用帳戶上新增原則指派。 此範例會使用內 Apply tag and its default value to resource groups 建原則,且 GUID 為 49c88fc8-6fd1-46fd-a676-f12d1d3a4c71

    • 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
      

      注意

      當您 az blueprint 在 Mac 上使用 時,請將 取代 \/ 包含路徑的參數值。 在這裡情況下,的值 parametersartifacts/policyTags.json變成 。

  5. 在訂用帳戶上新增記憶體標籤的另一個原則指派(藉由重複使用 storageAccountType_ parameter)。 這個額外的原則指派成品示範藍圖上定義的參數可供多個成品使用。 在此範例中,您會使用 storageAccountType 來設定資源群組上的標記。 此值提供您在下一個步驟中建立之記憶體帳戶的相關信息。 此範例會使用內 Apply tag and its default value to resource groups 建原則,且 GUID 為 49c88fc8-6fd1-46fd-a676-f12d1d3a4c71

    • JSON 檔案 - artifacts\policy 儲存體 Tags.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
      

      注意

      當您 az blueprint 在 Mac 上使用 時,請將 取代 \/ 包含路徑的參數值。 在這裡情況下,的值 parametersartifacts/policyStorageTags.json變成 。

  6. 在資源群組底下新增範本。 template ARM 樣本的參數包含範本的一般 JSON 元件。 此範本也會藉由將每個 參數傳遞至範本, storageAccountType重複使用、 tagNametagValue 藍圖參數。 藍圖參數可使用 參數 parameters提供給範本,並在範本 JSON 內使用索引鍵/值組來插入值。 藍圖和範本參數名稱可能相同。

    • JSON ARM 範本檔案 - artifacts\template 儲存體.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\template 儲存體 Params.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'
      

      注意

      當您 az blueprint 在 Mac 上使用 時,請將 取代 \/ 包含路徑的參數值。 在這裡情況下,的值 templateartifacts/templateStorage.json變成 ,並 parameters 變成 artifacts/templateStorageParams.json

  7. 在資源群組下新增角色指派。 與先前的角色指派項目類似,下列範例會使用角色的定義標識碼 Owner ,並提供與藍圖不同的參數。 此範例會 Owner 使用內建角色,且 GUID 為 8e3af657-a8ff-443c-a75c-2fe8c4bcb635

    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 會指定要指派的藍圖。 若要提供 、、、 identity和參數,請在 命令上使用相符的 az blueprint assignment createnameAzure CLI 參數,或在參數 JSON 檔案中提供它們locationblueprintlock

  1. 將藍圖部署指派給訂用帳戶,以執行藍圖部署。 contributors由於 和 owners 參數需要授與角色指派的主體陣列objectIds,因此請使用 Azure Active Directory Graph 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 參數會設定為 UserAssigned,而 user-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 藍圖,請繼續進行藍圖生命週期一文。