Azure ARM-内联条件语句

时间:2018-12-10 20:24:44

标签: json azure templates

我想知道是否可以在我的Azure ARM模板中执行内联条件语句。简而言之,如果给定参数设置为“是”,我想为NIC分配一个公共IP地址

        {
          "apiVersion": "2015-06-15",
          "type": "Microsoft.Network/networkInterfaces",
          "name": "my-network-interface",
          "location": "[resourceGroup().location]",
          "properties": {
            "ipConfigurations": [
              {
                "name": "ipconfig1",
                "properties": {
                  "privateIPAllocationMethod": "Static",
                  "subnet": {
                    "id": "my-subnet-name"
                  },
                  "privateIpAddress": "10.0.0.5",
                  IF ASSIGN PUBLIC IP THEN,
                       "publicIpAddress": "id": "[resourceId('Microsoft.Network/publicIPAddresses', 'my-public-ip)]"
                }
              }
            ]
          }
        },

1 个答案:

答案 0 :(得分:0)

您必须使用union()if()函数来实现。创建这些变量:

"external": {
    "publicIPAddress": {
        "id": "my_ip_id"
    }
},
"internal": {
    "privateIPAddress": "10.0.0.5",
    "privateIPAllocationMethod": "Static",
    "subnet": {
        "id": "my_subnet_id"
    }
},

然后执行以下操作:

"ipConfigurations": [
    {
        "name": "ipconfig1",
        "properties": "[if(equals(parameters('something'), 'yes'), union(variables('external'), variables('external')), variables('internal'))]"
    }
]
替代方法(如果您不关心代码重复),则可以使用略有不同的代码创建2个不同的部署,一个将部署条件1,另一个将部署条件2。此外,这种方法更易于理解,但较难维护。

相关问题