#!/bin/bash
# Prompt user for Azure login
az login
# Prompt user for resource group details
read -p "Enter the desired resource group name: " resourceGroupName
read -p "Enter the Azure region (e.g., eastus): " location
# Create the resource group
az group create --name $resourceGroupName --location $location
# Create a storage account (you can customize the storage account settings as needed)
storageAccountName="${resourceGroupName}storage$(date +%s)"
az storage account create \
--resource-group $resourceGroupName \
--name $storageAccountName \
--sku Standard_LRS \
--encryption-services blob
# Get the storage account key
accountKey=$(az storage account keys list --resource-group $resourceGroupName --account-name $storageAccountName --query "[0].value" -o tsv)
# Create 5 Ubuntu virtual machines
for i in {1..5}; do
read -p "Enter the administrator username for ubuntuVM$i: " adminUsername
read -sp "Enter the administrator password for ubuntuVM$i: " adminPassword
echo # Move to a new line after password input
# Create the virtual machine
az vm create \
--resource-group $resourceGroupName \
--name ubuntuVM$i \
--image Canonical:UbuntuServer:18.04-LTS:latest \
--admin-username $adminUsername \
--admin-password $adminPassword \
--size Standard_DS1_v2 \
--public-ip-address ""
# Get the NSG associated with the virtual machine
nsgId=$(az vm show --resource-group $resourceGroupName --name ubuntuVM$i --query "networkProfile.networkInterfaces[0].id" -o tsv | xargs -I {} az network nic show --ids {} --query "ipConfigurations[0].networkSecurityGroup.id" -o tsv)
# Allow all inbound traffic
az network nsg rule create \
--resource-group $resourceGroupName \
--nsg-name $(basename $nsgId) \
--name AllowAllInbound \
--priority 100 \
--direction Inbound \
--access Allow \
--protocol '*' \
--source-address-prefix '*' \
--source-port-range '*' \
--destination-address-prefix '*' \
--destination-port-range '*'
# Attach a managed disk to the virtual machine
az vm disk attach \
--resource-group $resourceGroupName \
--vm-name ubuntuVM$i \
--disk $(az disk create --resource-group $resourceGroupName --name ubuntuVM${i}OSDisk --size-gb 30 --sku Standard_LRS --query "id" -o tsv)
# Add a data disk to the virtual machine
az vm disk attach \
--resource-group $resourceGroupName \
--vm-name ubuntuVM$i \
--disk $(az disk create --resource-group $resourceGroupName --name ubuntuVM${i}DataDisk --size-gb 100 --sku Standard_LRS --query "id" -o tsv) \
--lun 1
# Configure the virtual machine to mount the Azure Storage account
az vm extension set \
--resource-group $resourceGroupName \
--vm-name ubuntuVM$i \
--name customScript \
--publisher Microsoft.Azure.Extensions \
--version 2.1 \
--settings '{"script": "sudo apt-get update && sudo apt-get install -y cifs-utils && sudo mkdir /mnt/azureStorage && sudo mount -t cifs //'$storageAccountName'.blob.core.windows.net/containerName /mnt/azureStorage -o vers=3.0,username='$storageAccountName',password='$accountKey',dir_mode=0755,file_mode=0664,serverino"}' \
--protected-settings '{"script": ""}'
done
echo "Virtual machines created successfully with Azure Storage accounts!"