Azure CLI list Disk - azure

I want to write a bash shell script which will take VMname and Resource Group name as argument and take a snapshot backup of given VM.
Please guide me the command which can list all the disks of particular VM
I dont know Azure CLI to list disks of particular VM

It seems there is no CLI command to list disks associated with a particular VM directly. But Azure VM just have two disk types, so you can list all disks Id associated with the VM through two CLI commands with the two types:
Data disk:
az vm show -d -g groupName -n vmName --query "storageProfile.dataDisks[].managedDisk.id"
Os disk:
az vm show -d -g groupName -n vmName --query "storageProfile.osDisk.managedDisk.id"
And if you want to create a snapshot from the VM, you can create it from the disks one by one through the CLI command az snapshot create.
Hope this will help you, any more question please give me the message.

Related

creating vm from snapshot when the snapshot is in another resource group

I have created vm from snapshot using azure cli command
Below is the script for the creation of vm from snapshot
az disk create -g $RD_OPTION_RESOURCEGROUP -n $RD_OPTION_DISKNAME --source $RD_OPTION_SNAPSHOTNAME
az vm create -g $RD_OPTION_RESOURCEGROUP -n $RD_OPTION_VMNAME --attach-os-disk $RD_OPTION_DISKNAME --os-type windows
This code will create the vm from snapshot in which snapshot is in one resource group say abc then the newly creating vm should also in same resource group abc
now i need to find the way like i want to create a vm from snapshot like the snapshot and newly creating vm will be in different resource groups... like if snapshot is in abc resource group then the newly creating vm have to be allocated in the other resource group say xyz
Please help me out in this
To create a VM from Snapshot which is in different resource group, copy the snapshot using below CLI script:
Define a variable as sourceSubscriptionId where you have to provide the subscription ID in which your snapshot resides.
sourceSubscriptionId="<subscriptionId>"
Define a variable as sourceResourceGroupName where you have to provide the resource group name in which your snapshot resides.
sourceResourceGroupName=yourSourceResourceGroupName
Define a variable where you have to provide snapshot name.
snapshotName=mySnapshotName
Set the context to the subscription Id where snapshot exists.
az account set --subscription $sourceSubscriptionId
Use the below cmdlet to get the snapshot ID.
snapshotId=$(az snapshot show --name $snapshotName --resource-group $sourceResourceGroupName --query [id] -o tsv)
Define a variable targetResourceGroupName where snapshot will be copied to:
targetResourceGroupName=mytargetResourceGroupName
To copy the snapshot to different resource group try using the below cmdlet:
az snapshot create --resource-group $targetResourceGroupName --name $snapshotName --source $snapshotId --sku Standard_LRS
Reference : azure-cli-samples/copy-snapshot-to-same-or-different-subscription.sh at master · Azure-Samples/azure-cli-samples · GitHub.

az vmss list command returns empty value

I have two resource groups in Azure, each contains one VMSS (Virtual Machine Scale Sets) and the resources are visible in Azure Portal. The problem is, the following commands return empty output:
az resource list --subscription MySubscription -g vmss-rg
az vmss list --subscription MySubscription -g vmss-rg
az vmss list
However, running az resource list with different resource groups which doesn't have VMSS works fine. I also tested it with different Azure account and subscription, it also worked.
We have tested the same command in our local as suggested by #VenkateshDodda-MT and it works fine. Posting it as an answer to help other community members for the similar issue so that they can find and fix their problem.
To achieve the above requirement we need to install the az powershell module in our local .
Post that run the command in powershell to get the vmss list under our subscription or resource groups:-
az resource list --resource-type "Microsoft.Compute/virtualMachineScaleSets"
OUTPUT:-
To get the VMSS list under particular resource group run the below cmd:
az resource list --resource-type "Microsoft.Compute/virtualMachineScaleSets" -g '<rgName>'
OUTPUT:-
For more information please refer this SO THREAD:Azure PS command returns empty list as suggested by #Olga Osinskaya - MSFT

Azure CLI - How to update cache setting of a live managed data disk attached to an Azure VM

Need some help figuring out the Azure CLI command and parameters to update cache setting of a live managed data disk attached to an Azure VM.
A managed data disk is created and attached to an Azure VM outside of my control. By default, Host caching is set to Read-Only. For some performance reasons, I would like to set Host caching to None for the attached data disk.
What did I try?
Tried to use below command
az vm update --subscription my-subscription --name my-vm-name --resource-group my-rgroup-name --disk-caching os=ReadWrite
This command updates the OS Disks Host cache setting. However, could not update managed disks setting. I tried different keys like managed, data-disks, attached, etc... instead of os in os=ReadWrite; Nothing worked, command throws ValueError
ValueError: invalid literal for int() with base 10: 'data-disks'
Explored another command - az disk update. However, it does not have option to update managed disk cache setting.
Tried to detach and re-attach the managed disk with --caching set to None. It works. However, this is not permitted in my case.
az vm disk detach --subscription my-subscription -g my-rgroup-name --vm-name my-vm-name --name managed-disk-name
az vm disk attach --subscription my-subscription -g my-rgroup-name --vm-name my-vm-name --name managed-disk-name --caching none
Need this for automation. So changing this setting from Azure portal UI is not an option.
Suggestions are much appreciated.
Without detaching the disk from virtual Machine using the below cmdlet to change the managed disk caching from ReadWrite\ReadOnly to None.
We have tested this cmdlet in our environment by creating a VirtualMachine & attached a Data disk to it and it is working fine.
Here is the Cmdlet:
az vm update --resource-group <resource-group-name> --name <vmName> --disk-caching os=ReadWrite 0(LunNumberOfTheDisk)=None
Here is the sample output screenshot for reference :
Below screenshot show the current state of disk caching before executing the above cmdlet.
In the below screenshot , you can see we have executed the cmdlet without stopping or detaching the disk from the virtual machine and the operation got succeeded without any interruption.

How can I find out what image was used to build an AzureVM?

When building a new VM on Azure, you have many different OS options available to choose from. How can I find out which option was used for an existing VM?
You can use the Azure CLI command to get the image information that used in the existing VM:
az vm show -g resourceGroupName -n vmName -d --query storageProfile.imageReference
Or get the available images for VM/VMSS in Azure with the command az vm image list.
Hope this will help you. If you need more help please give me the message.

Stop all compute in AKS (Azure Managed Kubernetes)

I have created a managed Kubernetes cluster in Azure, but it's only for learning purposes and so I only want to pay for the compute whilst I'm actually using it.
Is there a easy way to gracefully shut down and start up the VMs, availablity sets and load balancers?
You could use the Azure CLI to stop the the entire cluster:
az aks stop --name myAksCluster --resource-group myResourceGroup
And start it again with
az aks start --name myAksCluster --resource-group myResourceGroup
Before this feature, it was possible to stop the virtual machines via Powershell:
az vm deallocate --ids $(az vm list -g MC_my_resourcegroup_westeurope --query "[].id" -o tsv)
Replace MC_my_resourcegroup_westeurope with the name of your resource group that contains the VM(s).
When you want to start the VM(s) again, run:
az vm start --ids $(az vm list -g MC_my_resourcegroup_westeurope --query "[].id" -o tsv)
Only VMs cost money out of all AKS resources (well, VHDs as well, but you cannot really stop those). So you only need to take care of those. Edit: Public Ips also cost money, but you cannot stop those either.
For my AKS cluster I just use portal and issue stop\deallocate command. And start those back when I need them (everything seems to be working fine).
You can use REST API\powershell\cli\various SKDs to achieve the same result in an automated fashion.
Above method (az vm <deallocate|start> --ids $(...)) no longer seems to work.
Solved by first listing the VM scale sets and use these to deallocate/start:
$ResourceGroup = "MyResourceGroup"
$ClusterName = "MyAKSCluster"
$Location = "westeurope"
$vmssResourceGroup="MC_${ResourceGroup}_${ClusterName}_${Location}"
# List all VM scale sets
$vmssNames=(az vmss list --resource-group $vmssResourceGroup --query "[].id" -o tsv | Split-Path -Leaf)
# Deallocate first instance for each VM scale set
$vmssNames | ForEach-Object { az vmss deallocate --resource-group $vmssResourceGroup --name $_ --instance-ids 0}
# Start first instance for each VM scale set
$vmssNames | ForEach-Object { az vmss start --resource-group $vmssResourceGroup --name $_ --instance-ids 0}
There is a new feature just added to AKS:
The AKS Stop/Start cluster feature now in public preview allows AKS
customers to completely pause an AKS cluster and pick up where they
left off later with a switch of a button, saving time and cost.
Previously, a customer had to take multiple steps to stop or start a
cluster, adding to operations time and wasting compute resources. The
stop/start feature keeps cluster configurations in place and customers
can pick up where they left off without reconfiguring the clusters.
https://learn.microsoft.com/en-gb/azure/aks/start-stop-cluster
In your AKS cluster, goto properties and find your Resource group name. search for the Resource group and when you select it, it will list your virtual machines. For each Virtual Machine, select the Operations > Auto-Shutdown option and turn it on. This will turn the VM off saving you money when you aren't developing! To turn them back on again, you will need to follow the advice on previous answers or the answer here

Resources