首页 > 解决方案 > 如何在 ARM 模板中将参数与字符串值连接:无法解析模板语言表达式

问题描述

我需要连接 ARM 模板(JSON 文件)中的参数,但"/subscriptions/@{encodeURIComponent([parameters('id')])}/providers". 它看起来[parameters('id')]不被识别为参数,它只是作为文本传递。

我知道可以使用union表达式,但在我的情况下,我不需要只合并两个单独的参数(根据我的理解,这就是这样union做的)。我需要构造一个字符串。

我想到的另一个选择是在 JSON 文件之外进行,以便我可以将其"/subscriptions/@{encodeURIComponent([parameters('id')])}/providers"作为单个参数传递。但我发现这种方法无效,因为在我的 ARM 模板中,我有 10 个类似的变量,我只需要在其中“粘贴” id.

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "id": {
            "type": "string"
        }
    },
    "variables": {},
    "resources": [{
        ...
        "path": "/subscriptions/@{encodeURIComponent([parameters('id')])}/providers",
        ...
    }]
}

标签: jsonazureazure-devopsarm-template

解决方案


在 ARM 模板中,您可以使用concaturiComponent函数来连接参数:

"[concat('/subscriptions/', uriComponent(parameters('id')), '/providers')]"

在您的示例中,它将类似于:

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "id": {
            "type": "string"
        }
    },
    "variables": {},
    "resources": [{
        ...
        "path": "[concat('/subscriptions/', uriComponent(parameters('id')), '/providers')]",
        ...
    }]
}

请参阅concaturiComponent的文档。


推荐阅读