Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set environment variables in Azure ARM templates

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?

like image 872
Pablo Jomer Avatar asked Dec 07 '16 11:12

Pablo Jomer


2 Answers

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')]"
        }
    }
}
like image 61
DanielLarsenNZ Avatar answered Nov 02 '22 04:11

DanielLarsenNZ


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.

like image 21
4c74356b41 Avatar answered Nov 02 '22 03:11

4c74356b41