通过ARM模板向现有Vnet添加子网

时间:2018-03-15 03:17:33

标签: json azure azure-powershell arm-template vnet

以下是我的输入参数文件(parameter.json)

    {
    "VNetSettings":{
    "value":{
        "name":"VNet2",
        "addressPrefixes":"10.0.0.0/16",
        "subnets":[
            {
                "name": "sub1",
                "addressPrefix": "10.0.1.0/24"
            },
            {
                "name":"sub2",
                "addressPrefix":"10.0.2.0/24"
            }
        ]
    }
  }
}

以下是我应该部署子网的arm模板。(deploy.json)

   {
"contentversion":"1.0.0.0",
"$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"parameters":{
        "VNetSettings":{"type":"object"},
"noofsubnets":
        {
            "type":"int"
        },
"newOrExisting":
    {
        "type":"string",
        "allowedvalues":
            [
                "new",
                "existing"
            ]
    }
    },
"resources":
[{
  "condition":"[equals(parameters('newOrExisting'),'new')]",
  "type": "Microsoft.Network/virtualNetworks",
  "mode":"Incremental",
  "apiVersion": "2015-06-15",
  "name":"[parameters('VNetSettings').name]",
  "location":"[resourceGroup().location]",
  "properties":
  {
    "addressSpace":{
        "addressPrefixes":["[parameters('VNetSettings').addressPrefixes]"]
  },
    "copy":
       [{
         "name":"subnets",
         "count":"[parameters('noofsubnets')]",
         "input": 
             {
              "name": "[parameters('VNetSettings').subnets[copyIndex('subnets')].name]",
              "properties":
                 {
                   "addressPrefix": "[parameters('VNetSettings').subnets[copyIndex('subnets')].addressPrefix]"
                 }
             }
        }]

       }
  }]
}

模板应该做的是将这两个子网(sub1& sub2)添加到Vnet以及现有子网(如果已经存在的话)。但它正在做的是用这两个子网替换现有的子网在输入文件中。模式:增量应该这样做,但我不确定我是否将它放在正确的位置。我使用以下powershell命令部署此模板:

    New-AzureRmResourceGroupDeployment -Name testing -ResourceGroupName rgname -TemplateFile C:\Test\deploy.json -TemplateParameterFile C:\Test\parameterfile.json

1 个答案:

答案 0 :(得分:2)

这是预期的行为。你应该阅读'Idempotence'。你需要做的是创建一个子网资源,这样你就可以解决它。

{
    "apiVersion": "2016-03-30",
    "name": "vnetName\subnetName",
    "location": "[resourceGroup().location]]",
    "type": "Microsoft.Network/virtualNetworks/subnets",
    "properties": {
        "addressPrefix": "xx.x.x.xx"
    }
}

vnetName必须是您要在其中创建资源的vnet。