教程:将变量添加到 Azure 资源管理器 Bicep 文件Tutorial: Add variables to Azure Resource Manager Bicep file

本教程介绍如何将变量添加到 Bicep 文件。In this tutorial, you learn how to add a variable to your Bicep file. 利用变量,你只需编写一次表达式,然后即可在整个 Bicep 文件中重复使用该表达式,这样就简化了 Bicep 文件。Variables simplify your Bicep files by enabling you to write an expression once and reuse it throughout the Bicep file. 完成本教程需要 7 分钟This tutorial takes 7 minutes to complete.

备注

本文包含 Bicep 示例。This article contains Bicep examples. Bicep 目前为预览版。Bicep is currently in preview. 有关详细信息,请参阅 Bicep 项目For more information, see Project Bicep.

先决条件Prerequisites

建议完成有关函数的教程,但这不是必需的。We recommend that you complete the tutorial about functions, but it's not required.

必须具有包含 Bicep 扩展的 Visual Studio Code,以及 Azure PowerShell 或 Azure CLI。You must have Visual Studio Code with the Bicep extension, and either Azure PowerShell or Azure CLI. 有关详细信息,请参阅 Bicep 工具For more information, see Bicep tools.

查看 Bicep 文件Review Bicep file

在上一篇教程的末尾,你的 Bicep 文件有以下内容:At the end of the previous tutorial, your Bicep file had the following contents:

@minLength(3)
@maxLength(24)
param storageName string

@allowed([
  'Standard_LRS'
  'Standard_GRS'
  'Standard_RAGRS'
  'Premium_LRS'
])

param storageSKU string = 'Standard_LRS'

param location string = resourceGroup().location

resource stg 'Microsoft.Storage/storageAccounts@2019-04-01' = {
  name: storageName
  location: location
  sku: {
    name: storageSKU
  }
  kind: 'StorageV2'
  properties: {
    supportsHttpsTrafficOnly: true
  }
}

存储帐户名称的参数难以使用,因为你必须提供唯一名称。The parameter for the storage account name is hard-to-use because you have to provide a unique name. 如果已完成本系列中的前述教程,你可能已厌倦了猜测某个唯一的名称。If you've completed the earlier tutorials in this series, you're probably tired of guessing a unique name. 若要解决此问题,可以添加一个变量,以便为存储帐户构造唯一名称。You solve this problem by adding a variable that constructs a unique name for the storage account.

使用变量Use variable

以下示例演示了为 Bicep 文件添加变量而做的更改,该变量用于创建一个独一无二的存储帐户名称。The following example shows the changes to add a variable to your Bicep file that creates a unique storage account name. 请复制整个文件,并将你的 Bicep 文件替换为该文件的内容。Copy the whole file and replace your Bicep file with its contents.

@minLength(3)
@maxLength(11)
param storagePrefix string

@allowed([
  'Standard_LRS'
  'Standard_GRS'
  'Standard_RAGRS'
  'Premium_LRS'
])
param storageSKU string = 'Standard_LRS'

param location string = resourceGroup().location

var uniqueStorageName = '${storagePrefix}${uniqueString(resourceGroup().id)}'

resource stg 'Microsoft.Storage/storageAccounts@2019-04-01' = {
  name: uniqueStorageName
  location: location
  sku: {
    name: storageSKU
  }
  kind: 'StorageV2'
  properties: {
    supportsHttpsTrafficOnly: true
  }
}

请注意,它包含一个名为 uniqueStorageName 的变量。Notice that it includes a variable named uniqueStorageName. 此变量使用三个函数来构造一个字符串值。This variable uses three functions to construct a string value.

resourceGroup 函数是你所熟悉的。You're familiar with the resourceGroup function. 在此示例中,你获得 id 属性而不是 location 属性,如上一教程所示。In this case, you get the id property instead of the location property, as shown in the previous tutorial. id 属性返回资源组的完整标识符,包括订阅 ID 和资源组名称。The id property returns the full identifier of the resource group, including the subscription ID and resource group name.

uniqueString 函数创建一个 13 个字符的哈希值。The uniqueString function creates a 13 character hash value. 返回的值取决于传入的参数。The returned value is determined by the parameters you pass in. 在本教程中,我们使用资源组 ID 作为哈希值的输入。For this tutorial, you use the resource group ID as the input for the hash value. 这意味着,你可以将此 Bicep 文件部署到不同的资源组,并获取不同的唯一字符串值。That means you could deploy this Bicep file to different resource groups and get a different unique string value. 但是,如果部署到同一资源组,则获得同一值。However, you get the same value if you deploy to the same resource group.

Bicep 支持字符串内插语法。Bicep supports a string interpolation syntax. 就此变量来说,它采用参数中的字符串和 uniqueString 函数中的字符串,并将二者组合成一个字符串。For this variable, it takes the string from the parameter and the string from the uniqueString function, and combines them into one string.

可以通过 storagePrefix 参数传入一个用于标识存储帐户的前缀。The storagePrefix parameter enables you to pass in a prefix that helps you identify storage accounts. 可以创建你自己的命名约定,以便在从长的资源列表完成部署后,通过该约定轻松地标识存储帐户。You can create your own naming convention that makes it easier to identify storage accounts after deployment from a long list of resources.

最后请注意,存储帐户名称现在设置为变量而非参数。Finally, notice that the storage name is now set to the variable instead of a parameter.

部署 Bicep 文件Deploy Bicep file

我们来部署这个 Bicep 文件。Let's deploy the Bicep file. 部署此 Bicep 文件比部署前面的 Bicep 文件容易,因为你只提供存储名称的前缀。Deploying this Bicep file is easier than the previous Bicep files because you provide just the prefix for the storage name.

如果尚未创建资源组,请参阅创建资源组If you haven't created the resource group, see Create resource group. 此示例假定你已按第一篇教程所述将 bicepFile 变量设置为 Bicep 文件的路径。The example assumes you've set the bicepFile variable to the path to the Bicep file, as shown in the first tutorial.

若要运行此部署 cmdlet,你必须具有 Azure PowerShell 的最新版本To run this deployment cmdlet, you must have the latest version of Azure PowerShell.

New-AzResourceGroupDeployment `
  -Name addnamevariable `
  -ResourceGroupName myResourceGroup `
  -TemplateFile $bicepFile `
  -storagePrefix "store" `
  -storageSKU Standard_LRS

备注

如果部署失败,请使用 verbose 开关获取有关正在创建的资源的信息。If the deployment failed, use the verbose switch to get information about the resources being created. 使用 debug 开关获取调试的详细信息。Use the debug switch to get more information for debugging.

验证部署Verify deployment

可以通过在 Azure 门户中浏览资源组来验证部署。You can verify the deployment by exploring the resource group from the Azure portal.

  1. 登录 Azure 门户Sign in to the Azure portal.
  2. 在左侧菜单中选择“资源组”。 From the left menu, select Resource groups.
  3. 选择已部署到的资源组。Select the resource group you deployed to.
  4. 可以看到,我们已部署一项存储帐户资源。You see that a storage account resource has been deployed. 该存储帐户的名称为 store 加上一个由随机字符组成的字符串。The name of the storage account is store plus a string of random characters.

清理资源Clean up resources

若要继续学习下一篇教程,则不需删除该资源组。If you're moving on to the next tutorial, you don't need to delete the resource group.

如果你不打算继续学习,请删除该资源组以清理部署的资源。If you're stopping now, you might want to 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.
  4. 在顶部菜单中选择“删除资源组”。Select Delete resource group from the top menu.

后续步骤Next steps

在本教程中,我们添加了一个变量,用于创建存储帐户的唯一名称。In this tutorial, you added a variable that creates a unique name for a storage account. 在下一教程中,我们从已部署的存储帐户返回一个值。In the next tutorial, you return a value from the deployed storage account.