Skip to content

Instantly share code, notes, and snippets.

@staticgc
Created December 1, 2022 17:31
Show Gist options
  • Save staticgc/c6c6627a14b66d1a2e0426f8a2c1ba56 to your computer and use it in GitHub Desktop.
Save staticgc/c6c6627a14b66d1a2e0426f8a2c1ba56 to your computer and use it in GitHub Desktop.
Update ssh key to azure vm scale set
if [ $# -lt 2 ]; then
echo "$0: <resource group> <pub key file>"
echo "Optional: you can generate ssh key pair using:
'az sshkey create --name <key name> --resource-group <resource group name>'"
exit 1
fi
rg=$1
pubkey=$2
echo "Note: you will need to authenticate with using 'az login' and you should have 'Virtual machine administrator'
role to your user for the resource group"
scalesets=$(az resource list -g $rg | jq -r -c '.[] | select( .type | contains("Microsoft.Compute/virtualMachineScaleSets")) | .name')
for vmss in $scalesets
do
echo ">> updating key for scaleset: $vmss"
az vmss extension set \
--vmss-name $vmss \
-g $rg \
-n VMAccessForLinux \
--publisher Microsoft.OSTCExtensions \
--version 1.4 \
--protected-settings "{\"username\":\"azureuser\", \"ssh_key\":\"$(cat $pubkey)\"}"
az vmss update-instances --instance-ids '*' \
-n $vmss \
-g $rg
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment