ARM 模板的部署函数Deployment functions for ARM templates

资源管理器提供了以下函数,用于获取与 Azure 资源管理器 (ARM) 模板的当前部署相关的值:Resource Manager provides the following functions for getting values related to the current deployment of your Azure Resource Manager (ARM) template:

若要从资源、资源组或订阅获取值,请参阅 Resource functions(资源函数)。To get values from resources, resource groups, or subscriptions, see Resource functions.

部署deployment

deployment()

返回有关当前部署操作的信息。Returns information about the current deployment operation.

返回值Return value

此函数返回部署期间传递的对象。This function returns the object that is passed during deployment. 返回的对象中的属性因以下情况而异:The properties in the returned object differ based on whether you are:

  • 你部署的模板是本地文件,还是通过 URI 访问的远程文件。deploying a template that is a local file or deploying a template that is a remote file accessed through a URI.
  • 你是部署到资源组,还是部署到其他作用域之一(Azure 订阅管理组租户)。deploying to a reso urce group or deploying to one of the other scopes (Azure subscription, management group, or tenant).

将本地模板部署到资源组时,该函数返回以下格式:When deploying a local template to a resource group: the function returns the following format:

{
    "name": "",
    "properties": {
        "template": {
            "$schema": "",
            "contentVersion": "",
            "parameters": {},
            "variables": {},
            "resources": [
            ],
            "outputs": {}
        },
        "templateHash": "",
        "parameters": {},
        "mode": "",
        "provisioningState": ""
    }
}

将远程模板部署到资源组时,该函数返回以下格式:When deploying a remote template to a resource group: the function returns the following format:

{
    "name": "",
    "properties": {
        "templateLink": {
            "uri": ""
        },
        "template": {
            "$schema": "",
            "contentVersion": "",
            "parameters": {},
            "variables": {},
            "resources": [],
            "outputs": {}
        },
        "templateHash": "",
        "parameters": {},
        "mode": "",
        "provisioningState": ""
    }
}

当部署到 Azure 订阅、管理组或租户时,返回对象包含 location 属性。When you deploy to an Azure subscription, management group, or tenant, the return object includes a location property. 部署本地模板或外部模板时包含 location 属性。The location property is included when deploying either a local template or an external template. 格式为:The format is:

{
    "name": "",
    "location": "",
    "properties": {
        "template": {
            "$schema": "",
            "contentVersion": "",
            "resources": [],
            "outputs": {}
        },
        "templateHash": "",
        "parameters": {},
        "mode": "",
        "provisioningState": ""
    }
}

备注Remarks

如何根据父模板的 URI,使用 deployment() 链接到另一个模板。You can use deployment() to link to another template based on the URI of the parent template.

"variables": {  
    "sharedTemplateUrl": "[uri(deployment().properties.templateLink.uri, 'shared-resources.json')]"  
}

如果从门户中的部署历史记录重新部署模板,则该模板将部署为本地文件。If you redeploy a template from the deployment history in the portal, the template is deployed as a local file. 部署函数不返回 templateLink 属性。The templateLink property isn't returned in the deployment function. 如果模板依赖于 templateLink 来构建指向另一个模板的链接,请不要使用门户进行重新部署,If your template relies on templateLink to construct a link to another template, don't use the portal to redeploy. 而是使用最初部署模板时使用的命令。Instead, use the commands you used to originally deploy the template.

示例Example

下面的示例模板返回部署对象:The following example template returns the deployment object:

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "resources": [],
    "outputs": {
        "deploymentOutput": {
            "value": "[deployment()]",
            "type" : "object"
        }
    }
}

前面的示例返回以下对象:The preceding example returns the following object:

{
  "name": "deployment",
  "properties": {
    "template": {
      "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
      "contentVersion": "1.0.0.0",
      "resources": [],
      "outputs": {
        "deploymentOutput": {
          "type": "Object",
          "value": "[deployment()]"
        }
      }
    },
    "templateHash": "13135986259522608210",
    "parameters": {},
    "mode": "Incremental",
    "provisioningState": "Accepted"
  }
}

环境environment

environment()

返回有关用于部署的 Azure 环境的信息。Returns information about the Azure environment used for deployment.

返回值Return value

此函数返回当前 Azure 环境的属性。This function returns properties for the current Azure environment. 以下示例显示了全局 Azure 的属性。The following example shows the properties for global Azure. 主权云可能会返回略有不同的属性。Sovereign clouds may return slightly different properties.

{
  "name": "",
  "gallery": "",
  "graph": "",
  "portal": "",
  "graphAudience": "",
  "activeDirectoryDataLake": "",
  "batch": "",
  "media": "",
  "sqlManagement": "",
  "vmImageAliasDoc": "",
  "resourceManager": "",
  "authentication": {
    "loginEndpoint": "",
    "audiences": [
      "",
      ""
    ],
    "tenant": "",
    "identityProvider": ""
  },
  "suffixes": {
    "acrLoginServer": "",
    "azureDatalakeAnalyticsCatalogAndJob": "",
    "azureDatalakeStoreFileSystem": "",
    "azureFrontDoorEndpointSuffix": "",
    "keyvaultDns": "",
    "sqlServerHostname": "",
    "storage": ""
  }
}

示例Example

以下示例模板返回环境对象。The following example template returns the environment object.

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "resources": [],
    "outputs": {
        "environmentOutput": {
            "value": "[environment()]",
            "type" : "object"
        }
    }
}

前面的示例在部署到全局 Azure 时返回以下对象:The preceding example returns the following object when deployed to global Azure:

{
  "name": "AzureChinaCloud",
  "gallery": "https://gallery.chinacloudapi.cn/",
  "graph": "https://graph.chinacloudapi.cn/",
  "portal": "https://portal.azure.cn",
  "graphAudience": "https://graph.chinacloudapi.cn/",
  "activeDirectoryDataLake": "https://datalake.chinacloudapi.cn/",
  "batch": "https://batch.core.chinacloudapi.cn/",
  "media": "https://rest.media.chinacloudapi.cn",
  "sqlManagement": "https://management.core.chinacloudapi.cn:8443/",
  "vmImageAliasDoc": "https://raw.githubusercontent.com/Azure/azure-rest-api-specs/master/arm-compute/quickstart-templates/aliases.json",
  "resourceManager": "https://management.chinacloudapi.cn/",
  "authentication": {
    "loginEndpoint": "https://login.chinacloudapi.cn/",
    "audiences": [
      "https://management.core.chinacloudapi.cn/",
      "https://management.chinacloudapi.cn/"
    ],
    "tenant": "common",
    "identityProvider": "AAD"
  },
  "suffixes": {
    "acrLoginServer": ".azurecr.cn",
    "azureDatalakeAnalyticsCatalogAndJob": "azuredatalakeanalytics.net",
    "azureDatalakeStoreFileSystem": "azuredatalakestore.net",
    "azureFrontDoorEndpointSuffix": "azurefd.net",
    "keyvaultDns": ".vault.azure.cn",
    "sqlServerHostname": ".database.chinacloudapi.cn",
    "storage": "core.chinacloudapi.cn"
  }
}

参数parameters

parameters(parameterName)

返回一个参数值。Returns a parameter value. 指定的参数名称必须已在模板的 parameters 节中定义。The specified parameter name must be defined in the parameters section of the template.

parametersParameters

参数Parameter 必须Required 类型Type 说明Description
parameterNameparameterName Yes stringstring 要返回的参数名称。The name of the parameter to return.

返回值Return value

指定的参数的值。The value of the specified parameter.

备注Remarks

通常,使用参数设置资源值。Typically, you use parameters to set resource values. 以下示例将 Web 站点的名称设置为在部署过程中传递的参数值。The following example sets the name of web site to the parameter value passed in during deployment.

"parameters": { 
  "siteName": {
      "type": "string"
  }
},
"resources": [
   {
      "apiVersion": "2016-08-01",
      "name": "[parameters('siteName')]",
      "type": "Microsoft.Web/Sites",
      ...
   }
]

示例Example

以下示例模板演示了 parameters 函数的简化用法。The following example template shows a simplified use of the parameters function.

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "stringParameter": {
            "type" : "string",
            "defaultValue": "option 1"
        },
        "intParameter": {
            "type": "int",
            "defaultValue": 1
        },
        "objectParameter": {
            "type": "object",
            "defaultValue": {"one": "a", "two": "b"}
        },
        "arrayParameter": {
            "type": "array",
            "defaultValue": [1, 2, 3]
        },
        "crossParameter": {
            "type": "string",
            "defaultValue": "[parameters('stringParameter')]"
        }
    },
    "variables": {},
    "resources": [],
    "outputs": {
        "stringOutput": {
            "value": "[parameters('stringParameter')]",
            "type" : "string"
        },
        "intOutput": {
            "value": "[parameters('intParameter')]",
            "type" : "int"
        },
        "objectOutput": {
            "value": "[parameters('objectParameter')]",
            "type" : "object"
        },
        "arrayOutput": {
            "value": "[parameters('arrayParameter')]",
            "type" : "array"
        },
        "crossOutput": {
            "value": "[parameters('crossParameter')]",
            "type" : "string"
        }
    }
}

上述示例中使用默认值的输出为:The output from the preceding example with the default values is:

名称Name 类型Type ValueValue
stringOutputstringOutput StringString 选项 1option 1
intOutputintOutput intInt 11
objectOutputobjectOutput ObjectObject {"one": "a", "two": "b"}{"one": "a", "two": "b"}
arrayOutputarrayOutput ArrayArray [1, 2, 3][1, 2, 3]
crossOutputcrossOutput StringString 选项 1option 1

如需详细了解如何使用参数,请参阅 Azure 资源管理器模板中的参数For more information about using parameters, see Parameters in Azure Resource Manager template.

variablesvariables

variables(variableName)

返回变量的值。Returns the value of variable. 指定的变量名称必须已在模板的 variables 节中定义。The specified variable name must be defined in the variables section of the template.

parametersParameters

参数Parameter 必须Required 类型Type 说明Description
variableNamevariableName Yes StringString 要返回的变量名称。The name of the variable to return.

返回值Return value

指定的变量的值。The value of the specified variable.

备注Remarks

通常,使用变量通过只构造一次复杂值来简化模板。Typically, you use variables to simplify your template by constructing complex values only once. 以下示例构造存储帐户的唯一名称。The following example constructs a unique name for a storage account.

"variables": {
    "storageName": "[concat('storage', uniqueString(resourceGroup().id))]"
},
"resources": [
    {
        "type": "Microsoft.Storage/storageAccounts",
        "name": "[variables('storageName')]",
        ...
    },
    {
        "type": "Microsoft.Compute/virtualMachines",
        "dependsOn": [
            "[variables('storageName')]"
        ],
        ...
    }
],

示例Example

以下示例模板返回了不同的变量值。The following example template returns different variable values.

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {},
    "variables": {
        "var1": "myVariable",
        "var2": [ 1,2,3,4 ],
        "var3": "[ variables('var1') ]",
        "var4": {
            "property1": "value1",
            "property2": "value2"
          }
    },
    "resources": [],
    "outputs": {
        "exampleOutput1": {
            "value": "[variables('var1')]",
            "type" : "string"
        },
        "exampleOutput2": {
            "value": "[variables('var2')]",
            "type" : "array"
        },
        "exampleOutput3": {
            "value": "[variables('var3')]",
            "type" : "string"
        },
        "exampleOutput4": {
            "value": "[variables('var4')]",
            "type" : "object"
        }
    }
}

上述示例中使用默认值的输出为:The output from the preceding example with the default values is:

名称Name 类型Type ValueValue
exampleOutput1exampleOutput1 StringString myVariablemyVariable
exampleOutput2exampleOutput2 ArrayArray [1, 2, 3, 4][1, 2, 3, 4]
exampleOutput3exampleOutput3 StringString myVariablemyVariable
exampleOutput4exampleOutput4 ObjectObject {"property1": "value1", "property2": "value2"}{"property1": "value1", "property2": "value2"}

如需详细了解如何使用变量,请参阅 Azure 资源管理器模板中的变量For more information about using variables, see Variables in Azure Resource Manager template.

后续步骤Next steps