Skip to content

Instantly share code, notes, and snippets.

@takekazuomi
Last active July 28, 2022 12:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save takekazuomi/d26613bf9e10a4f085e5f0218b90c106 to your computer and use it in GitHub Desktop.
Save takekazuomi/d26613bf9e10a4f085e5f0218b90c106 to your computer and use it in GitHub Desktop.
deploy multiple vm using loop
param vmPrefix string
param location string
param vmCount int
module vm 'vm.bicep' = [for i in range(0, vmCount): {
name: 'vm-${i}'
params:{
name: '${vmPrefix}-${substring(uniqueString(resourceGroup().id), 0, 5)}-${i}'
location: location
vnetName:vnet.name
subnetName: 'subnet1'
}
}]
resource vnet 'Microsoft.Network/virtualNetworks@2019-11-01' = {
name: 'name'
location: resourceGroup().location
properties: {
addressSpace: {
addressPrefixes: [
'10.0.0.0/16'
]
}
subnets: [
{
name: 'subnet1'
properties: {
addressPrefix: '10.0.0.0/24'
}
}
]
}
}
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"metadata": {
"_generator": {
"name": "bicep",
"version": "0.3.539.46024",
"templateHash": "12748056562629077740"
}
},
"parameters": {
"vmPrefix": {
"type": "string"
},
"location": {
"type": "string"
},
"vmCount": {
"type": "int"
}
},
"functions": [],
"resources": [
{
"type": "Microsoft.Network/virtualNetworks",
"apiVersion": "2019-11-01",
"name": "name",
"location": "[resourceGroup().location]",
"properties": {
"addressSpace": {
"addressPrefixes": [
"10.0.0.0/16"
]
},
"subnets": [
{
"name": "subnet1",
"properties": {
"addressPrefix": "10.0.0.0/24"
}
}
]
}
},
{
"copy": {
"name": "vm",
"count": "[length(range(0, parameters('vmCount')))]"
},
"type": "Microsoft.Resources/deployments",
"apiVersion": "2019-10-01",
"name": "[format('vm-{0}', range(0, parameters('vmCount'))[copyIndex()])]",
"properties": {
"expressionEvaluationOptions": {
"scope": "inner"
},
"mode": "Incremental",
"parameters": {
"name": {
"value": "[format('{0}-{1}-{2}', parameters('vmPrefix'), substring(uniqueString(resourceGroup().id), 0, 5), range(0, parameters('vmCount'))[copyIndex()])]"
},
"location": {
"value": "[parameters('location')]"
},
"vnetName": {
"value": "name"
},
"subnetName": {
"value": "subnet1"
}
},
"template": {
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"metadata": {
"_generator": {
"name": "bicep",
"version": "0.3.539.46024",
"templateHash": "18020905035165685897"
}
},
"parameters": {
"name": {
"type": "string"
},
"location": {
"type": "string"
},
"vnetName": {
"type": "string"
},
"subnetName": {
"type": "string"
}
},
"functions": [],
"resources": [
{
"type": "Microsoft.Network/networkInterfaces",
"apiVersion": "2020-11-01",
"name": "@{name}-nic",
"location": "[parameters('location')]",
"properties": {
"ipConfigurations": [
{
"name": "ipconfig1",
"properties": {
"privateIPAllocationMethod": "Dynamic",
"subnet": {
"id": "[resourceId('Microsoft.Network/virtualNetworks/subnets', parameters('vnetName'), parameters('subnetName'))]"
}
}
}
]
}
},
{
"type": "Microsoft.Compute/virtualMachines",
"apiVersion": "2020-12-01",
"name": "[parameters('name')]",
"location": "[parameters('location')]",
"properties": {
"networkProfile": {
"networkInterfaces": [
{
"id": "[resourceId('Microsoft.Network/networkInterfaces', '@{name}-nic')]"
}
]
}
},
"dependsOn": [
"[resourceId('Microsoft.Network/networkInterfaces', '@{name}-nic')]"
]
}
]
}
},
"dependsOn": [
"[resourceId('Microsoft.Network/virtualNetworks', 'name')]"
]
}
]
}
param name string
param location string
param vnetName string
param subnetName string
resource vnet 'Microsoft.Network/virtualNetworks@2019-11-01' existing = {
name: vnetName
}
resource subnet 'Microsoft.Network/virtualNetworks/subnets@2020-11-01' existing = {
parent: vnet
name: subnetName
}
resource nic 'Microsoft.Network/networkInterfaces@2020-11-01' = {
name: '@{name}-nic'
location: location
properties: {
ipConfigurations: [
{
name: 'ipconfig1'
properties: {
privateIPAllocationMethod: 'Dynamic'
subnet: {
id: subnet.id
}
}
}
]
}
}
resource virtualMachine 'Microsoft.Compute/virtualMachines@2020-12-01' = {
name: name
location: location
properties: {
networkProfile: {
networkInterfaces: [
{
id: nic.id
}
]
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment