Maszyna wirtualna – ARM Templates cz.4

Jak stworzyć VM przy użyciu ARM Templates (JSON)

Plan: Stworzyć dwie maszyny wirtualne

W pierwszej kolejności zakładamy resource group ponieważ ARM Template tego nie potrafi. Użyjemy do tego komendy Azure CLI:

az group create –name OURGROUP –location westus2

W celu użycia ARM Template używamy poniższej komendy:

az group deployment create –resource-group OURGROUP –template-file C:\path\azuredeploy.json

Zakładamy, że plik azuredeploy.json znajduje się na dysku C: w katalogu path 🙂

Poniżej zawartość pliku.

file: azuredeploy.json

{
“$schema”: “https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#”,
“contentVersion”: “1.0.0.0”,
“parameters”: {
“virtualMachineSize”: {
“type”: “string”,
“defaultValue”: “Standard_DS1_v2”,
“metadata”: {
“description”: “Virtual machine size”
}
},
“adminUsername”: {
“type”: “string”,
“metadata”: {
“description”: “Default Admin username”
}
},
“adminPassword”: {
“type”: “securestring”,
“metadata”: {
“description”: “Password for admin”
}
},
“Environment”: {
“type”: “string”,
“allowedValues”: [ “prod”, “test”, “dev” ],
“metadata”: {
“description”: “Environment”
}
},
“storageAccountType”: {
“type”: “string”,
“defaultValue”: “Standard_LRS”,
“metadata”: {
“description”: “Storage Account type for the VM and VM diagnostic storage”
},
“allowedValues”: [
“Standard_LRS”,
“Premium_LRS”
]
},
“VMNAME”: {
“type”: “string”,
“defaultValue”: “VM”,
“metadata”: {
“description”: “VM Naming Standard”
}
},
“location”: {
“type”: “string”,
“defaultValue”: “[resourceGroup().location]”,
“metadata”: {
“description”: “Location for all resources.”
}
}
},
“variables”: {
“virtualMachineName1”: “[concat(parameters(‘VMNAME’),’01-‘,parameters(‘Environment’))]”,
“virtualMachineName2”: “[concat(parameters(‘VMNAME’),’02-‘,parameters(‘Environment’))]”,
“nic1”: “[concat(parameters(‘VMNAME’),’01-‘,’nic’)]”,
“nic2”: “[concat(parameters(‘VMNAME’),’02-‘,’nic’)]”,
“virtualNetworkName”: “virtualNetwork”,
“subnet1Name”: “[concat(variables(‘virtualMachineName1′),’subnet’)]”,
“subnet2Name”: “[concat(variables(‘virtualMachineName2′),’subnet’)]”,
“publicIPAddressName1”: “[concat(‘pubip’,variables(‘virtualMachineName1’))]”,
“publicIPAddressName2”: “[concat(‘pubip’,variables(‘virtualMachineName2’))]”,
“subnet1Ref”: “[resourceId(‘Microsoft.Network/virtualNetworks/subnets’, variables(‘virtualNetworkName’), variables(‘subnet1Name’))]”,
“subnet2Ref”: “[resourceId(‘Microsoft.Network/virtualNetworks/subnets’, variables(‘virtualNetworkName’), variables(‘subnet2Name’))]”,
“diagStorageAccountName”: “[concat(‘diagstor100’,parameters(‘Environment’))]”,
“networkSecurityGroupName1”: “[concat(‘NSG’,variables(‘virtualMachineName1’))]”,
“networkSecurityGroupName2”: “[concat(‘NSG’,variables(‘virtualMachineName2’))]”
“linked”: {
“templateUrl”: “https: //raw.githubusercontent.com/easycloudpl/AzureResourceGroup3/master/AzureResourceGroup3/vemki.json”
}
},
“resources”: [
{
“name”: “[variables(‘virtualMachineName1’)]”,
“type”: “Microsoft.Compute/virtualMachines”,
“apiVersion”: “2017-03-30”,
“location”: “[parameters(‘location’)]”,
“comments”: “This is the virtual machine that you’re building.”,
“dependsOn”: [
“[variables(‘nic1’)]”,
“[variables(‘diagStorageAccountName’)]”
],
“properties”: {
“osProfile”: {
“computerName”: “[variables(‘virtualMachineName1’)]”,
“adminUsername”: “[parameters(‘adminUsername’)]”,
“adminPassword”: “[parameters(‘adminPassword’)]”
“hardwareProfile”: {
“vmSize”: “[parameters(‘virtualMachineSize’)]”
}
},
“storageProfile”: {
“imageReference”: {
“publisher”: “Canonical”,
“offer”: “UbuntuServer”,
“sku”: “18.04-LTS”,
“version”: “latest”
},
“osDisk”: {
“createOption”: “FromImage”
},
“dataDisks”: []
},
“networkProfile”: {
“networkInterfaces”: [
{
“properties”: {
“primary”: true
},
“id”: “[resourceId(‘Microsoft.Network/networkInterfaces’, variables(‘nic1’))]”
}

]
},
“diagnosticsProfile”: {
“bootDiagnostics”: {
“enabled”: true,
“storageUri”: “[reference(resourceId(‘Microsoft.Storage/storageAccounts’, variables(‘diagStorageAccountName’)), ‘2017-06-01’).primaryEndpoints[‘blob’]]”
}
}
}
},
{
“name”: “[variables(‘virtualMachineName2’)]”,
“type”: “Microsoft.Compute/virtualMachines”,
“apiVersion”: “2017-03-30”,
“location”: “[parameters(‘location’)]”,
“comments”: “This is the virtual machine that you’re building.”,
“dependsOn”: [
“[variables(‘nic1’)]”,
“[variables(‘diagStorageAccountName’)]”
],
“properties”: {
“osProfile”: {
“computerName”: “[variables(‘virtualMachineName2’)]”,
“adminUsername”: “[parameters(‘adminUsername’)]”,
“adminPassword”: “[parameters(‘adminPassword’)]”
},
“hardwareProfile”: {
“vmSize”: “[parameters(‘virtualMachineSize’)]”
},
“storageProfile”: {
“imageReference”: {
“publisher”: “Canonical”,
“offer”: “UbuntuServer”,
“sku”: “18.04-LTS”,
“version”: “latest”
},
“osDisk”: {
“createOption”: “FromImage”
},
“dataDisks”: []
},
“networkProfile”: {
“networkInterfaces”: [
{
“properties”: {
“primary”: true
},
“id”: “[resourceId(‘Microsoft.Network/networkInterfaces’, variables(‘nic2’))]”
}

]
},
“diagnosticsProfile”: {
“bootDiagnostics”: {
“enabled”: true,
“storageUri”: “[reference(resourceId(‘Microsoft.Storage/storageAccounts’, variables(‘diagStorageAccountName’)), ‘2017-06-01’).primaryEndpoints[‘blob’]]”
}
}
}
},

{
“type”: “Microsoft.Storage/storageAccounts”,
“name”: “[variables(‘diagStorageAccountName’)]”,
“apiVersion”: “2017-06-01”,
“location”: “[parameters(‘location’)]”,
“sku”: {
“name”: “[parameters(‘storageAccountType’)]”
},
“kind”: “Storage”,
“properties”: {}
},
{
“type”: “Microsoft.Network/virtualNetworks”,
“name”: “[variables(‘virtualNetworkName’)]”,
“apiVersion”: “2017-06-01”,
“location”: “[parameters(‘location’)]”,
“comments”: “This will build a Virtual Network.”,
“properties”: {
“addressSpace”: {
“addressPrefixes”: [
“10.0.0.0/16”
]
},
“subnets”: [
{
“name”: “[variables(‘subnet1Name’)]”,
“properties”: {
“addressPrefix”: “10.0.0.0/24”
}
},
{
“name”: “[variables(‘subnet2Name’)]”,
“properties”: {
“addressPrefix”: “10.0.1.0/24”
}
}
]
}
},
{
“name”: “[variables(‘nic1’)]”,
“type”: “Microsoft.Network/networkInterfaces”,
“apiVersion”: “2017-06-01”,
“location”: “[parameters(‘location’)]”,
“comments”: “This will be your Primary NIC”,
“dependsOn”: [
“[variables(‘publicIpAddressName1’)]”,
“[variables(‘networkSecurityGroupName1’)]”,
“[variables(‘virtualNetworkName’)]”
],
“properties”: {
“ipConfigurations”: [
{
“name”: “ipconfig1”,
“properties”: {
“subnet”: {
“id”: “[variables(‘subnet1Ref’)]”
},
“privateIPAllocationMethod”: “Dynamic”,
“publicIpAddress”: {
“id”: “[resourceId(‘Microsoft.Network/publicIpAddresses’, variables(‘publicIpAddressName1’))]”
}
}
}
],
“networkSecurityGroup”: {
“id”: “[resourceId(‘Microsoft.Network/networkSecurityGroups’, variables(‘networkSecurityGroupName1’))]”
}
}
},
{
“name”: “[variables(‘nic2’)]”,
“type”: “Microsoft.Network/networkInterfaces”,
“apiVersion”: “2017-06-01”,
“location”: “[parameters(‘location’)]”,
“comments”: “This will be your Primary NIC”,
“dependsOn”: [
“[variables(‘publicIpAddressName2’)]”,
“[variables(‘networkSecurityGroupName2’)]”,
“[variables(‘virtualNetworkName’)]”
],
“properties”: {
“ipConfigurations”: [
{
“name”: “ipconfig1”,
“properties”: {
“subnet”: {
“id”: “[variables(‘subnet1Ref’)]”
},
“privateIPAllocationMethod”: “Dynamic”,
“publicIpAddress”: {
“id”: “[resourceId(‘Microsoft.Network/publicIpAddresses’, variables(‘publicIpAddressName2’))]”
}
}
}
],
“networkSecurityGroup”: {
“id”: “[resourceId(‘Microsoft.Network/networkSecurityGroups’, variables(‘networkSecurityGroupName2’))]”
}
}
},

{
“name”: “[variables(‘publicIpAddressName1’)]”,
“type”: “Microsoft.Network/publicIPAddresses”,
“apiVersion”: “2017-06-01”,
“location”: “[parameters(‘location’)]”,
“comments”: “Public IP for your Primary NIC”,
“properties”: {
“publicIPAllocationMethod”: “Dynamic”
}
},
{
“name”: “[variables(‘publicIpAddressName2’)]”,
“type”: “Microsoft.Network/publicIPAddresses”,
“apiVersion”: “2017-06-01”,
“location”: “[parameters(‘location’)]”,
“comments”: “Public IP for your Primary NIC”,
“properties”: {
“publicIPAllocationMethod”: “Dynamic”
}
},
{
“name”: “[variables(‘networkSecurityGroupName1’)]”,
“type”: “Microsoft.Network/networkSecurityGroups”,
“apiVersion”: “2016-09-01”,
“location”: “[parameters(‘location’)]”,
“comments”: “Network Security Group (NSG) for your Primary NIC”,
“properties”: {
“securityRules”: [
{
“name”: “default-allow-ssh”,
“properties”: {
“priority”: 1000,
“sourceAddressPrefix”: “*”,
“protocol”: “Tcp”,
“destinationPortRange”: “22”,
“access”: “Allow”,
“direction”: “Inbound”,
“sourcePortRange”: “*”,
“destinationAddressPrefix”: “*”
}
}
]
}
},
{
“name”: “[variables(‘networkSecurityGroupName2’)]”,
“type”: “Microsoft.Network/networkSecurityGroups”,
“apiVersion”: “2016-09-01”,
“location”: “[parameters(‘location’)]”,
“comments”: “Network Security Group (NSG) for your Primary NIC”,
“properties”: {
“securityRules”: [
{
“name”: “default-allow-ssh”,
“properties”: {
“priority”: 1000,
“sourceAddressPrefix”: “*”,
“protocol”: “Tcp”,
“destinationPortRange”: “22”,
“access”: “Allow”,
“direction”: “Inbound”,
“sourcePortRange”: “*”,
“destinationAddressPrefix”: “*”
}
}
]
}
}
],
“outputs”: {}
}

Tak utworzony template w znacznym stopniu przyśpiesza tworzenie maszyn wirtualnych ale nie tylko. ARM Template wykorzystujemy przy tworzeniu różnych zasobów w Azure.

Przewiń do góry