Azure 资源管理器模板中的用户定义函数User-defined functions in Azure Resource Manager template

在模板中,可以创建自己的函数。Within your template, you can create your own functions. 这些函数可在模板中使用。These functions are available for use in your template. 用户定义函数不同于模板中自动可用的标准模板函数User-defined functions are separate from the standard template functions that are automatically available within your template. 当有复杂的表达式在模板中重复使用时,请创建自己的函数。Create your own functions when you have complicated expressions that are used repeatedly in your template.

本文介绍如何在 Azure 资源管理器模板中添加用户定义函数。This article describes how to add user-defined functions in your Azure Resource Manager template.

定义函数Define the function

函数需要一个命名空间值以避免命名与模板函数发生冲突。Your functions require a namespace value to avoid naming conflicts with template functions. 下面的示例演示一个返回唯一名称的函数:The following example shows a function that returns a unique name:

"functions": [
  {
    "namespace": "contoso",
    "members": {
      "uniqueName": {
        "parameters": [
          {
            "name": "namePrefix",
            "type": "string"
          }
        ],
        "output": {
          "type": "string",
          "value": "[concat(toLower(parameters('namePrefix')), uniqueString(resourceGroup().id))]"
        }
      }
    }
  }
],

使用函数Use the function

下面的示例演示一个包含用户定义函数的模板。The following example shows a template that includes a user-defined function. 它使用该函数获取存储帐户的唯一名称。It uses that function to get a unique name for a storage account. 此模板有一个名为 storageNamePrefix 的参数,它将作为参数传递给该函数。The template has a parameter named storageNamePrefix that it passes as a parameter to the function.

{
 "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
 "contentVersion": "1.0.0.0",
 "parameters": {
   "storageNamePrefix": {
     "type": "string",
     "maxLength": 11
   }
 },
 "functions": [
  {
    "namespace": "contoso",
    "members": {
      "uniqueName": {
        "parameters": [
          {
            "name": "namePrefix",
            "type": "string"
          }
        ],
        "output": {
          "type": "string",
          "value": "[concat(toLower(parameters('namePrefix')), uniqueString(resourceGroup().id))]"
        }
      }
    }
  }
 ],
 "resources": [
   {
     "type": "Microsoft.Storage/storageAccounts",
     "apiVersion": "2019-04-01",
     "name": "[contoso.uniqueName(parameters('storageNamePrefix'))]",
     "location": "China East",
     "sku": {
       "name": "Standard_LRS"
     },
     "kind": "StorageV2",
     "properties": {
       "supportsHttpsTrafficOnly": true
     }
   }
 ]
}

限制Limitations

定义用户函数时,存在一些限制:When defining a user function, there are some restrictions:

  • 该函数不能访问变量。The function can't access variables.
  • 函数仅可使用函数中定义的参数。The function can only use parameters that are defined in the function. 在用户定义函数中使用 parameters 函数时,只能使用该函数的参数。When you use the parameters function within a user-defined function, you're restricted to the parameters for that function.
  • 该函数不能调用其他用户定义的函数。The function can't call other user-defined functions.
  • 该函数不能使用 reference 函数或任何 list 函数。The function can't use the reference function or any of the list functions.
  • 该函数的参数不能具有默认值。Parameters for the function can't have default values.

后续步骤Next steps