如何通过api创建azure虚拟机??(不是经典的,而是在新的azure管理门户上的)



我想通过api或python-sdk创建azure新的虚拟机,这是我们在azure新管理门户上拥有的,它允许我在门户上使用诸如操作机器的network security group之类的功能。谢谢在此处输入图像描述

您可以使用Visual Studio资源组项目,它将帮助您为VM生成JSON模板,并且您可以使用Powershell或API直接提交模板,

{
  "apiVersion": "2015-06-15",
  "type": "Microsoft.Compute/virtualMachines",
  "name": "[variables('vmName')]",
  "location": "[resourceGroup().location]",
  "tags": {
    "displayName": "VirtualMachine"
  },
  "dependsOn": [
    "[concat('Microsoft.Storage/storageAccounts/', variables('vhdStorageName'))]",
    "[concat('Microsoft.Network/networkInterfaces/', variables('nicName'))]"
  ],
  "properties": {
    "hardwareProfile": {
      "vmSize": "[variables('vmSize')]"
    },
    "osProfile": {
      "computerName": "[variables('vmName')]",
      "adminUsername": "[parameters('adminUsername')]",
      "adminPassword": "[parameters('adminPassword')]"
    },
    "storageProfile": {
      "imageReference": {
        "publisher": "[variables('imagePublisher')]",
        "offer": "[variables('imageOffer')]",
        "sku": "[parameters('windowsOSVersion')]",
        "version": "latest"
      },
      "osDisk": {
        "name": "osdisk",
        "vhd": {
          "uri": "[concat('http://', variables('vhdStorageName'), '.blob.core.windows.net/', variables('vhdStorageContainerName'), '/', variables('OSDiskName'), '.vhd')]"
        },
        "caching": "ReadWrite",
        "createOption": "FromImage"
      }
    },
    "networkProfile": {
      "networkInterfaces": [
        {
          "id": "[resourceId('Microsoft.Network/networkInterfaces', variables('nicName'))]"
        }
      ]
    },
    "diagnosticsProfile": {
      "bootDiagnostics": {
        "enabled": true,
        "storageUri": "[concat('http://', variables('diagnosticsStorageName'), '.blob.core.windows.net')]"
      }
    }
  },

用于Power Shell脚本

# Create or update the resource group using the specified template file and template parameters file

New AzureRmResourceGroup-Name$ResourceGroupName-Location$ResourceGroupLocation-Verbose-Force-ErrorAction Stop

New AzureRmResourceGroupDeployment-Name((Get-ChildItem$TemplateFile).BaseName+'-'+((Get-Date).ToUniversalTime().ToString('MMdd-HHm')) -ResourceGroupName $ResourceGroupName-TemplateFile$TemplateFile -TemplateParameterFile $TemplateParametersFile@可选参数`-强制-详细

您应该使用Azure资源管理模板,该模板使您能够使用要创建的环境的声明形式模板说明在此处https://azure.microsoft.com/en-us/documentation/articles/resource-group-authoring-templates/你会在GitHub上找到很多样本(寻找azure快速启动模板)

如果您仍然希望使用python SDK,则必须进行相应的REST API调用。这里有一篇关于这种方式的好文章:http://blogs.msdn.com/b/scicoria/archive/2015/02/12/azure-resource-manager-creating-an-iaas-vm-within-a-vnet.aspx

希望这能有所帮助顺致敬意,Stéphane

最新更新