Add SSH Key to VMSS

Run Command:

azĀ vmssĀ run-commandĀ invokeĀ --resource-groupĀ {VMSS_RESOURCE_GROUP}Ā --nameĀ {VMSS_NAME}Ā --command-idĀ RunShellScriptĀ --instance-idĀ {VMSS_ID}Ā --scriptsĀ "echoĀ '{PUBLIC_SSH_KEY}'Ā >>Ā /home/{USERNAME}/.ssh/authorized_keys"

Run Script for multiple instances:

instances=$(az vmss nic list -g {RESOURCE_GROUP_NAME} --vmss-name {VMSS_NAME} --query "[].ipConfigurations[].[privateIpAddress,id]" --output tsv | sed -e 's/^\([0-9\.]*\).*\/\([0-9]*\)\/.*$/\2/g')
for i in $instances; do
  echo "Working on $i"
  az vmss run-command invoke --resource-group {RESOURCE_GROUP_NAME} --name {VMSS_NAME} --command-id RunShellScript --instance-id $i --scripts "echo '$(cat /home/{USERNAME}/.ssh/id_rsa.pub)' >> /home/{USERNAME}/.ssh/authorized_keys"
done

Last updated