防护模板复制功能-跳过循环中的属性(如果不存在)-网络安全组-sourceAddressPrefix/sourceAdd



我正在尝试在用于部署网络安全组的arm模板中实现复制功能。我以前使用过这种格式部署模板,但由于Microsoft决定使用两个不同的名称,这取决于属性是单个项还是列表,我无法使用复制功能。我不得不考虑使用If语句来忽略循环中存在的null参数,这是我无法实现的。所以我的问题是,如果一个特定的属性不存在于循环中,如何遍历循环并忽略它。

有问题的两个属性是sourceAddressPrefix或sourceAddressPrefixes。这导致第二次迭代出现问题,我将收到一条错误消息这个语言表达式属性"sourceAddressPrefixes"不存在(如果我切换参数文件的顺序,即sourceAddressPrefix是第一个,则错误消息将指向"sourceAddressPrefix">

参数文件,正如您所看到的,有两个安全规则,一个设置为sourceAddressPrefix,另一个设置成sourceAddressPrefixes

{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"location": {
"value": "westeurope"
},
"SecurityRule":{
"value": [
{
"name": "AllowSyncWithAzureAD",
"protocol": "Tcp",
"sourcePortRange": "*",
"destinationPortRange": "443",
"sourceAddressPrefix": "*",
"destinationAddressPrefix": "*",
"access": "Allow",
"priority": 101,
"direction": "Inbound"
},
{
"name": "AllowPSRemotingSliceP",
"protocol": "Tcp",
"sourcePortRange": "*",
"destinationPortRange": "5986",
"sourceAddressPrefixes": "[variables('PSRemotingSlicePIPAddresses')]",
"destinationAddressPrefix": "*",
"access": "Allow",
"priority": 301,
"direction": "Inbound"
}                                                                                 
]
}          
}
}

在Template文件中,我用if语句添加了这两个属性,但很明显,我没有正确地编写它们,因为预期的结果是,如果在循环中不存在该属性,则忽略该属性。

{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"location": {
"type": "string",
"defaultValue": "[resourceGroup().location]",
"metadata": {
"description": "Location for all resources."
}
},
"SecurityRule": {
"type": "array"
}
},
"variables": {
"domainServicesNSGName": "AGR01MP-NSGAADDS01",
"PSRemotingSlicePIPAddresses": [
"52.182.100.238",
"52.180.177.87"
],
"RDPIPAddresses": [
"210.66.188.40/27",
"15.156.75.52/27",
"134.104.124.36/27",
"144.122.4.96/27"
],
"PSRemotingSliceTIPAddresses": [
"56.180.182.67",
"56.180.121.39",
"56.175.228.121"
]     
},
"resources": [
{
"apiVersion": "2018-10-01",
"type": "Microsoft.Network/networkSecurityGroups",
"name": "[variables('domainServicesNSGName')]",
"location": "[parameters('location')]",
"properties": {
"copy": [
{
"name":"securityRules",
"count": "[length(parameters('securityRule'))]",
"mode": "serial",
"input": {
"name": "[concat(parameters('securityRule')[copyIndex('securityRules')].name)]", 
"properties": {
"protocol": "[concat(parameters('securityRule')[copyIndex('securityRules')].protocol)]", 
"sourcePortRange": "[concat(parameters('securityRule')[copyIndex('securityRules')].sourcePortRange)]", 
"destinationPortRange": "[concat(parameters('securityRule')[copyIndex('securityRules')].destinationPortRange)]", 
"sourceAddressPrefixes": "[if(equals(parameters('securityRule')[copyIndex('securityRules')].sourceAddressPrefixes,''), json('null'), parameters('securityRule')[copyIndex('securityRules')].sourceAddressPrefixes)]",
"sourceAddressPrefix": "[if(equals(parameters('securityRule')[copyIndex('securityRules')].sourceAddressPrefix,''), json('null'), parameters('securityRule')[copyIndex('securityRules')].sourceAddressPrefix)]",
"destinationAddressPrefix": "[concat(parameters('securityRule')[copyIndex('securityRules')].destinationAddressPrefix)]", 
"access": "[concat(parameters('securityRule')[copyIndex('securityRules')].access)]", 
"priority": "[concat(parameters('securityRule')[copyIndex('securityRules')].priority)]", 
"direction": "[concat(parameters('securityRule')[copyIndex('securityRules')].direction)]" 
}  
}
}                                                           
]
}
}
],
"outputs": {}
}

找到解决方案

"sourceAddressPrefix": "[if(equals(parameters('SecurityRule')[copyIndex('securityRules')].name, 'SyncWithAzureAD'), parameters('SecurityRule')[copyIndex('securityRules')].sourceAddressPrefix, json('null'))]" , 
"sourceAddressPrefixes": "[if(contains(parameters('SecurityRule')[copyIndex('securityRules')].name, 'Allow'), parameters('SecurityRule')[copyIndex('securityRules')].sourceAddressPrefixes, json('null'))]" ,     

上面的代码允许我部署以更改忽略数组中的null值。尽管我不得不将AllowSyncWithAzureAD更改为SyncWithAzure AD,以便它不会被第二行拾取

相关内容

最新更新