Azure 资源管理器模板 : Conditional Deployment

标签 azure azure-resource-manager

我正在开发 ARM 模板,需要执行条件部署。例如,我在变量“子网”中定义了两个网络安全组。

"variables": {
    "subnets": [
        {
            "subnetname": "AzureBastionSubnet",
            "nsgname": "nsg_bastion1"
        },
        {
            "subnetname": "client-subnet",
            "nsgname": "nsg_client1"
        }
    ]
}

网络安全组“nsg_bastion1”需要使用预定义规则进行特殊处理,因为它是 Azure Bastion 子网的网络安全组。 “nsg_client1”将分配一些自定义规则,此时这些规则并不重要。

为了区分非 Bastion 和 Bastion 网络安全组,我创建了两个条件资源 block :

"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”。我已经验证这仅适用于两个资源 block ,但当它们都包含在代码中时则不起作用。它总是抛出以下错误消息:

    Code=InvalidTemplate; Message=Deployment template validation failed:
'The resource 'Microsoft.Network/networkSecurityGroups/AzureBastionSubnet' at line '' and column '' is defined multiple times in a template.

非常感谢您的帮助,提前致谢!

最佳答案

好吧,我以为我们已经解决了这个问题,但还没有......

即使您的条件是互斥的,同一模板中也不能有 2 个具有相同资源 ID 的资源。

我猜两者之间的区别在于 securityRules?如果是这样,最简单的选择可能是定义 2 个单独的变量,然后使用 if() 语句根据您的条件在它们之间进行交换。

如果你想在模板上进行扩展的话可以看看是否有更好的选择...

关于Azure 资源管理器模板 : Conditional Deployment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60672618/

相关文章:

.net - Azure AD 登录错误

azure - 为什么 Azure 资源组与特定区域关联?

azure 嵌套模板依赖项处理错误

azure - 从二头肌循环中调用的模块获取输出

azure - 设置adminPassword无效; linux部署在Azure资源管理器中

azure - RestSharp PATCH API 调用更新 Azure Devops 工作项抛出 BAD 请求 - "You must pass a valid patch document in the body of the request."

python - 如何通过 python 以编程方式提取 Azure IP 范围 json 文件?

c# - 使用 Mobile Service.LoginAsync() 处理后退按钮

xml - Microsoft Azure TTS 认知服务语音限制问题

azure - 取消 azure 资源管理器部署