0
0
Azurecloud~5 mins

Scripting with variables and loops in Azure - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes you want to run the same commands many times with small changes. Using variables and loops in scripts helps you do this easily and saves time.
When you need to create multiple Azure resources with similar settings.
When you want to update several virtual machines one by one.
When you want to automate repetitive tasks like starting or stopping services.
When you want to test different configurations quickly.
When you want to clean up many resources without typing each name.
Commands
Create a resource group named 'myResourceGroup' in the East US region to hold your Azure resources.
Terminal
az group create --name myResourceGroup --location eastus
Expected OutputExpected
{ "id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/myResourceGroup", "location": "eastus", "managedBy": null, "name": "myResourceGroup", "properties": { "provisioningState": "Succeeded" }, "tags": {}, "type": "Microsoft.Resources/resourceGroups" }
--name - Sets the name of the resource group.
--location - Sets the Azure region for the resource group.
This loop creates three virtual machines named vm1, vm2, and vm3 in the resource group. It uses a variable to change the VM name each time.
Terminal
for vmName in vm1 vm2 vm3; do az vm create --resource-group myResourceGroup --name $vmName --image UbuntuLTS --admin-username azureuser --generate-ssh-keys; done
Expected OutputExpected
- Running ... { "fqdns": "", "id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachines/vm1", "location": "eastus", "name": "vm1", "powerState": "VM running", "resourceGroup": "myResourceGroup", "zones": "" } - Running ... { "fqdns": "", "id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachines/vm2", "location": "eastus", "name": "vm2", "powerState": "VM running", "resourceGroup": "myResourceGroup", "zones": "" } - Running ... { "fqdns": "", "id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachines/vm3", "location": "eastus", "name": "vm3", "powerState": "VM running", "resourceGroup": "myResourceGroup", "zones": "" }
--resource-group - Specifies the resource group where the VM will be created.
--name - Sets the name of the virtual machine.
--image - Specifies the OS image for the VM.
This loop stops each virtual machine named vm1, vm2, and vm3 in the resource group using the variable vmName.
Terminal
for vmName in vm1 vm2 vm3; do az vm stop --resource-group myResourceGroup --name $vmName; done
Expected OutputExpected
- Stopping vm1 ... { "status": "Succeeded" } - Stopping vm2 ... { "status": "Succeeded" } - Stopping vm3 ... { "status": "Succeeded" }
--resource-group - Specifies the resource group of the VM.
--name - Specifies the VM to stop.
Key Concept

Using variables and loops in scripts lets you repeat commands with small changes automatically, saving time and avoiding mistakes.

Common Mistakes
Not using the $ sign before the variable name inside the loop.
The shell treats the variable name as plain text, so the command uses the literal string instead of the variable value.
Always use $variableName to access the value stored in the variable.
Forgetting to separate items in the loop with spaces.
The loop does not recognize the items correctly and may run zero or one iteration.
Separate each item with a space inside the loop declaration.
Running loops without creating the resource group first.
Commands fail because the target resource group does not exist.
Always create the resource group before creating resources inside it.
Summary
Create a resource group first to hold your Azure resources.
Use a loop with variables to run the same command multiple times with different values.
Use the $ sign to access variable values inside the loop commands.