RubenSo
RubenSo

Reputation: 45

Vnet creation task skip the task if it is run once

I am working to build a ci/cd pipeline for AKS. the first task set is "Azure resource group deployment" which is used for creating vnet /subnet for the AKS .
The intention is to skip the task next time onwards since the vnet and subnet are already in place. Second time onwards getting the following error -

BadRequest: { "error": { "code": "InUseSubnetCannotBeDeleted", "message": "Subnet AKSSubnet is in use by /subscriptions/***************************************/resourceGroups/MC_**************-CLUSTER_eastus/providers/Microsoft.Network/networkInterfaces/aks-agentpool-
########-nic-0/ipConfigurations/ipconfig1 and cannot be deleted. In order to delete the subnet, delete all the resources within the subnet. See aka.ms/deletesubnet.", "details": [] } }  
Error:  Task failed while creating or updating the template deployment. 

Looks like the task is trying to delete the subnet instead of skipping it. What is the resolution?

It is using following arm templates : azuredeploy.json

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "vnetName": {
      "type": "string",
      "defaultValue": "GEN-VNET-NAME",
      "metadata": {
        "description": "Name of the virtual Network"
      }
    },
    "vnetAddressPrefix": {
      "type": "string",
      "defaultValue": "10.10.0.0/16",
      "metadata": {
        "description": "Address prefix"
      }
    },
    "subnetPrefix": {
      "type": "string",
      "defaultValue": "10.10.0.0/24",
      "metadata": {
        "description": "Subnet Prefix"
      }
    },
    "subnetName": {
      "type": "string",
      "defaultValue": "Subnet",
      "metadata": {
        "description": "GEN-SUBNET-NAME"
      }
    }
  },
  "variables": {},
  "resources": [
    {
      "apiVersion": "2018-06-01",
      "type": "Microsoft.Network/virtualNetworks",
      "name": "[parameters('vnetName')]",
      "location": "[resourceGroup().location]",
      "properties": {
        "addressSpace": {
          "addressPrefixes": [
            "[parameters('vnetAddressPrefix')]"
          ]
        }
      },
      "resources": [
        {
          "apiVersion": "2018-06-01",
          "type": "subnets",
          "location": "[resourceGroup().location]",
          "name": "[parameters('subnetName')]",
          "dependsOn": [
            "[parameters('vnetName')]"
          ],
          "properties": {
            "addressPrefix": "[parameters('subnetPrefix')]"
          }
        }
      ]
    }
  ],
  "outputs": {
    "vnetName": {
      "type": "string",
      "value": "[parameters('vnetName')]"
    },
    "subnetName": {
      "type": "string",
      "value": "[parameters('subnetName')]"
    }
  }
}

azuredeploy.parameters.json

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "vnetName": {
      "value": "###########"
    },
    "vnetAddressPrefix": {
      "value": "10.10.0.0/16"
    },
    "subnetPrefix": {
      "value": "10.10.0.0/24"
    },
    "subnetName": {
      "value": "######"
    }
  }
}

Upvotes: 0

Views: 480

Answers (1)

4c74356b41
4c74356b41

Reputation: 72161

What is happening right here - your template is coded in such a fashion:

vnet resources
  empty subnets property
subnet resource(s)
bla-bla-bla

and what is happening here it is trying to coerce the vnet to have 0 subnets, due to how you authored your template. you have 2 options:

  1. put a condition on the vnet resource definition and pass a parameter to it if the build number is greater than 1 (or just manually specify at build time whether to skip it or not).
  2. modify your template to look like so:
vnet resource
  subnets property populated with subnets
bla-bla-bla

essentially, this has nothing to do with Azure Devops.

Upvotes: 2

Related Questions