【发布时间】:2020-06-25 14:19:20
【问题描述】:
我正在使用 ARM 模板,需要执行条件部署。例如,我在变量“子网”中定义了两个网络安全组。
"variables": {
"subnets": [
{
"subnetname": "AzureBastionSubnet",
"nsgname": "nsg_bastion1"
},
{
"subnetname": "client-subnet",
"nsgname": "nsg_client1"
}
]
}
网络安全组“nsg_bastion1”需要使用预定义规则进行特殊处理,因为它是 Azure Bastion 子网的网络安全组。 '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'))]"
}
},]
条件属性检查子网是否称为“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.
如果有任何帮助,我将不胜感激,在此先感谢!
【问题讨论】:
-
我建议将 AzureBastion NSG 设置为不同的参数,而不是将其包含在普通 NSG 的相同数组中。我想知道 copyIndex() 在条件中是如何工作的。
-
如何部署模板?更具体地说,您知道部署中的 PUT 中的 apiVersion 是什么吗? (这是要修复的)
-
nm - 请参阅下面的答案...
标签: azure azure-resource-manager