教程:在 Azure 资源管理器模板中使用条件Tutorial: Use condition in Azure Resource Manager templates

了解如何根据条件部署 Azure 资源。Learn how to deploy Azure resources based on conditions.

设置资源部署顺序教程介绍如何创建虚拟机、虚拟网络以及其他一些依赖资源(包括存储帐户)。In the Set resource deployment order tutorial, you create a virtual machine, a virtual network, and some other dependent resources including a storage account. 无需每次都创建新的存储帐户,可让用户选择是创建新的存储帐户还是使用现有的存储帐户。Instead of creating a new storage account every time, you let people choose between creating a new storage account and using an existing storage account. 为实现此目的,需定义附加的参数。To accomplish this goal, you define an additional parameter. 如果参数值为“new”,则创建新存储帐户。If the value of the parameter is "new", a new storage account is created.

资源管理器模板使用条件关系图

本教程涵盖以下任务:This tutorial covers the following tasks:

  • 打开快速入门模板Open a QuickStart template
  • 修改模板Modify the template
  • 部署模板Deploy the template
  • 清理资源Clean up resources

如果没有 Azure 订阅,请在开始前创建一个试用帐户If you don't have an Azure subscription, create a trial account before you begin.

先决条件Prerequisites

若要完成本文,需要做好以下准备:To complete this article, you need:

打开快速入门模板Open a Quickstart template

Azure 快速入门模板是资源管理器模板的存储库。Azure QuickStart Templates is a repository for Resource Manager templates. 无需从头开始创建模板,只需找到一个示例模板并对其自定义即可。Instead of creating a template from scratch, you can find a sample template and customize it. 本教程中使用的模板称为部署简单的 Windows VMThe template used in this tutorial is called Deploy a simple Windows VM.

  1. 在 Visual Studio Code 中,选择“文件”>“打开文件”。From Visual Studio Code, select File>Open File.

  2. 在“文件名”中粘贴以下 URL:In File name, paste the following URL:

    https://raw.githubusercontent.com/Azure/azure-quickstart-templates/master/101-vm-simple-windows/azuredeploy.json
    
  3. 选择“打开”以打开该文件。Select Open to open the file.

  4. 有五个通过此模板定义的资源:There are five resources defined by the template:

    • Microsoft.Storage/storageAccountsMicrosoft.Storage/storageAccounts.
    • Microsoft.Network/publicIPAddressesMicrosoft.Network/publicIPAddresses.
    • Microsoft.Network/virtualNetworksMicrosoft.Network/virtualNetworks.
    • Microsoft.Network/networkInterfacesMicrosoft.Network/networkInterfaces.
    • Microsoft.Compute/virtualMachinesMicrosoft.Compute/virtualMachines.

    在自定义模板之前,不妨对其进行一些基本的了解。It is helpful to get some basic understanding of the template before customizing it.

  5. 选择“文件”>“另存为”,将该文件的副本保存到名为 azuredeploy.json 的本地计算机。Select File>Save As to save a copy of the file to your local computer with the name azuredeploy.json.

修改模板Modify the template

对现有模板进行两项更改:Make two changes to the existing template:

  • 添加存储帐户名称参数。Add a storage account name parameter. 用户可以指定新的存储帐户名称或现有的存储帐户名称。Users can specify either a new storage account name or an existing storage account name.
  • 添加名为 newOrExisting 的新参数。Add a new parameter called newOrExisting. 部署使用此参数来确定是要创建新存储帐户还是使用现有的存储帐户。The deployment uses this parameter to determine where to create a new storage account or use an existing storage account.

下面是进行更改的过程:Here is the procedure to make the changes:

  1. 在 Visual Studio Code 中打开 azuredeploy.jsonOpen azuredeploy.json in Visual Studio Code.

  2. 在整个模板中,将 variables('storageAccountName') 替换为 parameters('storageAccountName')Replace variables('storageAccountName') with parameters('storageAccountName') in the whole template. variables('storageAccountName') 有三种外观。There are three appearances of variables('storageAccountName').

  3. 删除以下变量定义:Remove the following variable definition:

    "storageAccountName": "[concat(uniquestring(resourceGroup().id), 'sawinvm')]",
    
  4. 将以下两个参数添加到模板:Add the following two parameters to the template:

    "storageAccountName": {
      "type": "string"
    },
    "newOrExisting": {
      "type": "string", 
      "allowedValues": [
        "new", 
        "existing"
      ]
    },
    

    更新的参数定义如下所示:The updated parameters definition looks like:

    在资源管理器中使用条件

  5. 将以下行添加到存储帐户定义的开头。Add the following line to the beginning of the storage account definition.

    "condition": "[equals(parameters('newOrExisting'),'new')]",
    

    该条件检查名为 newOrExisting 的参数的值。The condition checks the value of a parameter called newOrExisting. 如果参数值为 new,则部署将创建存储帐户。If the parameter value is new, the deployment creates the storage account.

    更新的存储帐户定义如下所示:The updated storage account definition looks like:

    在资源管理器中使用条件

  6. storageUri 更新为以下值:Update storageUri with the following value:

    "storageUri": "[concat('https://', parameters('storageAccountName'), '.blob.core.chinacloudapi.cn')]"
    

    如果使用另一资源组中的现有存储帐户,则此更改是必需的。This change is necessary when you use an existing storage account under a different resource group.

  7. 保存更改。Save the changes.

部署模板Deploy the template

Note

本文进行了更新,以便使用新的 Azure PowerShell Az 模块。This article has been updated to use the new Azure PowerShell Az module. 你仍然可以使用 AzureRM 模块,至少在 2020 年 12 月之前,它将继续接收 bug 修补程序。You can still use the AzureRM module, which will continue to receive bug fixes until at least December 2020. 若要详细了解新的 Az 模块和 AzureRM 兼容性,请参阅新 Azure Powershell Az 模块简介To learn more about the new Az module and AzureRM compatibility, see Introducing the new Azure PowerShell Az module. 有关 Az 模块安装说明,请参阅安装 Azure PowerShellFor Az module installation instructions, see Install Azure PowerShell.

遵照部署模板中的说明部署模板。Follow the instructions in Deploy the template to deploy the template.

使用 Azure PowerShell 部署模板时,需要指定一个附加参数。When you deploy the template using Azure PowerShell, you need to specify one additional parameter. 若要提高安全性,请使用为虚拟机管理员帐户生成的密码。To increase security, use a generated password for the virtual machine administrator account. 请参阅先决条件See Prerequisites.

$resourceGroupName = Read-Host -Prompt "Enter the resource group name"
$storageAccountName = Read-Host -Prompt "Enter the storage account name"
$newOrExisting = Read-Host -Prompt "Create new or use existing (Enter new or existing)"
$location = Read-Host -Prompt "Enter the Azure location (i.e. chinaeast)"
$vmAdmin = Read-Host -Prompt "Enter the admin username"
$vmPassword = Read-Host -Prompt "Enter the admin password" -AsSecureString
$dnsLabelPrefix = Read-Host -Prompt "Enter the DNS Label prefix"

New-AzResourceGroup -Name $resourceGroupName -Location $location
New-AzResourceGroupDeployment `
    -ResourceGroupName $resourceGroupName `
    -adminUsername $vmAdmin `
    -adminPassword $vmPassword `
    -dnsLabelPrefix $dnsLabelPrefix `
    -storageAccountName $storageAccountName `
    -newOrExisting $newOrExisting `
    -TemplateFile azuredeploy.json

Note

如果 newOrExistingnew,但具有指定存储帐户名称的存储帐户已存在,则部署将会失败。The deployment fails if newOrExisting is new, but the storage account with the storage account name specified already exists.

请尝试创建 newOrExisting 设置为“existing”的另一个部署,并指定现有存储帐户。Try making another deployment with newOrExisting set to "existing" and specify an exiting storage account. 若要提前创建存储帐户,请参阅创建存储帐户To create a storage account beforehand, see Create a storage account.

清理资源Clean up resources

不再需要 Azure 资源时,请通过删除资源组来清理部署的资源。When the Azure resources are no longer needed, clean up the resources you deployed by deleting the resource group.

  1. 在 Azure 门户上的左侧菜单中选择“资源组”。From the Azure portal, select Resource group from the left menu.
  2. 在“按名称筛选”字段中输入资源组名称。Enter the resource group name in the Filter by name field.
  3. 选择资源组名称。Select the resource group name. 应会看到,该资源组中总共有六个资源。You shall see a total of six resources in the resource group.
  4. 在顶部菜单中选择“删除资源组”。Select Delete resource group from the top menu.

后续步骤Next steps

在本教程中,我们开发了一个允许用户选择创建新存储帐户或使用现有存储帐户的模板。In this tutorial, you developed a template that allows users to choose between creating a new storage account and using an existing storage account. 若要了解如何从 Azure Key Vault 检索机密并在模板部署中使用这些机密作为密码,请参阅:To learn how to retrieve secrets from Azure Key Vault, and use the secrets as passwords in the template deployment, see: