Created
May 28, 2024 22:01
-
-
Save sajayantony/4eb6b7f451d3d79cbe16255815366012 to your computer and use it in GitHub Desktop.
Schedule VM Start using ACR Tasks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Variables | |
acrName= | |
resourceGroup= | |
vmName= | |
# Create ACR task with encoded YAML | |
az acr task create \ | |
--name startvm \ | |
--registry $acrName \ | |
--schedule "dailyTimer:0 12 * * Mon-Fri" \ | |
--assign-identity '[system]' \ | |
--context /dev/null \ | |
--file - <<EOF | |
version: v1.0.0 | |
steps: | |
- id: login | |
cmd: az login --identity | |
- id: start-vm | |
cmd: az vm start --resource-group $resourceGroup --name $vmName | |
EOF | |
# Assign managed identity to the task | |
taskIdentity=$(az acr task identity show --name startvm --registry $acrName --query principalId --output tsv) | |
subscription=$(az account show --query id -o tsv) | |
echo Role assigning $taskIdentity to vm | |
# Assign a role to the VM using the task's managed identity | |
az role assignment create --assignee $taskIdentity --role Contributor --scope /subscriptions/$subscription/resourceGroups/$resourceGroup/providers/Microsoft.Compute/virtualMachines/$vmName | |
echo "ACR task created and VM role assigned!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment