你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

ARM 模板的部署函数

Azure 资源管理器提供以下函数,用于获取与 Azure 资源管理器模板(ARM 模板)的当前部署相关的值:

若要从资源、资源组或订阅获取值,请参阅 资源函数

Tip

建议使用 Bicep,因为它提供与 ARM 模板相同的功能,并且语法更易于使用。 若要了解详细信息,请参阅 deployment 函数。

deployer

deployer()

返回有关当前部署主体的信息。

在 Bicep 中,使用函数 deployer

返回值

此函数返回有关当前部署主体的信息,包括租户 ID、对象 ID 和用户主体名称:

{
  "objectId": "",
  "tenantId": "",
  "userPrincipalName": ""
}

Example

以下示例返回 deployer 对象:

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

前面的示例返回以下对象:

{
  "objectId":"aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb",
  "tenantId":"aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e",
  "userPrincipalName":"john.doe@contoso.com"
}

部署

deployment()

返回有关当前部署操作的信息。

在 Bicep 中,使用函数 deployment

返回值

此函数返回部署期间传递的对象。 返回的对象中的属性因以下条件而异:

  • 部署模板或模板规格。
  • 部署本地文件的模板,或部署通过 URI 访问的远程文件的模板。
  • 部署到资源组或部署到其他范围之一(Azure 订阅管理组租户)。

将本地模板部署到资源组时,该函数返回以下格式:

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

如果模板是远程的,则 templateLink 属性包含在返回的对象中。 该 templateLink 属性包含模板的 URI。 格式为:

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

有关详细信息,请参阅 “使用变量链接模板”。

将模板规格部署到资源组时,该函数返回以下格式:

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

当部署到 Azure 订阅、管理组或租户时,返回对象包含 location 属性。 部署本地模板或外部模板时包含 location 属性。 格式为:

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

部署 languageVersion 2.0 模板时,该 deployment 函数将返回有限的属性子集:

{
  "name": "",
  "location": "",
  "properties": {
    "template": {
      "contentVersion": ""
    },
    "templateLink": {
      "id": "",
      "uri": ""
    }
  }
}

Remarks

可用于 deployment() 根据父模板的 URI 链接到另一个模板:

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

如果从门户中的部署历史记录重新部署模板,则该模板将部署为本地文件。 部署函数不返回 templateLink 属性。 如果模板依赖于 templateLink 来构建指向另一个模板的链接,请不要使用门户进行重新部署, 而是使用最初部署模板时使用的命令。

Example

以下示例返回一个部署对象:

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

前面的示例返回以下对象:

{
  "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"
  }
}

对于订阅部署,以下示例返回部署对象:

{
  "$schema": "https://schema.management.azure.com/schemas/2018-05-01/subscriptionDeploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {},
  "variables": {},
  "resources": [],
  "outputs": {
    "exampleOutput": {
      "type": "object",
      "value": "[deployment()]"
    }
  }
}

环境

environment()

返回有关用于部署的 Azure 环境的信息。 该 environment() 函数不知道资源配置。 它只能为每个资源类型返回单个默认 DNS 后缀。

在 Bicep 中,使用函数 environment

Remarks

若要查看帐户的已注册环境列表,请使用 az cloud listGet-AzEnvironment

返回值

此函数返回当前 Azure 环境的属性。 以下示例显示了全局 Azure 的属性;主权云可能会返回略有不同的属性:

{
  "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

以下示例模板返回环境对象:

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

前面的示例在部署到全局 Azure 时返回以下对象:

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

parameters

parameters(parameterName)

返回一个参数值。 必须在模板的参数节中定义指定的 参数 名称。

在 Bicep 中,使用其符号名称直接引用 参数

Parameters

Parameter Required 类型 Description
parameterName Yes 字符串 要返回的参数名称。

返回值

指定的参数的值。

Remarks

通常,使用参数设置资源值。 以下示例将 Web 站点的名称设置为在部署过程中传递的参数值。

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

Example

以下示例演示函数的 parameters 简化用法:

{
  "$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": {
      "type": "string",
      "value": "[parameters('stringParameter')]"
    },
    "intOutput": {
      "type": "int",
      "value": "[parameters('intParameter')]"
    },
    "objectOutput": {
      "type": "object",
      "value": "[parameters('objectParameter')]"
    },
    "arrayOutput": {
      "type": "array",
      "value": "[parameters('arrayParameter')]"
    },
    "crossOutput": {
      "type": "string",
      "value": "[parameters('crossParameter')]"
    }
  }
}

上述示例中的默认值输出为:

Name 类型 Value
stringOutput String 选项 1
intOutput Int 1
objectOutput Object {“one”: “a”, “two”: “b”}
arrayOutput Array [1, 2, 3]
crossOutput String 选项 1

有关使用参数的详细信息,请参阅 ARM 模板中的参数

variables

variables(variableName)

返回变量的值。 必须在模板的 variables 节中定义指定的 变量 名称。

在 Bicep 中,通过使用变量的符号名称直接引用 变量

Parameters

Parameter Required 类型 Description
variableName Yes String 要返回的变量名称。

返回值

指定的变量的值。

Remarks

通常,使用变量通过只构造一次复杂值来简化模板。 以下示例为存储帐户构造唯一名称:

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

Example

以下示例返回不同的变量值:

{
  "$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": {
      "type": "string",
      "value": "[variables('var1')]"
    },
    "exampleOutput2": {
      "type": "array",
      "value": "[variables('var2')]"
    },
    "exampleOutput3": {
      "type": "string",
      "value": "[variables('var3')]"
    },
    "exampleOutput4": {
      "type": "object",
      "value": "[variables('var4')]"
    }
  }
}

上述示例中的默认值输出为:

Name 类型 Value
exampleOutput1 String myVariable
exampleOutput2 Array [1, 2, 3, 4]
exampleOutput3 String myVariable
exampleOutput4 Object {“property1”: “value1”, “property2”: “value2”}

有关使用变量的详细信息,请参阅 ARM 模板中的变量

后续步骤

若要详细了解 ARM 模板中的部分,请参阅 ARM 模板的结构和语法