动态创建ARM参数名称

时间:2018-08-30 19:17:18

标签: azure arm-template azure-template

我有一个需要动态生成参数名称的场景。像certificate1,certificate2,certificate3 ..等。当前,所有这些参数都应在主模板中定义。我们可以使用copy来在Main / Parent模板中动态地迭代和定义参数名称吗?还是ARM模板中有一种方法可以完成此任务?

2 个答案:

答案 0 :(得分:0)

您可以使用Azure模板中的复制功能来生成资源的名称,就像certificate1,certificate2,certificate3 ..等等。

以下示例:

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "resources": [
        {
            "apiVersion": "2016-01-01",
            "type": "Microsoft.Storage/storageAccounts",
            "name": "[concat('storage',copyIndex())]",
            "location": "[resourceGroup().location]",
            "sku": {
                "name": "Standard_LRS"
            },
            "kind": "Storage",
            "properties": {},
            "copy": {
                "name": "storagecopy",
                "count": 3
            }
        }
    ],
    "outputs": {}
}

存储名称如下:

存储0 储存1 storage2

有关更多详细信息,请参见Deploy multiple instances of a resource or property in Azure Resource Manager Templates

答案 1 :(得分:0)

您可以在变量部分或资源定义\资源属性中使用copy构造。然后可以将concat()copyIndex()函数一起使用来创建名称。

示例:

[concat('something-', copyIndex())]

这将为您提供诸如something-0,something-1,something-2等的名称(copyIndex从0开始)。您还可以选择给偏移量copyIndex来偏移它:

[concat('something-', copyIndex(10))]

这将为您提供名称,如something-10,something-11,something-12等。

复制变量\属性:

"copy": [
    {
        "name": "nameOfThePropertyOrVariableYouWantToIterateOver",
        "count": 3,
        "input": {
            "name": "[concat('something-', copyIndex('nameOfThePropertyOrVariableYouWantToIterateOver', 1))]"    
        }
    }
]

在这里您需要使用copyIndex函数指定要引用的循环,也可以使用offset

相关问题