5
votes

I want to set the environment on my deployments in the ARM-template to guarantee the environment is the same across machines. Is there a way to set environment variables for a virtual machine created with an ARM template?

2

2 Answers

11
votes

Windows

You can use a Custom Script Extension to invoke SETX at deployment time. Add a nested resource to the resources array of your VM resource. This example invokes SETX MyEnvironmentPrefix (environmentPrefix-parameter-value) /m on the target machine:

{
    "apiVersion": "2017-12-01",
    "type": "extensions",
    "name": "SetEnvironmentVar",
    "comments": "Sets the MyEnvironmentPrefix system env var",
    "location": "[resourceGroup().location]",
    "dependsOn": [
        "[resourceId('Microsoft.Compute/virtualMachines', parameters('vmName'))]"
    ],
    "properties": {
        "publisher": "Microsoft.Compute",
        "type": "CustomScriptExtension",
        "typeHandlerVersion": "1.9",
        "autoUpgradeMinorVersion": true,
        "settings": {
            "commandToExecute": "[concat('SETX MyEnvironmentPrefix ', parameters('environmentPrefix'), ' /m')]"
        }
    }
}
2
votes

I don't think there is a direct way to do that (looking at the schema), but you could always implement something custom, Script extension or DSC extension.