Azure Ressource Manager : un template JSON

Un petit template JSON que j’apprécie particulièrement, il permet de construire très rapidement des labs complets en mode IaaS via Azure ARM v2.
Toutes les VM sont créés de façon identique, dans une bulle complétement autonome au niveau stockage et réseau. Les OS sont hardcodés sur du 2012 R2, mais cela n’est pas compliqué à changer.

Pour l’utiliser, une fois basculé en mode ARM :

New-AzureResourceGroup -Name testARMBJDC -Location "north europe" -TemplateFile .\ARM.json -Verbose 

En argument :

      locationFromTemplate : l’emplacement où déployer les VM (qui peut être différent de l’emplacement où est “créer” le resourcegroup)

 

      nameFromTemplate : le nom utilisé pour construire les différentes ressources (stockage, réseau, etc.)

 

      vmSize : la taille des VM

 

      vnet_AddressSpace : le réseau

 

      publicip_DNS : le nom public des VM, attention car si ce nom existe deja, le déploiment sera en échec!

 

      vm_name : le nom des serveurs

 

      vm_admin : le nom de l’administrateur

 

      vm_pwd : le mot de passe par défaut

 

    vm_count : le nombre de VM a créer

Le code du JSON :

{
  "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json",
  "contentVersion": "1.0.0.0",
 
  "parameters": {
	"location" : {
		"type": "String",
		"defaultValue": "West Europe",
		"allowedValues": [
			"Central US",
			"South Central US",
			"East US",
			"West US",
			"North Central US",
			"East US 2",
			"North Europe",
			"West Europe",
			"Southeast Asia",
			"East Asia",
			"Japan West",
			"Japan East",
			"Brazil South"			
		],
		"metadata": {
			"Description": "Datacenter Location"
		}
	},
	"name": {
		"type": "String",
		"defaultValue": "testbrucejdc",
		"metadata": {
			"Description": "Root named used for resources"
		}
	},
	"vmSize" : {
		"type": "String",
		"defaultValue": "Basic_A1",
		"metadata": {
			"Description": "VM Size"
		}
	},
	"vnet_AddressSpace": {
		"type": "String",
		"defaultValue": "172.16.0.0/24",
		"metadata": {
			"Description": "Virtual Network subnet"
		}
	},
	"publicip_DNS": {
		"type": "String",
		"defaultValue": "testbrucejdc",
		"metadata": {
			"Description": "VM public IP DNS"
		}
	},
	"vm_name": {
		"type": "String",
		"defaultValue": "SRV",
		"metadata": {
			"Description": "VM Name"
		}
	},
	"vm_admin": {
		"type": "String",
		"defaultValue": "bjdc",
		"metadata": {
			"Description": "VM admin username"
		}
	},
	"vm_pwd": {
		"type": "SecureString",
		"defaultValue": "Premier*01",
		"metadata": {
			"Description": "VM admin password"
		}
	},
	
	"vm_count": {
		"type": "int",
		"defaultValue": 1,
		"metadata": {
			"Description": "Number of VM"
		}
	}
  },
    
  "variables": {	
	"author" : "brucejdc@brucejdc.fr",
	"storage_name" : "[toLower(concat(parameters('name'),'storage'))]",
	"storage_type": "Standard_LRS",
	"storage_uri" : "[concat('https://',variables('storage_name'),'.blob.core.windows.net')]",
	
	"vnet_name": "[concat(parameters('name'),'vnet')]",
	"vnet_ID": "[resourceId('Microsoft.Network/virtualNetworks',variables('vnet_name'))]",
	"vnet_subnet_name": "Subnet1",
	"vnet_subnet_ref": "[concat(variables('vnet_ID'),'/subnets/',variables('vnet_subnet_name'))]",
	
	"publicIP_Name": "[concat(parameters('name'),'ip')]",
	"publicIP_Type": "Dynamic",
	
	"eth_name": "eth"
	},
  
  "resources": [
 	{
		"type": "Microsoft.Storage/storageAccounts",
		"name": "[variables('storage_name')]",
		"apiVersion": "2015-05-01-preview",
		"location": "[parameters('location')]",
		"properties": {
			"accountType": "[variables('storage_type')]"
		}
	},
	
	{
		"APIversion" : "2015-05-01-preview",
		"type": "Microsoft.Network/virtualNetworks",
		"name": "[variables('vnet_name')]",		
		"location": "[parameters('location')]",
		"properties": {
			"addressspace": {
				"addressPrefixes": [
					"[parameters('vnet_AddressSpace')]"
				]
			},
			"subnets": [
				{
					"name": "[variables('vnet_subnet_name')]",
					"properties": {
						"addressPrefix": "[parameters('vnet_AddressSpace')]"
					}
				}
			]
		}		
	},
	
	{
		"apiVersion": "2014-12-01-preview",
		"type": "Microsoft.Network/publicIPAddresses",
		"name": "[concat(variables('publicIP_Name'),copyIndex())]",
		"location": "[parameters('location')]",
		"dependsOn" : [
			"[resourceId('Microsoft.Network/virtualNetworks',variables('vnet_name'))]"
		],
		"properties": {
			"publicIPAllocationMethod": "[variables('publicIP_Type')]",
			"dnsSettings": {
				"domainNameLabel": "[concat(parameters('publicip_DNS'),copyIndex())]"
			}
		},
		
		"copy": {
			"name": "[variables('publicIP_Name')]",
			"count": "[parameters('vm_count')]"
		}
	},
	
   {
        "apiVersion": "2015-05-01-preview",
        "type": "Microsoft.Network/networkInterfaces",
        "name": "[concat(variables('eth_name'), copyIndex())]",
        "location": "[parameters('location')]",
		"dependsOn" : [
			"[resourceId('Microsoft.Network/virtualNetworks',variables('vnet_name'))]",
			"[resourceId('Microsoft.Network/publicIPAddresses',concat(variables('publicIP_Name'),copyIndex()))]"
		],
        "properties": {
            "ipConfigurations": [
                {
                    "name": "ipconfig1",
                    "properties": {
                        "privateIPAllocationMethod": "Dynamic",
						"subnet": {
                            "id": "[variables('vnet_subnet_ref')]"
                        },
						"PublicIpAddress": {
							"Id": "[resourceId('Microsoft.Network/publicIPAddresses',concat(variables('publicIP_Name'),copyIndex()))]"
						}
                    }
                }
            ]
        },
		"copy": {
			"name": "[variables('eth_name')]",
			"count": "[parameters('vm_count')]"
		}
    },
	
	{
        "apiVersion": "2015-05-01-preview",
        "type": "Microsoft.Compute/virtualMachines",
        "name": "[concat(parameters('vm_name'), copyIndex())]",
        "location": "[parameters('location')]",
		"dependsOn" : [
			"[resourceId('Microsoft.Network/publicIPAddresses',concat(variables('publicIP_Name'),copyIndex()))]",
			"[resourceId('Microsoft.Storage/storageAccounts',variables('storage_name'))]",
			"[resourceId('Microsoft.Network/networkInterfaces',concat(variables('eth_name'), copyIndex()))]"
		],
        "properties": {
            "hardwareProfile": {
                "vmSize": "[parameters('vmSize')]"
            },
            "osProfile": {
                "computername": "[concat(parameters('vm_name'), copyIndex())]",
                "adminUsername": "[parameters('vm_admin')]",
                "adminPassword": "[parameters('vm_pwd')]"
            },
            
            "storageProfile": {
                "imageReference": {
                    "publisher": "MicrosoftWindowsServer",
                    "offer": "WindowsServer",
                    "sku": "2012-R2-Datacenter",
                    "version": "latest"
                },
				"osDisk" : {
					"name": "[parameters('vm_name')]",
					"createOption": "FromImage",
					"caching": "ReadWrite",
					"vhd": {
						"uri": "[concat(variables('storage_uri'),'/vhds/',parameters('vm_name'),copyIndex(),'.vhd')]"		
					
					}
				}
            },
			"networkProfile" : {
				"networkInterfaces": [
					{
						"id" : "[resourceId('Microsoft.Network/networkInterfaces',concat(variables('eth_name'),copyIndex()))]"
					}
				]
			}
        },
		"copy": {
			"name": "[parameters('vm_name')]",
			"count": "[parameters('vm_count')]"			
		}
    }
  ]
}