從 ARM 範本建立Azure DevTest Labs環境

在本文中,您將瞭解如何從Azure Resource Manager (ARM) 範本建立Azure DevTest Labs環境。 您可以使用 DevTest Labs 環境,在多個虛擬機器 () 或平臺即服務 (PaaS) 資源,輕鬆且一致地布建多個虛擬機器的實驗室。 例如,若要為多層式 Web 應用程式或 SharePoint 伺服器陣列建立實驗室。

環境中的資源會共用相同的生命週期,而且您可以一起管理它們。 您可以追蹤實驗室環境和 PaaS 資源的成本,就像追蹤個別實驗室 VM 的成本一樣。

您可以設定Azure DevTest Labs從公用或私人 Git 存放庫使用 ARM 範本。 深入瞭解 實驗室的範本存放庫

此圖顯示如何從公用或自訂範本存放庫中的 ARM 範本建立具有Azure DevTest Labs的環境。

如果您想要使用 ARM 範本來建立Azure DevTest Labs資源,請參閱快速入門:使用 ARM 範本在 DevTest Labs 中建立實驗室

限制

當您在 DevTest Labs 中從 ARM 範本建立實驗室時,請考慮下列限制:

  • VM 自動關機不適用於 PaaS 資源。

  • 部署 ARM 範本時,並非所有實驗室原則都會進行評估。 未評估的原則包括:每個實驗室使用者的 VM 數目、每個使用者的進階 VM 數目,以及每個使用者的進階 VM 數目。 例如,您的實驗室原則可能會將使用者限制為每位只有五個 VM。 不過,使用者可以部署 ARM 環境範本,建立數十個 VM。

根據範本建立環境

您可以從Azure DevTest Labs公用範本存放庫建立環境,也可以將私人範本存放庫新增至實驗室。

瞭解如何 為您的實驗室設定環境。 例如,如何設定範本存放庫、啟用或停用公用環境,以及選取用於建立實驗室的特定範本。

若要根據範本建立環境:

  1. Azure 入口網站中,選取您的實驗室資源。

  2. 在實驗室的 [概觀] 頁面上,從頂端工具列選取 [新增]。

  3. 在 [選擇基底] 頁面上,選取要使用的 ARM 環境範本。 可用的環境範本會首先出現在基底清單中。

    此螢幕擷取畫面顯示公用環境範本。

  4. 在 [ 新增 ] 畫面上,輸入 環境名稱,並填入其他輸入欄位。

    輸入欄位的數目和類型定義于 ARM 範本中。 視需要,為範本 azuredeploy.parameters.json 檔案定義為空白或預設值的輸入欄位值輸入值。

    • 針對 secure string 參數,您可以使用來自 Azure Key Vault 的秘密。 若要了解如何在金鑰保存庫中儲存祕密,以及在建立實驗室資源時使用它們,請參閱在 Azure Key Vault 中儲存祕密

    • 在 ARM 範本檔案中,GEN-UNIQUEGEN-UNIQUE-[N]GEN-SSH-PUB-KEYGEN-PASSWORD 參數值會產生空白輸入欄位,讓使用者輸入值。

    此螢幕擷取畫面顯示 SharePoint 環境裡的 [新增] 窗格。

  5. 選取 [新增] 以建立環境。

    此環境會立即開始佈建。 您可以在實驗室 [概觀] 頁面上的 [我的環境] 底下看到佈建狀態。 佈建環境可能需要很長的時間。

  6. 環境建立完成後,展開 [ 我的環境 ] 底下的環境,以查看範本布建的 VM 和其他資源清單。

    顯示環境中 VM 清單的螢幕擷取畫面。

    部署會建立新的資源群組,來佈建 ARM 範本已定義的所有環境資源。 選取 [我的環境] 底下的環境名稱,以檢視資源群組和範本已建立的所有資源。

    此螢幕擷取畫面顯示所有環境資源的資源群組。

  7. 選取環境 VM 以查看 VM 可用的動作,例如管理設定、排程和原則。

    此螢幕擷取畫面顯示環境 VM 的可執行動作。

環境範本存放庫

透過Azure DevTest Labs,您可以從 ARM 範本建立環境。 ARM 範本可以來自兩個來源:

提示

若要建議公用範本的修訂或新增專案,請針對開放原始碼 GitHub 公用範本存放庫提交提取要求。

設定實驗室的公用環境設定

您可以將實驗室設定為從公用範本存放庫使用範本。 如果您為實驗室啟用公用範本存放庫,則使用者可以直接在Azure 入口網站中選取這些範本來快速建立環境,類似于他們在實驗室中建立 VM 的方式。

此外,您也可以選取哪些範本可供使用者用來建立環境。

建立實驗室時啟用公用環境

當您建立實驗室時,若要啟用實驗室的公用環境存放庫存取:

  1. 當您建立 DevTest Labs 資源時,請選取[基本設定] 索引標籤。

  2. 在 [公用環境] 欄位中選取 [開啟]。

    此螢幕擷取畫面顯示啟用新實驗室的公用環境。

啟用或停用現有實驗室的公用環境

針對您使用 ARM 範本建立的現有實驗室或實驗室,可能無法啟用公用環境。 若要啟用或停用現有實驗室的公用環境存放庫:

  1. Azure 入口網站中,選取您的實驗室資源。

  2. 在左側導覽中,選取 [設定和原則]。

  3. 在左側導覽的[虛擬機器基底] 底下,選取 [公用環境]。

  4. 針對此實驗室的 [啟用公用環境] 選取[是] 或 [],以啟用或停用實驗室的公用環境。

  5. 選取 [儲存]。

選取可用的公用環境範本

當您啟用公用環境時,存放庫中的所有環境範本都可用於建立環境。 只允許實驗室的特定環境:

  1. Azure 入口網站中,選取您的實驗室資源。

  2. 在左側導覽中,選取 [設定和原則]。

  3. 在左側導覽的[虛擬機器基底] 底下,選取 [公用環境]。

  4. 從清單中取消選取特定環境,使其無法供實驗室使用者使用,然後選取 [ 儲存]。

    顯示實驗室公用環境清單的螢幕擷取畫面。

設定環境使用者權限

根據預設,實驗室使用者在環境中具有 讀者 角色,而且無法變更環境資源。 例如,使用者無法停止或啟動資源。 為實驗室使用者提供 參與者 角色,讓他們能夠編輯環境資源:

  1. Azure 入口網站中,選取您的實驗室資源。

  2. 在左側導覽中,選取 [設定和原則]。

  3. 選取左側導覽中的 [實驗室設定 ]。

  4. [環境存取>資源群組使用者權限] 下,選取 [參與者],然後選取 [儲存]。

    此螢幕擷取畫面顯示設定實驗室使用者 [參與者] 權限。

自動建立環境

如果需要針對開發或測試案例建立多個環境,您可以使用 Azure PowerShell 或 Azure CLI 將環境部署自動化。

您可以使用 Azure CLI 命令 az deployment group create 來建立環境。 如需詳細資訊,請參閱使用 Resource Manager 範本與 Azure CLI 來部署資源

實驗室擁有者和管理員可以使用 Azure PowerShell,根據 ARM 範本建立 VM 和環境。

注意

建議您使用 Azure Az PowerShell 模組來與 Azure 互動。 請參閱安裝 Azure PowerShell 以開始使用。 若要了解如何遷移至 Az PowerShell 模組,請參閱將 Azure PowerShell 從 AzureRM 遷移至 Az。

若要使用 Azure PowerShell 將 ARM 環境範本部署自動化:

  1. 將 ARM 環境範本簽入 Git 存放庫,並將存放庫新增至實驗室

  2. 將下列 PowerShell 指令碼儲存至您的電腦,名稱為 deployenv.ps1。 此指令碼會呼叫 ARM 範本,以在實驗室中建立環境。

    #Requires -Module Az.Resources
    
    [CmdletBinding()]
    
    param (
    # ID of the Azure subscription for the lab
    [string] [Parameter(Mandatory=$true)] $SubscriptionId,
    
    # Name of the lab in which to create the environment
    [string] [Parameter(Mandatory=$true)] $LabName,
    
    # Name of the template repository connected to the lab
    [string] [Parameter(Mandatory=$true)] $RepositoryName,
    
    # Name of the template (folder name in the Git repository)
    [string] [Parameter(Mandatory=$true)] $TemplateName,
    
    # Name of the environment to create in the lab
    [string] [Parameter(Mandatory=$true)] $EnvironmentName,
    
    # The parameters to be passed to the template. Each parameter is prefixed with "-param_".
    # For example, if the template has a parameter named "TestVMName" with a value of "MyVMName",
    # the string in $Params will be "-param_TestVMName MyVMName".
    # This convention allows the script to dynamically handle different templates.
    [Parameter(ValueFromRemainingArguments=$true)]
        $Params
    )
    
    # Sign in to Azure, or comment out this statement to completely automate environment creation.
    Connect-AzAccount
    
    # Select the subscription that has the lab.  
    Set-AzContext -SubscriptionId $SubscriptionId | Out-Null
    
    # Get the user ID to use later in the script.
    $UserId = $((Get-AzADUser -UserPrincipalName ((Get-AzContext).Account).Id).Id)
    
    # Get the lab location.
    $lab = Get-AzResource -ResourceType "Microsoft.DevTestLab/labs" -Name $LabName
    if ($lab -eq $null) { throw "Unable to find lab $LabName in subscription $SubscriptionId." }
    
    # Get information about the repository connected to the lab.
    $repository = Get-AzResource -ResourceGroupName $lab.ResourceGroupName `
        -ResourceType 'Microsoft.DevTestLab/labs/artifactsources' `
        -ResourceName $LabName `
        -ApiVersion 2016-05-15 `
        | Where-Object { $RepositoryName -in ($_.Name, $_.Properties.displayName) } `
        | Select-Object -First 1
    if ($repository -eq $null) { throw "Unable to find repository $RepositoryName in lab $LabName." }
    
    # Get information about the ARM template base for the environment.
    $template = Get-AzResource -ResourceGroupName $lab.ResourceGroupName `
        -ResourceType "Microsoft.DevTestLab/labs/artifactSources/armTemplates" `
        -ResourceName "$LabName/$($repository.Name)" `
        -ApiVersion 2016-05-15 `
        | Where-Object { $TemplateName -in ($_.Name, $_.Properties.displayName) } `
        | Select-Object -First 1
    if ($template -eq $null) { throw "Unable to find template $TemplateName in lab $LabName." }
    
    # Build the template parameters by using parameter names and values.
    $parameters = Get-Member -InputObject $template.Properties.contents.parameters -MemberType NoteProperty | Select-Object -ExpandProperty Name
    $templateParameters = @()
    
    # Extract the custom parameters from $Params and format them as name/value pairs.
    $Params | ForEach-Object {
        if ($_ -match '^-param_(.*)' -and $Matches[1] -in $parameters) {
            $name = $Matches[1]                
        } elseif ( $name ) {
            $templateParameters += @{ "name" = "$name"; "value" = "$_" }
            $name = $null #reset name variable
        }
    }
    
    # Create an object to hold the necessary template properties.
    $templateProperties = @{ "deploymentProperties" = @{ "armTemplateId" = "$($template.ResourceId)"; "parameters" = $templateParameters }; }
    
    # Deploy the environment in the lab by using the New-AzResource command.
    New-AzResource -Location $Lab.Location `
        -ResourceGroupName $lab.ResourceGroupName `
        -Properties $templateProperties `
        -ResourceType 'Microsoft.DevTestLab/labs/users/environments' `
        -ResourceName "$LabName/$UserId/$EnvironmentName" `
        -ApiVersion '2016-05-15' -Force
    
    Write-Output "Environment $EnvironmentName completed."
    
  3. 執行指令碼,使用您自己的值來取代下列項目的範例值:

    • SubscriptionId
    • LabName
    • ResourceGroupName
    • RepositoryName
    • TemplateName (Git 存放庫中的範本資料夾)
    • EnvironmentName
    ./deployenv.ps1 -SubscriptionId "000000000-0000-0000-0000-0000000000000" -LabName "mydevtestlab" -ResourceGroupName "mydevtestlabRG000000" -RepositoryName "myRepository" -TemplateName "ARM template folder name" -EnvironmentName "myNewEnvironment"
    

下一步