azure-arm 模板中的环境变量

Environment variables in azure-arm templates

我正在尝试使用 azure 模板和打包器在 azure 上自动部署。

我已经构建了我的加壳器 ami(?),其中包含应用程序(java fat jar)。

现在,我的应用程序连接到 azure evnet hub,它与我的虚拟机同时部署并在相同的 json 模板中声明。

我想将连接属性作为我的 arm 模板中的环境变量传递给 vm。天蓝色模板有可能吗?我在 SO 上发现了一个类似的问题,半年前问过,没有答案。但也许从那以后发生了一些变化?人们如何在 Azure 上解决此类问题?遗憾的是,Terraform 不是一个选项,它不涵盖 Azure 基础设施的其他部分。

不,您不能直接设置环境。使用 ARM 模板将变量添加到 VM,但您可以使用 ARM 模板部署脚本扩展,它会为您执行此操作。

I would like to pass connection properties to vm as environment variables in my arm template. Is it possible with azure templates?

正如4c74356b41所说,模板不直接支持这个。根据你的情况,我建议你可以使用 Custom Script Extension.

自定义脚本扩展在 Azure 虚拟机上下载并执行脚本。此扩展对于 post 部署配置、软件安装或任何其他配置/管理任务很有用。

Azure 自定义脚本扩展支持 Linux 和 Windows,您可以编写一个脚本来配置虚拟机的连接属性并使用扩展在您的虚拟机上执行。自定义脚本扩展模板示例如下:

 "resources": [
                {
                    "type": "extensions",
                    "name": "CustomScriptExtension",
                    "apiVersion": "2015-06-15",
                    "location": "[resourceGroup().location]",
                    "dependsOn": [
                        "[variables('vmName')]"
                    ],
                    "properties": {
                        "publisher": "Microsoft.Compute",
                        "type": "CustomScriptExtension",
                        "typeHandlerVersion": "1.8",
                        "autoUpgradeMinorVersion": true,
                        "settings": {
                            "fileUris": [
                                "[concat(parameters('_artifactsLocation'), '/', variables('ScriptFolder'), '/', variables('ScriptFileName'), parameters('_artifactsLocationSasToken'))]"
                            ],
                            "commandToExecute": "[concat('powershell -ExecutionPolicy Unrestricted -File ', variables('scriptFolder'), '/', variables('scriptFileName'), ' ', variables('scriptParameters'))]"
                        }
                    }
                }
            ]

他们可以在 Git Hub.

上找到很多关于此扩展的示例