您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

教程:在 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. 否则,将使用具有所提供名称的现有存储帐户。Otherwise, an existing storage account with the name provided is used.

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

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

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

本教程仅介绍使用条件的基本方案。This tutorial only covers a basic scenario of using conditions. 有关详细信息,请参阅:For more information, see:

如果还没有 Azure 订阅,可以在开始前创建一个免费帐户If you don't have an Azure subscription, create a free 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. 请参阅模板参考See the template reference.

    • Microsoft.Network/publicIPAddressesMicrosoft.Network/publicIPAddresses. 请参阅模板参考See the template reference.

    • Microsoft.Network/virtualNetworksMicrosoft.Network/virtualNetworks. 请参阅模板参考See the template reference.

    • Microsoft.Network/networkInterfacesMicrosoft.Network/networkInterfaces. 请参阅模板参考See the template reference.

    • Microsoft.Compute/virtualMachinesMicrosoft.Compute/virtualMachines. 请参阅模板参考See the template reference.

      在自定义模板之前,不妨对其进行一些基本的了解。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 whether 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 the three variables('storageAccountName') with parameters('storageAccountName') in the whole template.

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

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

  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 the storageUri property of the virtual machine resource definition with the following value:

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

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

  7. 保存更改。Save the changes.

部署模板Deploy the template

按照部署模板中的说明打开 Cloud shell 并上传修改后的模板,然后运行以下 PowerShell 脚本来部署模板。Follow the instructions in Deploy the template to open the Cloud shell and upload the revised template, and then run the following PowerShell script to deploy the template.

$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. centralus)"
$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 "$HOME/azuredeploy.json"

备注

如果 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 existing 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. 若要删除资源组,请选择“试一试”,打开 Cloud shell 。To delete the resource group, select Try it to open the Cloud shell. 若要粘贴 PowerShell 脚本,请右键单击 shell 窗格,然后选择“粘贴” 。To paste the PowerShell script, right-click the shell pane, and then select Paste.

$resourceGroupName = Read-Host -Prompt "Enter the same resource group name you used in the last procedure"
Remove-AzResourceGroup -Name $resourceGroupName

后续步骤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: