moulip
moulip

Reputation: 151

How to make user chose between ssh password or public key in Azure ARM template

I have built my Linux template everything is working fine but I have a remaining issue. Actually I want to let my users chose between using a password or an SSH public key to authenticate against the VM. I have created the 2 parameters, password and key and I want to make one mandatory if the other is blank, and set the resources part accordingly.

How can I achieve that ?

Upvotes: 0

Views: 581

Answers (1)

4c74356b41
4c74356b41

Reputation: 72181

You can use this snippet (add these variables\parameters to your template):

"parameters": {
    "authType": {
        "type": "string",
        "defaultValue": "password",
        "allowedValues": [
            "password",
            "ssh"
        ]
    }
},
"variables": {
    "ssh": {
        "computerName": "[variables('vmName')]",
        "adminUsername": "[parameters('adminUsername')]",
        "linuxConfiguration": {
            "disablePasswordAuthentication": true,
            "ssh": {
                "publicKeys": [
                    {
                        "path": "[concat('/home/',parameters('adminUsername'),'/.ssh/authorized_keys')]",
                        "keyData": "[parameters('sshPublicKey')]"
                    }
                ]
            }
        }
    },
    "password": {
        "computerName": "[variables('vmName')]",
        "adminUsername": "[parameters('adminUsername')]",
        "adminPassword": "[parameters('adminPassword')]"
    }
},

and then in your VM definition do this:

"osProfile": "[variables(parameters('authType'))]"

which will retrieve either variable called ssh or variable called password and assign that to osProfile

Upvotes: 1

Related Questions