比较模板的 JSON 和 BicepComparing JSON and Bicep for templates

本文将 Azure 资源管理器模板(ARM 模板)的 Bicep 语法与 JSON 语法进行了比较。This article compares Bicep syntax with JSON syntax for Azure Resource Manager templates (ARM templates). 在大多数情况下,Bicep 提供的语法比 JSON 中的等效语法更简单。In most cases, Bicep provides syntax that is less verbose than the equivalent in JSON.

如果你知道如何使用 JSON 开发 ARM 模板,请使用以下示例来了解 Bicep 的等效语法。If you're familiar with using JSON to develop ARM templates, use the following examples to learn about the equivalent syntax for Bicep.

表达式Expressions

若要创作表达式,请运行以下命令:To author an expression:

func()
"[func()]"

参数Parameters

若要使用默认值声明参数,请运行以下命令:To declare a parameter with a default value:

param demoParam string = 'Contoso'
"parameters": {
  "demoParam": {
    "type": "string",
    "defaultValue": "Contoso"
  }
}

若要获取参数值,请运行以下命令:To get a parameter value:

demoParam
[parameters('demoParam'))]

变量Variables

若要声明变量,请运行以下命令:To declare a variable:

var demoVar = 'example value'
"variables": {
  "demoVar": "example value"
},

若要获取变量值,请运行以下命令:To get a variable value:

demoVar
[variables('demoVar'))]

字符串Strings

若要连接字符串,请运行以下命令:To concatenate strings:

'${namePrefix}-vm'
[concat(parameters('namePrefix'), '-vm')]

逻辑运算符Logical operators

若要返回逻辑 AND,请运行以下命令:To return the logical AND:

isMonday && isNovember
[and(parameter('isMonday'), parameter('isNovember'))]

若要按条件设置值,请运行以下命令:To conditionally set a value:

isMonday ? 'valueIfTrue' : 'valueIfFalse'
[if(parameters('isMonday'), 'valueIfTrue', 'valueIfFalse')]

部署范围Deployment scope

若要设置部署的目标范围,请运行以下命令:To set the target scope of the deployment:

targetScope = 'subscription'
"$schema": "https://schema.management.azure.com/schemas/2018-05-01/subscriptionDeploymentTemplate.json#"

资源Resources

若要声明资源,请运行以下命令:To declare a resource:

resource vm 'Microsoft.Compute/virtualMachines@2020-06-01' = {
  ...
}
"resources": [ 
  { 
    "type": "Microsoft.Compute/virtualMachines", 
    "apiVersion": "2020-06-01", 
    ... 
  } 
]

若要按条件部署资源,请运行以下命令:To conditionally deploy a resource:

resource vm 'Microsoft.Compute/virtualMachines@2020-06-01' = if(deployVM) {
  ...
}
"resources": [ 
  {
    "condition": "[parameters('deployVM')]",
    "type": "Microsoft.Compute/virtualMachines", 
    "apiVersion": "2020-06-01", 
    ... 
  } 
]

若要设置资源属性,请运行以下命令:To set resource property:

sku: '2016-Datacenter'
"sku": "2016-Datacenter",

若要获取模板中资源的资源 ID,请运行以下命令:To get resource ID of resource in the template:

nic1.id
[resourceId('Microsoft.Network/networkInterfaces', variables('nic1Name'))]

循环Loops

若要循环访问数组或计数中的项,请运行以下命令:To iterate over items in an array or count:

[for storageName in storageAccounts: {
  ...
}]
"copy": {
  "name": "storagecopy",
  "count": "[length(parameters('storageAccounts'))]"
},
...

资源依赖关系Resource dependencies

若要设置资源之间的依赖关系,请运行以下命令:To set dependency between resources:

对于 Bicep,请依赖于依赖关系的自动检测,或手动设置依赖关系。For Bicep, either rely on automatic detection of dependencies or manually set dependency.

dependsOn: [ stg ]
"dependsOn": ["[resourceId('Microsoft.Storage/storageAccounts', 'parameters('storageAccountName'))]"]

引用资源Reference resources

若要从模板中的资源获取属性,请运行以下命令:To get a property from a resource in the template:

diagsAccount.properties.primaryEndpoints.blob
[reference(resourceId('Microsoft.Storage/storageAccounts', variables('diagStorageAccountName'))).primaryEndpoints.blob]

若要从未部署在模板中的现有资源获取属性,请运行以下命令:To get a property from an existing resource that isn't deployed in the template:

resource stg 'Microsoft.Storage/storageAccounts@2019-06-01' existing = {
  name: storageAccountName
}

// use later in template as often as needed
stg.properties.primaryEndpoints.blob
// required every time the property is needed
"[reference(resourceId('Microsoft.Storage/storageAccounts/', parameters('storageAccountName')), '2019-06-01').primaryEndpoints.blob]"

输出Outputs

若要从模板中的资源输出属性,请运行以下命令:To output a property from a resource in the template:

output hostname string = publicIP.properties.dnsSettings.fqdn
"outputs": {
  "hostname": {
    "type": "string",
    "value": "[reference(resourceId('Microsoft.Network/publicIPAddresses', variables('publicIPAddressName'))).dnsSettings.fqdn]"
  },
}

代码重用Code reuse

若要将解决方案隔离到多个文件,请运行以下命令:To separate a solution into multiple files:

建议Recommendations

  • 如果可能,请避免在 Bicep 文件中使用 referenceresourceId 函数。When possible, avoid using the reference and resourceId functions in your Bicep file. 引用同一 Bicep 部署中的资源时,请改用资源标识符。When you reference a resource in the same Bicep deployment, use the resource identifier instead. 例如,如果你在 Bicep 文件中部署了一个资源并使用 stg 作为资源标识符,请使用类似于 stg.idstg.properties.primaryEndpoints.blob 的语法来获取属性值。For example, if you've deployed a resource in your Bicep file with stg as the resource identifier, use syntax like stg.id or stg.properties.primaryEndpoints.blob to get property values. 通过使用资源标识符,可以在资源之间创建隐式依赖关系。By using the resource identifier, you create an implicit dependency between resources. 不需要使用 dependsOn 属性显式设置依赖关系。You don't need to explicitly set the dependency with the dependsOn property.
  • 如果资源未在 Bicep 文件中部署,你仍可以使用 现有 关键字获取对该资源的符号引用。If the resource isn't deployed in the Bicep file, you can still get a symbolic reference to the resource using the existing keyword.
  • 请为标识符使用一致的大小写。Use consistent casing for identifiers. 如果你不确定要使用哪种类型的大小写,请尝试使用驼峰式大小写。If you're unsure what type of casing to use, try camel casing. 例如,param myCamelCasedParameter stringFor example, param myCamelCasedParameter string.
  • 仅当需要使用说明向用户提供基本信息时,才向参数中添加说明。Add a description to a parameter only when the description provides essential information to users. 对于某些信息,你可以使用 // 注释。You can use // comments for some information.

后续步骤Next steps