我正在处理一个ARM模板,需要执行条件部署。例如,我在一个变量‘subnet’中定义了两个网络安全组。
"variables": {
"subnets": [
{
"subnetname": "AzureBastionSubnet",
"nsgname": "nsg_bastion1"
},
{
"subnetname": "client-subnet",
"nsgname": "nsg_client1"
}
]
}网络安全组'nsg_bastion1‘需要使用预定义的规则进行特殊处理,因为它是Azure堡垒子网的网络安全组。'nsg_client1‘将分配一些自定义规则,这在这一点上无关紧要。
为了区分非堡垒和堡垒网络安全组,我创建了两个条件资源块:
"resources": [
// NSG (non-Bastion)
{
"condition": "[not(equals(variables('subnets')[copyIndex()].name, 'AzureBastionSubnet'))]",
"name": "[variables('subnets')[copyIndex()].nsg]",
"type": "Microsoft.Network/networkSecurityGroups",
"apiVersion": "2019-11-01",
"location": "[parameters('location')]",
"properties": {},
"copy": {
"name": "nsg-c",
"count": "[length(variables('subnets'))]"
}
},
// NSG (Bastion)
{
"condition": "[equals(variables('subnets')[copyIndex()].name, 'AzureBastionSubnet')]",
"name": "[variables('subnets')[copyIndex()].nsg]",
"type": "Microsoft.Network/networkSecurityGroups",
"apiVersion": "2019-11-01",
"location": "[parameters('location')]",
"properties": {},
"resources": [
// Bastion Security Rules .....
],
"copy": {
"name": "nsg-bastion-c",
"count": "[length(variables('subnets'))]"
}
},]condition属性检查子网是否称为'AzureBastionSubnet‘。我已经验证了这对两个资源块单独有效,但是当它们都包含在代码中时就不起作用了。它总是抛出以下错误消息:
Code=InvalidTemplate; Message=Deployment template validation failed:
'The resource 'Microsoft.Network/networkSecurityGroups/AzureBastionSubnet' at line '' and column '' is defined multiple times in a template.如果有任何帮助,我将不胜感激,提前谢谢!
发布于 2020-03-20 05:31:42
好吧,我以为我们已经解决了这个问题,但还没有……
即使您的条件是互斥的,您也不能在同一模板中有两个具有相同resourceId的资源。
我猜两者的区别在于securityRules?如果是这样,最简单的选择可能是定义两个单独的var,然后使用if()语句根据您的条件在它们之间进行交换。
如果不是这样,如果你想在模板上展开,可以看看是否有更好的选择……
https://stackoverflow.com/questions/60672618
复制相似问题