用于 Azure Resource Manager 模板的数值函数
Resource Manager 提供以下用于处理整数的函数:
add
add(operand1, operand2)
返回提供的两个整数的总和。
Parameters
参数 | 必选 | 类型 | 说明 |
---|---|---|---|
operand1 | 是 | int | 被加数。 |
operand2 | 是 | int | 加数。 |
返回值
一个整数,包含参数的总和。
示例
以下示例模板将添加两个参数。
{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"first": {
"type": "int",
"defaultValue": 5,
"metadata": {
"description": "First integer to add"
}
},
"second": {
"type": "int",
"defaultValue": 3,
"metadata": {
"description": "Second integer to add"
}
}
},
"resources": [
],
"outputs": {
"addResult": {
"type": "int",
"value": "[add(parameters('first'), parameters('second'))]"
}
}
}
上述示例中使用默认值的输出为:
名称 | 类型 | 值 |
---|---|---|
addResult | int | 8 |
要使用 Azure CLI 部署此示例模板,请使用:
az group deployment create -g functionexamplegroup --template-uri https://raw.githubusercontent.com/Azure/azure-docs-json-samples/master/azure-resource-manager/functions/add.json
要使用 PowerShell 部署此示例模板,请使用:
New-AzureRmResourceGroupDeployment -ResourceGroupName functionexamplegroup -TemplateUri https://raw.githubusercontent.com/Azure/azure-docs-json-samples/master/azure-resource-manager/functions/add.json
copyIndex
copyIndex(loopName, offset)
返回迭代循环的索引。
Parameters
参数 | 必选 | 类型 | 说明 |
---|---|---|---|
loopName | 否 | 字符串 | 用于获取迭代的循环的名称。 |
offset | 否 | int | 要添加到的从零开始的迭代值的数字。 |
备注
此函数始终用于 copy 对象。 如果没有提供任何值作为 偏移量,则返回当前迭代值。 迭代值从零开始。 定义资源或变量时,你可以使用迭代循环。
loopName 属性可用于指定 copyIndex 是引用资源迭代还是引用属性迭代。 如果没有为 loopName 提供值,则将使用当前的资源类型迭代。 在属性上迭代时,请为 loopName 提供值。
有关如何使用 copyIndex 的完整说明,请参阅 Create multiple instances of resources in Azure Resource Manager(在 Azure Resource Manager 中创建多个资源实例)。
有关定义变量时使用“copyIndex”的示例,请参阅变量。
示例
以下示例显示名称中包含 copy 循环和索引值。
"resources": [
{
"name": "[concat('examplecopy-', copyIndex())]",
"type": "Microsoft.Web/sites",
"copy": {
"name": "websitescopy",
"count": "[parameters('count')]"
},
...
}
]
返回值
一个表示迭代的当前索引的整数。
div
div(operand1, operand2)
返回提供的两个整数在整除后的商。
Parameters
参数 | 必选 | 类型 | 说明 |
---|---|---|---|
operand1 | 是 | int | 被除数。 |
operand2 | 是 | int | 除数。 不能为 0。 |
返回值
一个表示商的整数。
示例
以下示例模板将一个参数除以另一个参数。
{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"first": {
"type": "int",
"defaultValue": 8,
"metadata": {
"description": "Integer being divided"
}
},
"second": {
"type": "int",
"defaultValue": 3,
"metadata": {
"description": "Integer used to divide"
}
}
},
"resources": [
],
"outputs": {
"divResult": {
"type": "int",
"value": "[div(parameters('first'), parameters('second'))]"
}
}
}
上述示例中使用默认值的输出为:
名称 | 类型 | 值 |
---|---|---|
divResult | int | 2 |
要使用 Azure CLI 部署此示例模板,请使用:
az group deployment create -g functionexamplegroup --template-uri https://raw.githubusercontent.com/Azure/azure-docs-json-samples/master/azure-resource-manager/functions/div.json
要使用 PowerShell 部署此示例模板,请使用:
New-AzureRmResourceGroupDeployment -ResourceGroupName functionexamplegroup -TemplateUri https://raw.githubusercontent.com/Azure/azure-docs-json-samples/master/azure-resource-manager/functions/div.json
float
float(arg1)
将值转换为浮点数。 仅当将自定义参数传递给应用程序(例如,逻辑应用)时,才使用此函数。
Parameters
参数 | 必选 | 类型 | 说明 |
---|---|---|---|
arg1 | 是 | 字符串或整数 | 要转换为浮点数的值。 |
返回值
一个浮点数。
示例
以下示例演示如何使用 float 将参数传递给逻辑应用:
{
"type": "Microsoft.Logic/workflows",
"properties": {
...
"parameters": {
"custom1": {
"value": "[float('3.0')]"
},
"custom2": {
"value": "[float(3)]"
},
int
int(valueToConvert)
将指定的值转换为整数。
Parameters
参数 | 必选 | 类型 | 说明 |
---|---|---|---|
valueToConvert | 是 | 字符串或整数 | 要转换为整数的值。 |
返回值
转换后的值的整数。
示例
以下示例模板将用户提供的参数值转换为整数。
{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"stringToConvert": {
"type": "string",
"defaultValue": "4"
}
},
"resources": [
],
"outputs": {
"intResult": {
"type": "int",
"value": "[int(parameters('stringToConvert'))]"
}
}
}
上述示例中使用默认值的输出为:
名称 | 类型 | 值 |
---|---|---|
intResult | int | 4 |
要使用 Azure CLI 部署此示例模板,请使用:
az group deployment create -g functionexamplegroup --template-uri https://raw.githubusercontent.com/Azure/azure-docs-json-samples/master/azure-resource-manager/functions/int.json
要使用 PowerShell 部署此示例模板,请使用:
New-AzureRmResourceGroupDeployment -ResourceGroupName functionexamplegroup -TemplateUri https://raw.githubusercontent.com/Azure/azure-docs-json-samples/master/azure-resource-manager/functions/int.json
max
max (arg1)
返回整数数组或逗号分隔的整数列表中的最大值。
Parameters
参数 | 必选 | 类型 | 说明 |
---|---|---|---|
arg1 | 是 | 整数数组或逗号分隔的整数列表 | 要获取最大值的集合。 |
返回值
一个整数,表示集合中的最大值。
示例
以下示例模板演示如何对整数数组和整数列表使用 max:
{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"arrayToTest": {
"type": "array",
"defaultValue": [0,3,2,5,4]
}
},
"resources": [],
"outputs": {
"arrayOutput": {
"type": "int",
"value": "[max(parameters('arrayToTest'))]"
},
"intOutput": {
"type": "int",
"value": "[max(0,3,2,5,4)]"
}
}
}
上述示例中使用默认值的输出为:
名称 | 类型 | 值 |
---|---|---|
arrayOutput | int | 5 |
intOutput | int | 5 |
要使用 Azure CLI 部署此示例模板,请使用:
az group deployment create -g functionexamplegroup --template-uri https://raw.githubusercontent.com/Azure/azure-docs-json-samples/master/azure-resource-manager/functions/max.json
要使用 PowerShell 部署此示例模板,请使用:
New-AzureRmResourceGroupDeployment -ResourceGroupName functionexamplegroup -TemplateUri https://raw.githubusercontent.com/Azure/azure-docs-json-samples/master/azure-resource-manager/functions/max.json
min
min (arg1)
返回整数数组或逗号分隔的整数列表中的最小值。
Parameters
参数 | 必选 | 类型 | 说明 |
---|---|---|---|
arg1 | 是 | 整数数组或逗号分隔的整数列表 | 要获取最小值的集合。 |
返回值
一个整数,表示集合中的最小值。
示例
以下示例模板演示如何对整数数组和整数列表使用 min:
{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"arrayToTest": {
"type": "array",
"defaultValue": [0,3,2,5,4]
}
},
"resources": [],
"outputs": {
"arrayOutput": {
"type": "int",
"value": "[min(parameters('arrayToTest'))]"
},
"intOutput": {
"type": "int",
"value": "[min(0,3,2,5,4)]"
}
}
}
上述示例中使用默认值的输出为:
名称 | 类型 | 值 |
---|---|---|
arrayOutput | int | 0 |
intOutput | int | 0 |
要使用 Azure CLI 部署此示例模板,请使用:
az group deployment create -g functionexamplegroup --template-uri https://raw.githubusercontent.com/Azure/azure-docs-json-samples/master/azure-resource-manager/functions/min.json
要使用 PowerShell 部署此示例模板,请使用:
New-AzureRmResourceGroupDeployment -ResourceGroupName functionexamplegroup -TemplateUri https://raw.githubusercontent.com/Azure/azure-docs-json-samples/master/azure-resource-manager/functions/min.json
mod
mod(operand1, operand2)
返回使用提供的两个整数整除后的余数。
Parameters
参数 | 必选 | 类型 | 说明 |
---|---|---|---|
operand1 | 是 | int | 被除数。 |
operand2 | 是 | int | 除数,不能为 0。 |
返回值
一个表示余数的整数。
示例
以下示例模板将返回一个参数除以另一个参数后所得的余数。
{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"first": {
"type": "int",
"defaultValue": 7,
"metadata": {
"description": "Integer being divided"
}
},
"second": {
"type": "int",
"defaultValue": 3,
"metadata": {
"description": "Integer used to divide"
}
}
},
"resources": [
],
"outputs": {
"modResult": {
"type": "int",
"value": "[mod(parameters('first'), parameters('second'))]"
}
}
}
上述示例中使用默认值的输出为:
名称 | 类型 | 值 |
---|---|---|
modResult | int | 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/mod.json
要使用 PowerShell 部署此示例模板,请使用:
New-AzureRmResourceGroupDeployment -ResourceGroupName functionexamplegroup -TemplateUri https://raw.githubusercontent.com/Azure/azure-docs-json-samples/master/azure-resource-manager/functions/mod.json
mul
mul(operand1, operand2)
返回提供的两个整数的积。
Parameters
参数 | 必选 | 类型 | 说明 |
---|---|---|---|
operand1 | 是 | int | 被乘数。 |
operand2 | 是 | int | 乘数。 |
返回值
一个表示积的整数。
示例
以下示例模板将一个参数乘以另一个参数。
{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"first": {
"type": "int",
"defaultValue": 5,
"metadata": {
"description": "First integer to multiply"
}
},
"second": {
"type": "int",
"defaultValue": 3,
"metadata": {
"description": "Second integer to multiply"
}
}
},
"resources": [
],
"outputs": {
"mulResult": {
"type": "int",
"value": "[mul(parameters('first'), parameters('second'))]"
}
}
}
上述示例中使用默认值的输出为:
名称 | 类型 | 值 |
---|---|---|
mulResult | int | 15 |
要使用 Azure CLI 部署此示例模板,请使用:
az group deployment create -g functionexamplegroup --template-uri https://raw.githubusercontent.com/Azure/azure-docs-json-samples/master/azure-resource-manager/functions/mul.json
要使用 PowerShell 部署此示例模板,请使用:
New-AzureRmResourceGroupDeployment -ResourceGroupName functionexamplegroup -TemplateUri https://raw.githubusercontent.com/Azure/azure-docs-json-samples/master/azure-resource-manager/functions/mul.json
sub
sub(operand1, operand2)
返回提供的两个整数在相减后的结果。
Parameters
参数 | 必选 | 类型 | 说明 |
---|---|---|---|
operand1 | 是 | int | 被减数。 |
operand2 | 是 | int | 减数。 |
返回值
一个表示减后结果的整数。
示例
以下示例模板将一个参数与另一个参数相减。
{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"first": {
"type": "int",
"defaultValue": 7,
"metadata": {
"description": "Integer subtracted from"
}
},
"second": {
"type": "int",
"defaultValue": 3,
"metadata": {
"description": "Integer to subtract"
}
}
},
"resources": [
],
"outputs": {
"subResult": {
"type": "int",
"value": "[sub(parameters('first'), parameters('second'))]"
}
}
}
上述示例中使用默认值的输出为:
名称 | 类型 | 值 |
---|---|---|
subResult | int | 4 |
要使用 Azure CLI 部署此示例模板,请使用:
az group deployment create -g functionexamplegroup --template-uri https://raw.githubusercontent.com/Azure/azure-docs-json-samples/master/azure-resource-manager/functions/sub.json
要使用 PowerShell 部署此示例模板,请使用:
New-AzureRmResourceGroupDeployment -ResourceGroupName functionexamplegroup -TemplateUri https://raw.githubusercontent.com/Azure/azure-docs-json-samples/master/azure-resource-manager/functions/sub.json
后续步骤
- 有关 Azure 资源管理器模板中各部分的说明,请参阅创作 Azure 资源管理器模板。
- 若要合并多个模板,请参阅将链接的模板与 Azure Resource Manager 配合使用。
- 若要在创建资源类型时迭代指定的次数,请参阅在 Azure Resource Manager 中创建多个资源实例。
- 若要查看如何部署已创建的模板,请参阅使用 Azure Resource Manager 模板部署应用程序。