如果在ARM模板中复制函数无法从参数中选取值,可以尝试使用表达式将参数的值传递给复制函数。以下是一个示例代码:
"parameters": {
"sourceResourceId": {
"type": "string",
"defaultValue": "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Compute/virtualMachines/{vmName}",
"metadata": {
"description": "The resource ID of the source virtual machine."
}
},
"numberOfCopies": {
"type": "int",
"defaultValue": 2,
"metadata": {
"description": "The number of copies to create."
}
}
},
"variables": {
"copyFunctions": [
{
"name": "copy1",
"count": "[parameters('numberOfCopies')]",
"input": {
"id": "[concat(parameters('sourceResourceId'), '/copy', copyIndex('copy1'))]"
}
}
]
},
"resources": [
{
"type": "Microsoft.Compute/virtualMachines",
"name": "[variables('copyFunctions')[copyIndex('copy1')].input.id]",
"apiVersion": "2021-07-01",
"location": "[resourceGroup().location]",
"copy": {
"name": "virtualMachinesCopy",
"count": "[parameters('numberOfCopies')]"
},
"properties": {
// Virtual machine properties...
}
}
]
在上述示例中,我们使用了一个名为copyFunctions
的变量来定义复制函数。这个复制函数接受numberOfCopies
参数的值,并使用concat
函数将其与sourceResourceId
参数的值进行组合。然后,在资源定义中,我们使用copyIndex
函数将复制函数的结果作为资源的名称。
通过这种方式,我们可以将参数的值传递给复制函数,并将其用于资源的创建。