0
0
Azurecloud~20 mins

Scripting with variables and loops in Azure - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Azure Scripting Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
service_behavior
intermediate
2:00remaining
Azure CLI Loop Variable Behavior
Consider this Azure CLI script snippet that loops to create resource groups with variable names. What will be the name of the last resource group created?
Azure
for i in {1..3}; do az group create --name myResourceGroup$i --location eastus; done
AmyResourceGroup3
BmyResourceGroup1
CmyResourceGroup4
DmyResourceGroup0
Attempts:
2 left
💡 Hint
The loop runs from 1 to 3 inclusive, appending the loop variable to the name.
Configuration
intermediate
2:00remaining
Azure ARM Template Loop Variable Usage
In an Azure ARM template, you want to create 3 storage accounts with names 'storageacct1', 'storageacct2', and 'storageacct3' using a loop. Which of the following 'copy' loop configurations correctly sets the storage account names?
A"copy": { "name": "storageLoop", "count": 3 }, "name": "storageacct${copyIndex()}"
B"copy": { "name": "storageLoop", "count": 3 }, "name": "storageacct[copyIndex()+1]"
C"copy": { "name": "storageLoop", "count": 3 }, "name": "storageacct[copyIndex()]"
D"copy": { "name": "storageLoop", "count": 3 }, "name": "storageacct${copyIndex()+1}"
Attempts:
2 left
💡 Hint
copyIndex() starts at 0, so add 1 to start names at 1.
Architecture
advanced
2:00remaining
Automating VM Creation with Azure CLI Loops
You want to create 5 Azure virtual machines named vm1 to vm5 in a loop using Azure CLI. Which script snippet correctly uses a loop and variable to create these VMs with the name pattern?
Afor i in {1..5}; do az vm create --resource-group myRG --name vm$i --image UbuntuLTS --admin-username azureuser; done
Bfor i in $(seq 1 5); do az vm create --resource-group myRG --name vm$i --image UbuntuLTS --admin-username azureuser; done
Cfor i in 1 2 3 4 5; do az vm create --resource-group myRG --name vm$i --image UbuntuLTS --admin-username azureuser; done
Dfor i in 5 4 3 2 1; do az vm create --resource-group myRG --name vm$i --image UbuntuLTS --admin-username azureuser; done
Attempts:
2 left
💡 Hint
Use brace expansion {1..5} for a simple loop from 1 to 5.
security
advanced
2:00remaining
Looping Over Secrets in Azure Key Vault
You have a script that loops over a list of secret names to retrieve their values from Azure Key Vault. Which approach correctly uses variables and loops to fetch and print each secret's value securely?
Afor secret in secret1 secret2 secret3; do az keyvault secret show --vault-name myVault --name $secret --query value -o table; done
Bfor secret in secret1 secret2 secret3; do az keyvault secret show --vault-name myVault --name "$secret" --query value -o json; done
Cfor secret in secret1 secret2 secret3; do az keyvault secret show --vault-name myVault --name $secret --query value -o tsv; done
Dfor secret in secret1 secret2 secret3; do az keyvault secret show --vault-name myVault --name $secret --query value -o json; done
Attempts:
2 left
💡 Hint
Use -o tsv to get raw secret value without extra formatting.
Best Practice
expert
3:00remaining
Efficient Variable Use in Azure CLI Loops for Resource Tagging
You want to tag multiple Azure resources in a loop with a variable tag value that changes each iteration. Which script snippet correctly updates the tag value variable inside the loop and applies it to each resource?
AtagValue="env"; for i in {1..3}; do az resource tag --tags environment=$tagValue$i --ids /subscriptions/xxx/resourceGroups/myRG/providers/Microsoft.Compute/virtualMachines/vm$i; done
Bfor i in {1..3}; do az resource tag --tags environment=env$i --ids /subscriptions/xxx/resourceGroups/myRG/providers/Microsoft.Compute/virtualMachines/vm$i; done
Cfor i in {1..3}; do tagValue=env$i; az resource tag --tags environment=$tagValue --ids /subscriptions/xxx/resourceGroups/myRG/providers/Microsoft.Compute/virtualMachines/vm$i; done
Dfor i in {1..3}; do tagValue="env$i"; az resource tag --tags environment=$tagValue --ids /subscriptions/xxx/resourceGroups/myRG/providers/Microsoft.Compute/virtualMachines/vm$i; done
Attempts:
2 left
💡 Hint
Assign the variable inside the loop with quotes to handle the string correctly.