Create multiple Azure VMs using the copy object - azure

I've got an ARM Template and Parameter file which successfully deploys a domain-joined VM in Azure.
Virtual Machine
NIC
OS Disk
It needs updated to deploy 500 VMs incrementing name suffix -01, -02, -03 etc. I'm trying to use copy object in the resources section of my template but running into issues so I wish to review how I'm approaching this.
https://learn.microsoft.com/en-us/azure/azure-resource-manager/templates/create-multiple-instances
Snippet from original ARM Template
"resources": [
{
"type": "Microsoft.Network/networkInterfaces",
"name": "[variables('nicName')]",
"apiVersion": "[variables('apiVersion')]",
"tags": "[parameters('tag')]",
"location": "[parameters('location')]",
"properties": {
"ipConfigurations": [
{
"name": "ipconfig",
"properties": {
"privateIPAllocationMethod": "Dynamic",
"subnet": {
"id": "[parameters('subnetID')]"
}
}
}
]
}
},
{
"type": "Microsoft.Compute/virtualMachines",
"name": "[variables('vmSettings').vmNamePrefix]",
"apiVersion": "2017-03-30",
"tags": "[parameters('tag')]",
"location": "[parameters('location')]",
"properties": {
"hardwareProfile": {
"vmSize": "[parameters('vmSize')]"
},
"osProfile": {
"computerName": "[variables('vmSettings').vmNamePrefix]",
"adminUsername": "[variables('vmSettings').adminUserName]",
"adminPassword": "[variables('vmSettings').adminPassword]"
},
"storageProfile": {
"imageReference": "[variables('imageReference')]",
"osDisk": {
"name": "[concat(parameters('dnsLabelPrefix'), '-os')]",
"caching": "ReadWrite",
"createOption": "FromImage"
},
"dataDisks": [ ]
},
"networkProfile": {
"networkInterfaces": [
{
"id": "[resourceId('Microsoft.Network/networkInterfaces', variables('nicName'))]"
}
]
}
},
"dependsOn": [
"[resourceId('Microsoft.Network/networkInterfaces', variables('nicName'))]"
]
},
Do I simply use copy on VM or have to add this as well on NIC and OS Disk?
One of the syntax I've tried. Sample syntax I could retry would be useful.
"name": "[concat(variables('vmSettings').vmNamePrefix), copyIndex()]"
EDIT:
Updated the ARM Template added missing right parentheses ")" and hardcoding "count" value 3 for now to simplify testing. Latest version is
"resources": [
{
"type": "Microsoft.Network/networkInterfaces",
"name": "[concat(variables('nicName'), '-', copyIndex())]",
"apiVersion": "[variables('apiVersion')]",
"tags": "[parameters('tag')]",
"location": "[parameters('location')]",
"copy": {
"name": "nicLoop",
"count": 3
},
"properties": {
"ipConfigurations": [
{
"name": "ipconfig",
"properties": {
"privateIPAllocationMethod": "Dynamic",
"subnet": {
"id": "[parameters('subnetID')]"
}
}
}
]
}
},
{
"type": "Microsoft.Compute/virtualMachines",
"name": "[concat(variables('vmSettings').vmNamePrefix, '-', copyIndex())]",
"apiVersion": "2017-03-30",
"tags": "[parameters('tag')]",
"location": "[parameters('location')]",
"copy": {
"name": "vmLoop",
"count": 3
},
"properties": {
"hardwareProfile": {
"vmSize": "[parameters('vmSize')]"
},
"osProfile": {
"computerName": "[variables('vmSettings').vmNamePrefix]",
"adminUsername": "[variables('vmSettings').adminUserName]",
"adminPassword": "[variables('vmSettings').adminPassword]"
},
"storageProfile": {
"imageReference": "[variables('imageReference')]",
"osDisk": {
"name": "[concat(parameters('dnsLabelPrefix'), '-os')]",
"caching": "ReadWrite",
"createOption": "FromImage"
},
"dataDisks": [ ]
},
"networkProfile": {
"networkInterfaces": [
{
"id": "[resourceId('Microsoft.Network/networkInterfaces', concat(variables('nicName'), '-', copyIndex()))]"
}
]
}
},
"dependsOn": [
"[resourceId('Microsoft.Network/networkInterfaces', concat(variables('nicName'), '-', copyIndex()))]"
]
},
Latest error:
New-AzResourceGroupDeployment : 9:41:51 PM - Error: Code=InvalidTemplate; Message=Deployment template validation failed: 'The resource 'Microsoft.Compute/virtualMachines/vmname' is not defined in the template. Please see https://aka.ms/arm-template for usage details.'.
Parameter file has this variable
"dnsLabelPrefix": { "value": "vmname" },

A1. You need to add the copy both in VM and the NIC, not the OS Disk.
A2. I suggest you just use the VM name suffix with the copy index, not like 01, 02 and etc. You can see the function copyIndex(). Then you can change the template for the Nic and VM that you provided like this:
"resources": [
{
"type": "Microsoft.Network/networkInterfaces",
"name": "[concat(variables('nicName'), '-', copyIndex())]",
"apiVersion": "[variables('apiVersion')]",
"tags": "[parameters('tag')]",
"location": "[parameters('location')]",
"copy": {
"name": "nicLoop",
"count": "[parameters('numberOfInstances')]"
},
"properties": {
"ipConfigurations": [
{
"name": "ipconfig",
"properties": {
"privateIPAllocationMethod": "Dynamic",
"subnet": {
"id": "[parameters('subnetID')]"
}
}
}
]
}
},
{
"type": "Microsoft.Compute/virtualMachines",
"name": "[concat(variables('vmSettings').vmNamePrefix, '-', copyIndex()]",
"apiVersion": "2017-03-30",
"tags": "[parameters('tag')]",
"location": "[parameters('location')]",
"copy": {
"name": "vmLoop",
"count": "[parameters('numberOfInstances')]"
},
"properties": {
"hardwareProfile": {
"vmSize": "[parameters('vmSize')]"
},
"osProfile": {
"computerName": "[variables('vmSettings').vmNamePrefix]",
"adminUsername": "[variables('vmSettings').adminUserName]",
"adminPassword": "[variables('vmSettings').adminPassword]"
},
"storageProfile": {
"imageReference": "[variables('imageReference')]",
"osDisk": {
"name": "[concat(parameters('dnsLabelPrefix'), '-os')]",
"caching": "ReadWrite",
"createOption": "FromImage"
},
"dataDisks": [ ]
},
"networkProfile": {
"networkInterfaces": [
{
"id": "[resourceId('Microsoft.Network/networkInterfaces', concat(variables('nicName'), '-', copyIndex())]"
}
]
}
},
"dependsOn": [
"[resourceId('Microsoft.Network/networkInterfaces', variables('nicName'), '-', copyIndex())]"
]
},

Related

ARM : Get network Interface Private IP and Default Gateway

im deploying an ARM template for a virtual machine with network interfaces
i have a .sh script that needs parameters to be passed to it
parameters :
NIC private ip
NIC default Gateway
nic template :
{
"type": "Microsoft.Network/networkInterfaces",
"apiVersion": "2019-06-01",
"name": "[parameters('nic2name')]",
"location": "[parameters('location')]",
"dependsOn": [
"[parameters('virtualNetworkName')]"
],
"properties": {
"ipConfigurations": [
{
"name": "ipconfig1",
"properties": {
"privateIPAllocationMethod": "Dynamic",
"subnet": {
"id": "[variables('subnetRef')]"
},
"primary": true
}
}
],
"primary": false
}
}
im sitting variables to get this parameters but idk how to get them
"smnet_dev": "[resourceId('Microsoft.Network/networkInterfaces', parameters('nic2name')).ipConfigurations]",
"smnet_dflt_gw": "[resourceId('Microsoft.Network/networkInterfaces', parameters('nic2name')).<default gateway>]"
i hope someone can guide me to the right direction
AFAIK, We can not give it directly instead of that we can use PowerShell script or bicep which will retrieve the value of nic and put it in variable.
In ARM while we are creating resources (e.g virtual machine with nic) we can add the variable as following and then we can deploy the NIC with gateways.
Things we have tried to create a virtual Machine with nic and provide the variable as shown below it got succeed .
You can use the below template by passing your required name in parameter file for the Automation.
Template.json:-
{
"$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"location": {
"type": "string"
},
"networkInterfaceName": {
"type": "string"
},
"enableAcceleratedNetworking": {
"type": "bool"
},
"networkSecurityGroupName": {
"type": "string"
},
"networkSecurityGroupRules": {
"type": "array"
},
"subnetName": {
"type": "string"
},
"virtualNetworkName": {
"type": "string"
},
"addressPrefixes": {
"type": "array"
},
"subnets": {
"type": "array"
},
"publicIpAddressName": {
"type": "string"
},
"publicIpAddressType": {
"type": "string"
},
"publicIpAddressSku": {
"type": "string"
},
"pipDeleteOption": {
"type": "string"
},
"virtualMachineName": {
"type": "string"
},
"virtualMachineComputerName": {
"type": "string"
},
"virtualMachineRG": {
"type": "string"
},
"osDiskType": {
"type": "string"
},
"osDiskDeleteOption": {
"type": "string"
},
"virtualMachineSize": {
"type": "string"
},
"nicDeleteOption": {
"type": "string"
},
"adminUsername": {
"type": "string"
},
"adminPassword": {
"type": "secureString"
},
"patchMode": {
"type": "string"
},
"enableHotpatching": {
"type": "bool"
},
"tags": {
"type": "object"
}
},
"variables": {
"nsgId": "[resourceId(resourceGroup().name, 'Microsoft.Network/networkSecurityGroups', parameters('networkSecurityGroupName'))]",
"vnetName": "[parameters('virtualNetworkName')]",
"vnetId": "[resourceId(resourceGroup().name,'Microsoft.Network/virtualNetworks', parameters('virtualNetworkName'))]",
"subnetRef": "[concat(variables('vnetId'), '/subnets/', parameters('subnetName'))]"
},
"resources": [
{
"name": "[parameters('networkInterfaceName')]",
"type": "Microsoft.Network/networkInterfaces",
"apiVersion": "2021-03-01",
"location": "[parameters('location')]",
"dependsOn": [
"[concat('Microsoft.Network/networkSecurityGroups/', parameters('networkSecurityGroupName'))]",
"[concat('Microsoft.Network/virtualNetworks/', parameters('virtualNetworkName'))]",
"[concat('Microsoft.Network/publicIpAddresses/', parameters('publicIpAddressName'))]"
],
"properties": {
"ipConfigurations": [
{
"name": "ipconfig1",
"properties": {
"subnet": {
"id": "[variables('subnetRef')]"
},
"privateIPAllocationMethod": "Dynamic",
"publicIpAddress": {
"id": "[resourceId(resourceGroup().name, 'Microsoft.Network/publicIpAddresses', parameters('publicIpAddressName'))]",
"properties": {
"deleteOption": "[parameters('pipDeleteOption')]"
}
}
}
}
],
"enableAcceleratedNetworking": "[parameters('enableAcceleratedNetworking')]",
"networkSecurityGroup": {
"id": "[variables('nsgId')]"
}
},
"tags": "[parameters('tags')]"
},
{
"name": "[parameters('networkSecurityGroupName')]",
"type": "Microsoft.Network/networkSecurityGroups",
"apiVersion": "2019-02-01",
"location": "[parameters('location')]",
"properties": {
"securityRules": "[parameters('networkSecurityGroupRules')]"
},
"tags": "[parameters('tags')]"
},
{
"name": "[parameters('virtualNetworkName')]",
"type": "Microsoft.Network/virtualNetworks",
"apiVersion": "2020-11-01",
"location": "[parameters('location')]",
"properties": {
"addressSpace": {
"addressPrefixes": "[parameters('addressPrefixes')]"
},
"subnets": "[parameters('subnets')]"
},
"tags": "[parameters('tags')]"
},
{
"name": "[parameters('publicIpAddressName')]",
"type": "Microsoft.Network/publicIpAddresses",
"apiVersion": "2019-02-01",
"location": "[parameters('location')]",
"properties": {
"publicIpAllocationMethod": "[parameters('publicIpAddressType')]"
},
"sku": {
"name": "[parameters('publicIpAddressSku')]"
},
"tags": "[parameters('tags')]"
},
{
"name": "[parameters('virtualMachineName')]",
"type": "Microsoft.Compute/virtualMachines",
"apiVersion": "2021-07-01",
"location": "[parameters('location')]",
"dependsOn": [
"[concat('Microsoft.Network/networkInterfaces/', parameters('networkInterfaceName'))]"
],
"properties": {
"hardwareProfile": {
"vmSize": "[parameters('virtualMachineSize')]"
},
"storageProfile": {
"osDisk": {
"createOption": "fromImage",
"managedDisk": {
"storageAccountType": "[parameters('osDiskType')]"
},
"deleteOption": "[parameters('osDiskDeleteOption')]"
},
"imageReference": {
"publisher": "MicrosoftWindowsServer",
"offer": "WindowsServer",
"sku": "2019-datacenter-gensecond",
"version": "latest"
}
},
"networkProfile": {
"networkInterfaces": [
{
"id": "[resourceId('Microsoft.Network/networkInterfaces', parameters('networkInterfaceName'))]",
"properties": {
"deleteOption": "[parameters('nicDeleteOption')]"
}
}
]
},
"osProfile": {
"computerName": "[parameters('virtualMachineComputerName')]",
"adminUsername": "[parameters('adminUsername')]",
"adminPassword": "[parameters('adminPassword')]",
"windowsConfiguration": {
"enableAutomaticUpdates": true,
"provisionVmAgent": true,
"patchSettings": {
"enableHotpatching": "[parameters('enableHotpatching')]",
"patchMode": "[parameters('patchMode')]"
}
}
},
"licenseType": "Windows_Server"
},
"tags": "[parameters('tags')]"
}
],
"outputs": {
"adminUsername": {
"type": "string",
"value": "[parameters('adminUsername')]"
}
}
}
From this aforementioned script here is our NIC template ;
"resources": [
{
"name": "[parameters('networkInterfaceName')]",
"type": "Microsoft.Network/networkInterfaces",
"apiVersion": "2021-03-01",
"location": "[parameters('location')]",
"dependsOn": [
"[concat('Microsoft.Network/networkSecurityGroups/', parameters('networkSecurityGroupName'))]",
"[concat('Microsoft.Network/virtualNetworks/', parameters('virtualNetworkName'))]",
"[concat('Microsoft.Network/publicIpAddresses/', parameters('publicIpAddressName'))]"
],
"properties": {
"ipConfigurations": [
{
"name": "ipconfig1",
"properties": {
"subnet": {
"id": "[variables('subnetRef')]"
},
"privateIPAllocationMethod": "Dynamic",
"publicIpAddress": {
"id": "[resourceId(resourceGroup().name, 'Microsoft.Network/publicIpAddresses', parameters('publicIpAddressName'))]",
"properties": {
"deleteOption": "[parameters('pipDeleteOption')]"
}
}
}
}
],
OUTPUT SCREENSHOT FOR REFERENCE:-
For more information please refer the below links:-
SIMILAR SO THREAD|ARM Template - Get a private ip address from a network interface id & Cannot find VirtualNetwork with name' creating a 'Microsoft.Web/hostingEnvironments' resource .

How to get ResourceId as output for VM when using copyIndex

I use copyIndex(0) to create several virtualMachine resources (along with publicIP addresses, nic...)
I need the resourceID as output from the deployment for further processing. Usually I do this with the resourceId() function, but since the names are dynamic and copyIndex is not valid in outputs section, I can't figure out the proper syntax for this:
{
"code": "DeploymentOutputEvaluationFailed",
"message": "Unable to evaluate template outputs: 'resourceID'. Please see error details and deployment operations. Please see https://aka.ms/arm-debug for usage details.",
"details": [
{
"code": "DeploymentOutputEvaluationFailed",
"target": "resourceID",
"message": "The template output 'resourceID' is not valid: The template function 'copyIndex' is not expected at this location. The function can only be used in a resource with copy specified. Please see https://aka.ms/arm-copy for usage details.."
}
]
}
I guess I need to change resourceID to array, but what is the proper syntax for fetching the resourceId of the dynamically created VMs?
Full ARM template below:
{
"$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"virtualMachineNamePrefix": {
"type": "string"
},
"virtualMachineSize": {
"type": "string"
},
"virtualMachineCount": {
"type": "int"
},
"adminUsername": {
"type": "string"
},
"adminPassword": {
"type": "secureString"
}
},
"variables": {
"resourceGroupName": "[toLower(ResourceGroup().name)]",
"location": "[resourceGroup().location]",
"networkSecurityGroupName": "[concat(variables('resourceGroupName'), '-nsg')]",
"nsgId": "[resourceId(resourceGroup().name, 'Microsoft.Network/networkSecurityGroups', variables('networkSecurityGroupName'))]",
"subnetName": "default",
"virtualNetworkId": "[concat('/subscriptions/', subscription().subscriptionId, '/resourceGroups/', ResourceGroup().name, '/providers/Microsoft.Network/virtualNetworks/', variables('resourceGroupName'), '-vnet')]",
"operatingSystem": "Server2016",
"operatingSystemValues": {
"Server2016": {
"PublisherValue": "MicrosoftWindowsServer",
"OfferValue": "WindowsServer",
"SkuValue": "2016-Datacenter"
}
},
"subnetRef": "[concat(variables('virtualNetworkId'), '/subnets/', variables('subnetName'))]"
},
"resources": [
{
"apiVersion": "2016-03-30",
"type": "Microsoft.Network/publicIPAddresses",
"name": "[concat(parameters('virtualMachineNamePrefix'), copyIndex(0), '-ip')]",
"location": "[variables('location')]",
"copy": {
"name": "PIPCopy",
"count": "[parameters('virtualMachineCount')]"
},
"tags": {
"displayName": "[concat(parameters('virtualMachineNamePrefix'), copyIndex(0), '-ip')]"
},
"properties": {
"publicIPAllocationMethod": "Dynamic"
}
},
{
"name": "[concat(parameters('virtualMachineNamePrefix'), copyIndex(0), '-nic')]",
"type": "Microsoft.Network/networkInterfaces",
"apiVersion": "2016-03-30",
"location": "[variables('location')]",
"copy": {
"name": "NICCopy",
"count": "[parameters('virtualMachineCount')]"
},
"dependsOn": [
"[concat('Microsoft.Network/publicIpAddresses/', parameters('virtualMachineNamePrefix'), copyIndex(0), '-ip')]"
],
"properties": {
"ipConfigurations": [
{
"name": "ipconfig1",
"properties": {
"privateIPAllocationMethod": "Dynamic",
"publicIPAddress": {
"id": "[resourceId('Microsoft.Network/publicIPAddresses', concat(parameters('virtualMachineNamePrefix'), copyIndex(0), '-ip'))]"
},
"subnet": {
"id": "[variables('subnetRef')]"
}
}
}
],
"networkSecurityGroup": {
"id": "[variables('nsgId')]"
}
}
},
{
"type": "Microsoft.Compute/virtualMachines",
"name": "[concat(parameters('virtualMachineNamePrefix'), copyIndex(0))]",
"apiVersion": "2017-03-30",
"location": "[variables('location')]",
"identity": {
"type": "SystemAssigned"
},
"copy": {
"name": "VMcopy",
"count": "[parameters('virtualMachineCount')]"
},
"properties": {
"hardwareProfile": {
"vmSize": "[parameters('virtualMachineSize')]"
},
"storageProfile": {
"imageReference": {
"publisher": "[variables('operatingSystemValues')[variables('operatingSystem')].PublisherValue]",
"offer": "[variables('operatingSystemValues')[variables('operatingSystem')].OfferValue]",
"sku": "[variables('operatingSystemValues')[variables('operatingSystem')].SkuValue]",
"version": "latest"
},
"osDisk": {
"name": "[concat(parameters('virtualMachineNamePrefix'),copyIndex(0), '-disk')]",
"createOption": "FromImage",
"managedDisk": {
"storageAccountType": "Premium_LRS"
},
"caching": "ReadWrite"
}
},
"osProfile": {
"computerName": "[concat(parameters('virtualMachineNamePrefix'),copyIndex(0))]",
"adminUsername": "[parameters('adminUsername')]",
"windowsConfiguration": {
"provisionVMAgent": true
},
"secrets": [],
"adminPassword": "[parameters('adminPassword')]"
},
"networkProfile": {
"networkInterfaces": [
{
"id": "[resourceId('Microsoft.Network/networkInterfaces', concat(parameters('virtualMachineNamePrefix'), copyIndex(0), '-nic'))]"
}
]
}
},
"dependsOn": [
"[concat('Microsoft.Network/networkInterfaces/', parameters('virtualMachineNamePrefix'), copyIndex(0), '-nic')]"
]
}
],
"outputs": {
"resourceID": {
"type": "string",
"value": "[resourceId('Microsoft.Network/networkInterfaces', concat(parameters('virtualMachineNamePrefix'), copyIndex(0)))]"
}
}
}
UPDATE:
Thanks # 4c74356b41 for a working answer:
"copy": [
{
"name": "resources",
"count": "[parameters('virtualMachineCount')]",
"input": {
"id": "[resourceId('Microsoft.Compute/virtualMachines', concat(parameters('virtualMachineNamePrefix'), copyIndex('resources')))]"
}
}
]
probably use a variable and output it?
"variables": {
"copy": [
{
"name": "resources",
"count": "[parameters('virtualMachineCount')]"
"input": {
"id": "[resourceId('Microsoft.Network/networkInterfaces', concat(parameters('virtualMachineNamePrefix'), copyIndex('resources')))]"
}
}
]
}
and just use that var:
"outputs": {
"resourceID": {
"type": "array",
"value": "[variables('resources')]"
}
}
update: I've just noticed your comment in one of the previous answers, so I'm not sure if this answer is what you are looking for, if not - tell me in the comments what you are after exactly, since I'm a bit confused.

Deploy Azure VM , ARM template with basic software

I am trying to create Azure VM with ARM template with basic software, I've written ARM template for this but I am unable to deploy VM in this template.
Please refer below error :-
{"code":"DeploymentFailed","message":"At least one resource deployment
operation failed. Please list deployment operations for details.
Please see https://aka.ms/arm-debug for usage
details.","details":[{"code":"BadRequest","message":"{\r\n \"error\":
{\r\n \"code\": \"InvalidTemplate\",\r\n \"message\": \"Unable to
process template language expressions for resource
'/subscriptions/b312b990-e271-4a63-a63a-d6768e1c7aad/resourceGroups/rohittestrg/providers/Microsoft.Compute/virtualMachines/mydevboxvm1'
at line '110' and column '9'. 'The template parameter ' ' is not
found. Please see https://aka.ms/arm-template/#parameters for usage
details.'\"\r\n }\r\n}"}]}.
I used this link for reference
https://raw.githubusercontent.com/FBoucher/SimpleDevBox/master/azure-deploy.json
Powershell script:
https://raw.githubusercontent.com/rt7055/Testrepo/master/simpledevbox.ps1?token=AtI3tjSPO7YLVq2xI2zPwznWAZPiWzd2ks5cWbmOwA%3D%3D
I am trying to do this on Windows server.
This would be great if you can provide solution for this.
{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters":{
"scriptURL": {
"type":"string"
},
"adminUsername": {
"type":"string"
},
"adminPassword": {
"type":"string"
}
},
"variables": {
"scriptURL":" https://raw.githubusercontent.com/rt7055/Testrepo/master/simpledevbox.ps1?token=AtI3to3qKSHYNEZh9tfMfXZQ-P4zRULeks5cWZ5kwA%3D%3D "
},
"resources": [
{
"type": "Microsoft.Storage/storageAccounts",
"name": "[toLower('mydevboxstoragekata')]",
"apiVersion": "2015-06-15",
"location": "[resourceGroup().location]",
"tags": {
"displayName": "mydevbox Storage Account"
},
"properties": {
"accountType":"Standard_LRS"
}
},
{
"apiVersion": "2017-06-01",
"type": "Microsoft.Network/publicIPAddresses",
"name": "mydevbox-PublicIP",
"location": "[resourceGroup().location]",
"tags": {
"displayName": "PublicIPAddress"
}
},
{
"apiVersion": "2017-06-01",
"type": "Microsoft.Network/virtualNetworks",
"name": "mydevbox-VirtualNetwork",
"location": "[resourceGroup().location]",
"tags": {
"displayName": "VirtualNetwork"
},
"properties": {
"addressSpace": {
"addressPrefixes": [
"10.0.0.0/16"
]
},
"subnets": [
{
"name": "mydevbox-VirtualNetwork-Subnet",
"properties": {
"addressPrefix": "10.0.0.0/24"
}
}
]
}
},
{
"apiVersion": "2015-06-15",
"type": "Microsoft.Network/networkInterfaces",
"name": "mydevbox-NetworkInterface",
"location": "[resourceGroup().location]",
"dependsOn": [
"Microsoft.Network/publicIPAddresses/mydevbox-PublicIP",
"Microsoft.Network/virtualNetworks/mydevbox-VirtualNetwork"
],
"tags": {
"displayName": "mydevbox Network Interface"
},
"properties": {
"ipConfigurations": [
{
"name": "ipconfig1",
"properties": {
"privateIPAllocationMethod": "Dynamic",
"publicIPAddress": {
"id": "[resourceId('Microsoft.Network/publicIPAddresses', 'mydevbox-PublicIP')]"
},
"subnet": {
"id": "[concat(resourceId('Microsoft.Network/virtualNetworks', 'mydevbox-VirtualNetwork'), '/subnets/mydevbox-VirtualNetwork-Subnet')]"
}
}
}
]
}
},
{
"apiVersion": "2017-03-30",
"type": "Microsoft.Compute/virtualMachines",
"name": "mydevboxvm1",
"location": "[resourceGroup().location]",
"dependsOn": [
"[concat('Microsoft.Storage/storageAccounts/', toLower('mydevboxstoragekata'))]",
"Microsoft.Network/networkInterfaces/mydevbox-NetworkInterface"
],
"tags": {
"displayName": "mydevboxvm1"
},
"properties": {
"hardwareProfile": {
"vmSize": "Standard_A1"
},
"osProfile": {
"computerName": "mydevboxvm1",
"adminUsername": "[parameters('')]",
"adminPassword": "[parameters('')]"
},
"storageProfile": {
"imageReference": {
"publisher": "MicrosoftWindowsServer",
"offer": "WindowsServer",
"sku": "2012-R2-Datacenter",
"version": "latest"
},
"osDisk": {
"createOption": "FromImage"
},
"dataDisks":[
{
"diskSizeGB": 1023,
"lun": 0,
"createOption": "Empty"}
]
},
"networkProfile": {
"networkInterfaces": [
{
"id": "[resourceId('Microsoft.Network/networkInterfaces', 'mydevbox-NetworkInterface')]"
}
]
},
"diagnosticsProfile": {
"bootDiagnostics": {
"enabled": true,
"storageUri": "[concat('http://', toLower('mydevboxstoragekata'), '.blob.core.windows.net')]"
}
}
},
"resources": [
{
"apiVersion": "2017-03-30",
"type": "extensions",
"name": "config-app",
"location": "[resourceGroup().location]",
"dependsOn": [
"[concat('Microsoft.Compute/virtualMachines/', 'mydevboxvm1')]"
],
"tags": {
"displayName": "config-app"
},
"properties": {
"publisher": "Microsoft.Compute",
"type": "CustomScriptExtension",
"typeHandlerVersion": "1.9",
"autoUpgradeMinorVersion": true,
"settings": {
"fileUris": [
"[variables('scriptURL')]"
]
},
"protectedSettings": {
"commandToExecute": "[concat('powershell -ExecutionPolicy Unrestricted -File ', './simpledevbox.ps1')]"
}
}
}
]
}
],
"outputs": {}
}
You forgot to pass parameter names here:
"adminUsername": "[parameters('')]",
"adminPassword": "[parameters('')]"
this should be this:
"adminUsername": "[parameters('adminUsername')]",
"adminPassword": "[parameters('adminPassword')]"

Setting environment variables in an azure vm template

Hi I'm curious if some one can help me with settings for adding environment variables to a vm in an azure template.
{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"adminPassword": {
"defaultValue": null,
"type": "SecureString"
},
"processtype": {
"type": "String",
"defaultValue": "sfm",
"allowedValues": [
"sfm",
"mvs",
"mesh",
"text",
"import",
"temp"
]
},
"vmSize": {
"type" : "String",
"defaultValue": "Standard_D12_v2",
"allowedValues": [
"Basic_A2",
"Standard_D12_v2",
"Standard_D13_v2",
"Standard_D14_v2"
]
},
"id": {
"type": "String",
"defaultValue": "0000",
"minLength": 4,
"maxLength": 4
}
},
"variables": {
"location":"northeurope",
"instance_name" : "[toUpper(concat('ps_', parameters('processtype'),'_',parameters('id')))]",
"unique_string": "[uniqueString(resourceGroup().id, variables('instance_name'))]",
"unique_name" : "[toUpper(variables('unique_string'))]",
"networkSecurityGroupName": "[toUpper(concat('nsg',variables('unique_name')))]",
"virtualNetworkName" : "[toUpper(concat('ns', variables('unique_name')))]",
"addressPrefix": "10.0.0.0/16",
"subnetName": "Subnet",
"subnetPrefix": "10.0.0.0/24",
"subnetRef": "[toUpper(concat(variables('vnetID'),'/subnets/',variables('subnetName')))]",
"vnetID":"[resourceId('Microsoft.Network/virtualNetworks',variables('virtualNetworkName'))]",
"publicIPAddressName": "[toUpper(concat('ip', variables('unique_name')))]",
"storageAccountName": "<storageAcName>",
"networkInterfaceName": "[toUpper(concat('ni', variables('unique_name')))]"
},
"resources": [
{
"comments": "Generalized VM",
"type": "Microsoft.Compute/virtualMachines",
"name": "[concat(variables('instance_name'), '_' ,variables('unique_name'))]",
"apiVersion": "2015-06-15",
"location": "[variables('location')]",
"properties": {
"hardwareProfile": {
"vmSize": "[parameters('vmSize')]"
},
"storageProfile": {
"osDisk": {
"osType": "Windows",
"name": "[variables('unique_name')]",
"createOption": "FromImage",
"image":{
"uri": "<VHDPATH>"
},
"vhd": {
"uri": "[concat('<vhd path>',variables('unique_name'),'.vhd')]"
},
"caching": "ReadWrite"
},
"dataDisks": []
},
"osProfile": {
"computerName": "[parameters('processtype')]",
"adminUsername": "<username>",
"windowsConfiguration": {
"provisionVMAgent": true,
"enableAutomaticUpdates": true
},
"secrets": [],
"adminPassword": "[parameters('adminPassword')]"
},
"networkProfile": {
"networkInterfaces": [
{
"id": "[resourceId('Microsoft.Network/networkInterfaces', variables('networkInterfaceName'))]"
}
]
}
},
"dependsOn": [
"[resourceId('Microsoft.Storage/storageAccounts', variables('storageAccountName'))]",
"[resourceId('Microsoft.Network/networkInterfaces', variables('networkInterfaceName'))]"
]
},
{
"comments": "Generalized Security Group",
"type": "Microsoft.Network/networkSecurityGroups",
"name": "[variables('networkSecurityGroupName')]",
"apiVersion": "2016-03-30",
"location": "[variables('location')]",
"properties": {
"securityRules": [
{
"name": "default-allow-rdp",
"properties": {
"protocol": "*",
"sourcePortRange": "*",
"destinationPortRange": "3389",
"sourceAddressPrefix": "*",
"destinationAddressPrefix": "*",
"access": "Allow",
"priority": 1000,
"direction": "Inbound"
}
}
]
},
"dependsOn": []
},
{
"comments": "Generalized IP Configuration",
"type": "Microsoft.Network/publicIPAddresses",
"name": "[variables('publicIPAddressName')]",
"apiVersion": "2016-03-30",
"location": "[variables('location')]",
"properties": {
"publicIPAllocationMethod": "Dynamic",
"idleTimeoutInMinutes": 4
},
"dependsOn": []
},
{
"comments": "Generalized virtual network",
"type": "Microsoft.Network/virtualNetworks",
"name": "[variables('virtualNetworkName')]",
"apiVersion": "2016-03-30",
"location": "[variables('location')]",
"properties": {
"addressSpace": {
"addressPrefixes": [ "[variables('addressPrefix')]" ]
},
"subnets": [ {
"name": "[variables('subnetName')]",
"properties": {
"addressPrefix": "[variables('subnetPrefix')]"
}
} ]
},
"dependsOn": []
},
{
"comments": "Generalized Network Interface",
"type": "Microsoft.Network/networkInterfaces",
"name": "[variables('networkInterfaceName')]",
"apiVersion": "2016-03-30",
"location": "[variables('location')]",
"properties": {
"ipConfigurations": [
{
"name": "ipconfig1",
"properties": {
"privateIPAllocationMethod": "Dynamic",
"publicIPAddress": {
"id": "[resourceId('Microsoft.Network/publicIPAddresses', variables('publicIPAddressName'))]"
},
"subnet": {
"id": "[concat(resourceId('Microsoft.Network/virtualNetworks', variables('virtualNetworkName')), '/subnets/', variables('subnetName'))]"
}
}
}
],
"dnsSettings": {
"dnsServers": []
},
"enableIPForwarding": false,
"networkSecurityGroup": {
"id": "[resourceId('Microsoft.Network/networkSecurityGroups', variables('networkSecurityGroupName'))]"
}
},
"dependsOn": [
"[resourceId('Microsoft.Network/publicIPAddresses', variables('publicIPAddressName'))]",
"[resourceId('Microsoft.Network/virtualNetworks', variables('virtualNetworkName'))]",
"[resourceId('Microsoft.Network/networkSecurityGroups', variables('networkSecurityGroupName'))]"
]
},
{
"comments": "Generalized storage account.",
"type": "Microsoft.Storage/storageAccounts",
"sku": {
"name": "Standard_LRS",
"tier": "Standard"
},
"kind": "Storage",
"name": "[variables('storageAccountName')]",
"apiVersion": "2016-01-01",
"location": "[variables('location')]",
"tags": {},
"properties": {},
"dependsOn": []
}
]
}
Above is my current template. Where can Ispecify environment variables or some other variable that is accessable inside the vm?

Adding Availability Set To Azure Virtual Machine Template Creation

I can create a Azure VM with a specific VHD from the template below but how do I also add it to an Availability Set. I can't do this after VM creation so I need to do it here.
{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"location": {
"type": "string",
"metadata": {
"description": "Location to create the VM in"
}
},
"osDiskVhdUri": {
"type": "string",
"metadata": {
"description": "Uri of the existing VHD"
}
},
"osType": {
"type": "string",
"allowedValues": [
"Windows",
"Linux"
],
"metadata": {
"description": "Type of OS on the existing vhd"
}
},
"vmSize": {
"type": "string",
"defaultValue": "Standard_D2",
"metadata": {
"description": "Size of the VM"
}
},
"vmName": {
"type": "string",
"metadata": {
"description": "Name of the VM"
}
}
},
"variables": {
"api-version": "2015-06-15",
"addressPrefix": "10.0.0.0/16",
"subnetName": "Subnet",
"subnetPrefix": "10.0.0.0/24",
"publicIPAddressName": "specializedVMPublicIP",
"publicIPAddressType": "Dynamic",
"virtualNetworkName": "specializedVMVNET",
"vnetID": "[resourceId('Microsoft.Network/virtualNetworks',variables('virtualNetworkName'))]",
"subnetRef": "[concat(variables('vnetID'),'/subnets/',variables('subnetName'))]",
"nicName": "specializedVMNic"
},
"resources": [
{
"apiVersion": "[variables('api-version')]",
"type": "Microsoft.Network/virtualNetworks",
"name": "[variables('virtualNetworkName')]",
"location": "[parameters('location')]",
"properties": {
"addressSpace": {
"addressPrefixes": [
"[variables('addressPrefix')]"
]
},
"subnets": [
{
"name": "[variables('subnetName')]",
"properties": {
"addressPrefix": "[variables('subnetPrefix')]"
}
}
]
}
},
{
"apiVersion": "[variables('api-version')]",
"type": "Microsoft.Network/networkInterfaces",
"name": "[variables('nicName')]",
"location": "[parameters('location')]",
"dependsOn": [
"[concat('Microsoft.Network/publicIPAddresses/', variables('publicIPAddressName'))]",
"[concat('Microsoft.Network/virtualNetworks/', variables('virtualNetworkName'))]"
],
"properties": {
"ipConfigurations": [
{
"name": "ipconfig1",
"properties": {
"privateIPAllocationMethod": "Dynamic",
"publicIPAddress": {
"id": "[resourceId('Microsoft.Network/publicIPAddresses',variables('publicIPAddressName'))]"
},
"subnet": {
"id": "[variables('subnetRef')]"
}
}
}
]
}
},
{
"apiVersion": "[variables('api-version')]",
"type": "Microsoft.Network/publicIPAddresses",
"name": "[variables('publicIPAddressName')]",
"location": "[parameters('location')]",
"properties": {
"publicIPAllocationMethod": "[variables('publicIPAddressType')]"
}
},
{
"apiVersion": "[variables('api-version')]",
"type": "Microsoft.Compute/virtualMachines",
"name": "[parameters('vmName')]",
"location": "[parameters('location')]",
"dependsOn": [
"[concat('Microsoft.Network/networkInterfaces/', variables('nicName'))]"
],
"properties": {
"hardwareProfile": {
"vmSize": "[parameters('vmSize')]"
},
"storageProfile": {
"osDisk": {
"name": "[concat(parameters('vmName'),'-osDisk')]",
"osType": "[parameters('osType')]",
"caching": "ReadWrite",
"vhd": {
"uri": "[parameters('osDiskVhdUri')]"
},
"createOption": "Attach"
}
},
"networkProfile": {
"networkInterfaces": [
{
"id": "[resourceId('Microsoft.Network/networkInterfaces', variables('nicName'))]"
}
]
}
}
}
]
}
The best way to figure things like this out is to poke through the templates until you find what you need!
So according to this template, you create an Availability Set like this
"resources": [
{
"type": "Microsoft.Compute/availabilitySets",
"name": "availabilitySet1",
"apiVersion": "2015-06-15",
"location": "[parameters('location')]",
"properties": {
"platformFaultDomainCount": "3",
"platformUpdateDomainCount": "20"
}
}
]
and then (according to this) you use it like this
"apiVersion": "[variables('apiVersion')]",
"type": "Microsoft.Compute/virtualMachines",
"name": "[concat('myvm', copyIndex())]",
"location": "[variables('location')]",
"copy": {
"name": "virtualMachineLoop",
"count": "[parameters('numberOfInstances')]"
},
"dependsOn": [
"[concat('Microsoft.Network/networkInterfaces/', 'nic', copyindex())]",
"[concat('Microsoft.Storage/storageAccounts/', variables('storageAccountName'))]"
],
"properties": {
"availabilitySet": {
"id": "[resourceId('Microsoft.Compute/availabilitySets', variables('availabilitySetName'))]"
},
You would need to be looking at the Microsoft.Compute/availabilitySets resource provider, here is some sample JSON from one of my templates.
"resources": [
{
"type": "Microsoft.Compute/availabilitySets",
"name": "availabilitySet1",
"apiVersion": "2015-06-15",
"location": "[parameters('location')]",
"properties": {
"platformFaultDomainCount": "3",
"platformUpdateDomainCount": "20"
}
}
]
You then need to use the availabilitySet property of the virtualMachines resource provider to add the VMs to the availability set. Make sure you use dependsOn to ensure the availability set is created before the VM. As an example if you refering to it by name:
"properties": {
"hardwareProfile": { "vmSize": "Standard_A0" },
"networkProfile": ...,
"availabilitySet": { "id": "availabilitySet1" },
}

Resources