用于 Azure Resource Manager 模板的部署函数
Resource Manager 提供以下函数,用于从与部署相关的模板和值部分获取值:
若要从资源、资源组或订阅获取值,请参阅 Resource functions(资源函数)。
部署
deployment()
返回有关当前部署操作的信息。
返回值
此函数返回部署期间传递的对象。 根据部署对象是作为链接还是内联对象传递,所返回对象中的属性将有所不同。 如果部署对象是以内联形式传递的(例如使用 Azure PowerShell 中的 -TemplateFile 参数指向本地文件时),所返回的对象采用以下格式:
{
"name": "",
"properties": {
"template": {
"$schema": "",
"contentVersion": "",
"parameters": {},
"variables": {},
"resources": [
],
"outputs": {}
},
"parameters": {},
"mode": "",
"provisioningState": ""
}
}
如果对象是以链接形式传递的(例如使用 -TemplateUri 参数指向远程文件时),所返回的对象采用以下格式:
{
"name": "",
"properties": {
"templateLink": {
"uri": ""
},
"template": {
"$schema": "",
"contentVersion": "",
"parameters": {},
"variables": {},
"resources": [],
"outputs": {}
},
"parameters": {},
"mode": "",
"provisioningState": ""
}
}
备注
如何根据父模板的 URI,使用 deployment() 链接到另一个模板。
"variables": {
"sharedTemplateUrl": "[uri(deployment().properties.templateLink.uri, 'shared-resources.json')]"
}
示例
下面的示例模板返回部署对象:
{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"resources": [],
"outputs": {
"subscriptionOutput": {
"value": "[deployment()]",
"type" : "object"
}
}
}
前面的示例返回以下对象:
{
"name": "deployment",
"properties": {
"template": {
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"resources": [],
"outputs": {
"subscriptionOutput": {
"type": "Object",
"value": "[deployment()]"
}
}
},
"parameters": {},
"mode": "Incremental",
"provisioningState": "Accepted"
}
}
要使用 Azure CLI 部署此示例模板,请使用:
az group deployment create -g functionexamplegroup --template-uri https://raw.githubusercontent.com/Azure/azure-docs-json-samples/master/azure-resource-manager/functions/deployment.json
要使用 PowerShell 部署此示例模板,请使用:
New-AzureRmResourceGroupDeployment -ResourceGroupName functionexamplegroup -TemplateUri https://raw.githubusercontent.com/Azure/azure-docs-json-samples/master/azure-resource-manager/functions/deployment.json
参数
parameters(parameterName)
返回一个参数值。 指定的参数名称必须已在模板的 parameters 节中定义。
Parameters
参数 | 必选 | 类型 | 说明 |
---|---|---|---|
parameterName | 是 | 字符串 | 要返回的参数名称。 |
返回值
指定的参数的值。
备注
通常,使用参数设置资源值。 以下示例将 Web 站点的名称设置为在部署过程中传递的参数值。
"parameters": {
"siteName": {
"type": "string"
}
},
"resources": [
{
"apiVersion": "2016-08-01",
"name": "[parameters('siteName')]",
"type": "Microsoft.Web/Sites",
...
}
]
示例
以下示例模板演示了 parameters 函数的简化用法。
{
"$schema": "https://schema.management.azure.com/schemas/2015-01-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"
}
}
}
上述示例中使用默认值的输出为:
名称 | 类型 | 值 |
---|---|---|
stringOutput | String | 选项 1 |
intOutput | int | 1 |
objectOutput | 对象 | {"one": "a", "two": "b"} |
arrayOutput | Array | [1, 2, 3] |
crossOutput | String | 选项 1 |
要使用 Azure CLI 部署此示例模板,请使用:
az group deployment create -g functionexamplegroup --template-uri https://raw.githubusercontent.com/Azure/azure-docs-json-samples/master/azure-resource-manager/functions/parameters.json
要使用 PowerShell 部署此示例模板,请使用:
New-AzureRmResourceGroupDeployment -ResourceGroupName functionexamplegroup -TemplateUri https://raw.githubusercontent.com/Azure/azure-docs-json-samples/master/azure-resource-manager/functions/parameters.json
variables
variables(variableName)
返回变量的值。 指定的变量名称必须已在模板的 variables 节中定义。
Parameters
参数 | 必选 | 类型 | 说明 |
---|---|---|---|
variableName | 是 | 字符串 | 要返回的变量名称。 |
返回值
指定的变量的值。
备注
通常,使用变量通过只构造一次复杂值来简化模板。 以下示例构造存储帐户的唯一名称。
"variables": {
"storageName": "[concat('storage', uniqueString(resourceGroup().id))]"
},
"resources": [
{
"type": "Microsoft.Storage/storageAccounts",
"name": "[variables('storageName')]",
...
},
{
"type": "Microsoft.Compute/virtualMachines",
"dependsOn": [
"[variables('storageName')]"
],
...
}
],
示例
以下示例模板返回了不同的变量值。
{
"$schema": "https://schema.management.azure.com/schemas/2015-01-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"
}
}
}
上述示例中使用默认值的输出为:
名称 | 类型 | 值 |
---|---|---|
exampleOutput1 | String | myVariable |
exampleOutput2 | Array | [1, 2, 3, 4] |
exampleOutput3 | String | myVariable |
exampleOutput4 | 对象 | {"property1": "value1", "property2": "value2"} |
要使用 Azure CLI 部署此示例模板,请使用:
az group deployment create -g functionexamplegroup --template-uri https://raw.githubusercontent.com/Azure/azure-docs-json-samples/master/azure-resource-manager/functions/variables.json
要使用 PowerShell 部署此示例模板,请使用:
New-AzureRmResourceGroupDeployment -ResourceGroupName functionexamplegroup -TemplateUri https://raw.githubusercontent.com/Azure/azure-docs-json-samples/master/azure-resource-manager/functions/variables.json
后续步骤
- 有关 Azure Resource Manager 模板中各部分的说明,请参阅 Authoring Azure Resource Manager templates(创作 Azure Resource Manager 模板)。
- 若要合并多个模板,请参阅将链接的模板与 Azure Resource Manager 配合使用。
- 若要在创建资源类型时迭代指定的次数,请参阅在 Azure Resource Manager 中创建多个资源实例。
- 若要查看如何部署已创建的模板,请参阅使用 Azure Resource Manager 模板部署应用程序。