A Marketplace-en lévő Azure-beli virtuális gép áthelyezése másik előfizetésbe

A következőkre vonatkozik: ✔️ Linux rendszerű windowsos ✔️ virtuális gépek rugalmas méretezési ✔️ csoportjai

Ha egy Marketplace-beli virtuális gépet egy másik előfizetésbe szeretne áthelyezni, át kell helyeznie az operációsrendszer-lemezt az előfizetésbe, majd újra létre kell hoznia a virtuális gépet.

Az adatlemezek új előfizetésbe való áthelyezéséhez nincs szükség erre az eljárásra. Ehelyett hozzon létre egy új virtuális gépet az új előfizetésben a Marketplace-ről, majd helyezze át és csatolja az adatlemezt.

Ez a szkript három műveletet mutat be:

  • Hozzon létre egy pillanatképet egy operációsrendszer-lemezről.
  • Helyezze át a pillanatképet egy másik előfizetésbe.
  • Hozzon létre egy virtuális gépet a pillanatkép alapján.

Előfeltételek

Példaszkript

Ha egy Marketplace-beli virtuális gépet egy másik előfizetésbe szeretne áthelyezni, létre kell hoznia egy új virtuális gépet ugyanahhoz a Marketplace-ajánlathoz az áthelyezett operációsrendszer-lemezről.

Megjegyzés

Ha a virtuálisgép-csomag már nem érhető el a Marketplace-en, nem használhatja ezt az eljárást.

#!/bin/bash
# Set variable values before proceeding. 

# Variables
sourceResourceGroup= Resource group for the current virtual machine
sourceSubscription= Subscription for the current virtual machine
vmName= Name of the current virtual machine

destinationResourceGroup= Resource group for the new virtual machine, create if necessary
destinationSubscription= Subscription for the new virtual machine

# Set your current subscription for the source virtual machine
az account set --subscription $sourceSubscription

# Load variables about your virtual machine
# osType = windows or linux
osType=$(az vm get-instance-view --resource-group $sourceResourceGroup \
    --name $vmName --subscription $sourceSubscription \
    --query 'storageProfile.osDisk.osType' --output tsv)

# offer = Your offer in Marketplace
offer=$(az vm get-instance-view --resource-group $sourceResourceGroup \
    --name $vmName --query 'storageProfile.imageReference.offer' --output tsv)

# plan = Your plan in Marketplace
plan=$(az vm get-instance-view --resource-group $sourceResourceGroup \
    --name $vmName --query 'plan' --output tsv)

# publisher = Your publisher in Marketplace
publisher=$(az vm get-instance-view --resource-group $sourceResourceGroup \
    --name $vmName --query 'storageProfile.imageReference.publisher' --output tsv)

# Get information to create new virtual machine
planName=$(az vm get-instance-view --resource-group $sourceResourceGroup \
    --subscription $sourceSubscription --query 'plan.name' --name $vmName)
planProduct=$(az vm get-instance-view --resource-group $sourceResourceGroup \
    --subscription $sourceSubscription --query 'plan.product' --name $vmName)
planPublisher=$(az vm get-instance-view --resource-group $sourceResourceGroup \
    --subscription $sourceSubscription --query 'plan.publisher' --name $vmName)

# Get the name of the OS disk
osDiskName=$(az vm show --resource-group $sourceResourceGroup --name $vmName \
    --query 'storageProfile.osDisk.name' --output tsv)

# Verify the terms for your market virtual machine
az vm image terms show --offer $offer --plan '$plan' --publisher $publisher \
    --subscription $sourceSubscription

# Deallocate the virtual machine
az vm deallocate --resource-group $sourceResourceGroup --name $vmName

# Create a snapshot of the OS disk
az snapshot create --resource-group $sourceResourceGroup --name MigrationSnapshot \
    --source "/subscriptions/$sourceSubscription/resourceGroups/$sourceResourceGroup/providers/Microsoft.Compute/disks/$osDiskName"

# Move the snapshot to your destination resource group
az resource move --destination-group $destinationResourceGroup \
    --destination-subscription-id $destinationSubscription \
    --ids "/subscriptions/$sourceSubscription/resourceGroups/$sourceResourceGroup/providers/Microsoft.Compute/snapshots/MigrationSnapshot"

# Set your subscription to the destination value
az account set --subscription $destinationSubscription

# Accept the terms from the Marketplace
az vm image terms accept --offer $offer --plan '$plan' --publisher $publisher \
    --subscription $destinationSubscription

# Create disk from the snapshot 
az disk create --resource-group $destinationResourceGroup --name DestinationDisk \
    --source "/subscriptions/$destinationSubscription/resourceGroups/$destinationResourceGroup/providers/Microsoft.Compute/snapshots/MigrationSnapshot" \
    --os-type $osType

# Create virtual machine from disk
az vm create --resource-group $destinationResourceGroup --name $vmName \
    --plan-name $planName --plan-product $planProduct  --plan-publisher $planPublisher \
    --attach-os-disk "/subscriptions/$destinationSubscription/resourceGroups/$destinationResourceGroup/providers/Microsoft.Compute/disks/DestinationDisk" \
    --os-type $osType

Az erőforrások eltávolítása

A minta futtatása után az alábbi parancsokkal távolítsa el az erőforráscsoportokat és az összes társított erőforrást:

az group delete --name $sourceResourceGroup --subscription $sourceSubscription
az group delete --name $destinationResourceGroup --subscription $destinationSubscription

A cikkben használt Azure CLI-hivatkozások

Következő lépések