I am using Python to create a VM using a resource like the below in the template. Let's say adminUsername is "bob". I can use the same code to create a 1-off VM using a vanilla Ubuntu image where Bob is not a user. However, as seen in the template below, we use a custom image which already has an account for Bob. Even though we place a public key in the template keyData property (in the form "ssh-rsa aslflkasdfkladflkj..."). Again, this tactic works when using a vanilla Ubuntu image, but on our custom image, /home/bob/.ssh/authorized_keys is not being updated - the only keys in that file are the ones from the original base image. I expect the deployment to add a public key to that file so that I can ssh in using the new public key.
Furthermore, if I use the same deployment template by change adminUsername to "alice", the deployment fails to make the new Alice user account.
I am verifying all this by using the "Reset Password" feature on the Azure VM dashboard where I'll add yet another username "charlie". I can then ssh in just fine using the corresponding private key and then look at the authorized_keys file for bob (which exists, and only has entries from the original image, but no new entries), but not alice because her account didn't even get made by the deployment template.
My goal is to make a deployment using a template like the below and using the image in the imageReference paramater, and be able to SSH in as Bob using the the private key associated with the public key provided in keyData parameter. How can I do this?
{
"apiVersion": "2021-03-01",
"dependsOn": [
"[concat(variables('vmNameWorker'), 0)]",
"[variables('nicNameMaster')]"
],
"location": "[resourceGroup().location]",
"name": "[variables('vmNameMaster')]",
"properties": {
"hardwareProfile": {
"vmSize": "[parameters('vmMasterSize')]"
},
"networkProfile": {
"networkInterfaces": [
{
"id": "[resourceId('Microsoft.Network/networkInterfaces', variables('nicNameMaster'))]"
}
]
},
"osProfile": {
"adminUsername": "[parameters('adminUsername')]",
"computerName": "[variables('vmNameMaster')]",
"linuxConfiguration": {
"ssh": {
"publicKeys": [
{
"keyData": "[parameters('sshKeyData')]",
"path": "[concat('/home/', parameters('adminUsername'), '/.ssh/authorized_keys')]"
}
]
}
}
},
"storageProfile": {
"imageReference": {
"id": "[parameters('imageName')]"
}
}
},
"resources": [
{
"apiVersion": "2016-03-30",
"type": "extensions",
"name": "Installation",
"location": "[resourceGroup().location]",
"dependsOn": [
"[variables('vmNameMaster')]"
],
"properties": {
"publisher": "Microsoft.Azure.Extensions",
"type": "CustomScript",
"typeHandlerVersion": "2.0",
"autoUpgradeMinorVersion": false,
"settings": {
"fileUris": [
"[concat(parameters('_artifactsLocation'), parameters('_artifactsLocationSasToken'))]"
]
},
"protectedSettings": {
"commandToExecute": "[variables('installationCLI')]"
}
}
}
],
"type": "Microsoft.Compute/virtualMachines"
},