Azure Classroom Series – ARM Templates 04/Oct/2019

Variables

  • Variables are value which can be manipulated with in the templated
  • Variable syntax can be found here

Using Copy Effectively

  • Starting from where we left in the last article. We have removed the count parameter & added a variable for network name.
  • Used a function to populate the resource location
  • With these changes, the script looks something like this
{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion" : "1.0.0.0",
    "parameters": {
        "vnetaddresspace": {
            "type": "array",
            "defaultValue":["192.168.0.0/16"] 

        },
        "subnetnames":{
            "type": "array",
            "defaultValue": ["web", "business","data"]
        },
        "subnetAddress":{
            "type": "array",
            "defaultValue": ["192.168.0.0/24","192.168.1.0/24", "192.168.2.0/24"]
        }
    },
    "variables":{
        "vnetname": "ntier"
    },
    "resources":[
        {
            "name": "[variables('vnetname')]",
            "type": "Microsoft.Network/virtualNetworks",
            "apiVersion": "2019-04-01",
            "location": "[resourceGroup().location]",
            "properties": {
                "addressSpace": {
                    "addressPrefixes": "[parameters('vnetaddresspace')]"
                }

            } 
        },
        {
            "name": "[concat(variables('vnetname'),'/',parameters('subnetnames')[copyIndex()])]",
            "type": "Microsoft.Network/virtualNetworks/subnets",
            "apiVersion": "2019-04-01",
            "properties": {
                "addressPrefix": "[parameters('subnetAddress')[copyIndex()]]"
            },
            "dependsOn": ["[variables('vnetname')]"],
            "copy": { 
                "name": "websitescopy", 
                "count": "[length(parameters('subnetnames'))]",
                "mode": "serial"
            }
                        
        }

    ]

}
  • Lets add an simple output to the template. In this case i will be adding the count of subnets as output. Refer here for more info.
{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion" : "1.0.0.0",
    "parameters": {
        "vnetaddresspace": {
            "type": "array",
            "defaultValue":["192.168.0.0/16"] 

        },
        "subnetnames":{
            "type": "array",
            "defaultValue": ["web", "business","data"]
        },
        "subnetAddress":{
            "type": "array",
            "defaultValue": ["192.168.0.0/24","192.168.1.0/24", "192.168.2.0/24"]
        }
    },
    "variables":{
        "vnetname": "ntier"
    },
    "resources":[
        {
            "name": "[variables('vnetname')]",
            "type": "Microsoft.Network/virtualNetworks",
            "apiVersion": "2019-04-01",
            "location": "[resourceGroup().location]",
            "properties": {
                "addressSpace": {
                    "addressPrefixes": "[parameters('vnetaddresspace')]"
                }

            } 
        },
        {
            "name": "[concat(variables('vnetname'),'/',parameters('subnetnames')[copyIndex()])]",
            "type": "Microsoft.Network/virtualNetworks/subnets",
            "apiVersion": "2019-04-01",
            "properties": {
                "addressPrefix": "[parameters('subnetAddress')[copyIndex()]]"
            },
            "dependsOn": ["[variables('vnetname')]"],
            "copy": { 
                "name": "websitescopy", 
                "count": "[length(parameters('subnetnames'))]",
                "mode": "serial"
            }
                        
        }

    ],
    "outputs": {
        "subnets-count": {
            "type": "int",
            "value": "[length(parameters('subnetnames'))]"
        }
    }

}
  • Try to create deployment and look at the outputs section

  • Microsoft has developed lot of sample templates for various deployments. Refer Here

  • If you would like to create user defined functions with in arm template, Refer Here

Whats left

  • How can i call existing template from my arm template
  • How to create parameters file
  • How do i execute arm templates from CLI and Powershell

By continuous learner

devops & cloud enthusiastic learner

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Please turn AdBlock off
Social Media Icons Powered by Acurax Web Design Company

Discover more from Direct DevOps from Quality Thought

Subscribe now to keep reading and get access to the full archive.

Continue reading

Visit Us On FacebookVisit Us On LinkedinVisit Us On Youtube