链接的ARM模板导致模板无效

时间:2018-07-05 05:41:39

标签: azure azure-devops azure-keyvault arm-template

让我们从我要完成的事情开始。

我想做的是创建一个ARM模板,在其中我从Azure Key Vault检索机密,而无需在特定的Key Vault上指定太多细节。听起来很容易,并且可能在每个生产系统中都会实现。

进行快速搜索后,我发现了the syntax for such a thing is the following

"parameters": {
  "adminPassword": {
    "reference": {
      "keyVault": {
        "id": "[resourceId(subscription().subscriptionId,  parameters('vaultResourceGroup'), 'Microsoft.KeyVault/vaults', parameters('vaultName'))]"
      },
      "secretName": "[parameters('secretName')]"
    }
  },

根据我的收集,您需要将其添加到外部模板中,因为不能在'main'方法中使用所使用的方法。

因此,我开始创建一个“主” ARM模板和一个名为appservice.json的新模板,其中包含我的App Service所需的所有内容,包括appSettings块,这些块需要Key Vault的机密

在主模板中,我完成了以下操作,as described in the documentation

{
      "apiVersion": "2017-05-10",
      "name": "linkedTemplate",
      "type": "Microsoft.Resources/deployments",
      "properties": {
        "mode": "Incremental",
        "templateLink": {
          "uri": "[uri(deployment().properties.templateLink.uri, 'appservice.json')]",
          "contentVersion": "1.0.0.0"
        },

但是,在部署时遇到以下错误。

"error": {
    "code": "InvalidTemplate",
    "message": "Unable to process template language expressions for resource '/subscriptions/ba49bae7-2b37-4504-914b-441763a2bcd3/resourceGroups/cfpexchange-jan-test/providers/Microsoft.Resources/deployments/linkedTemplate' at line '1' and column '1526'. 'The language expression property 'templateLink' doesn't exist, available properties are 'name, properties'.'"
  }

我还尝试了以下操作,因为我注意到Visual Studio中的IntelliSense告诉我properties不会退出,我应该直接使用templateLink

{
      "apiVersion": "2017-05-10",
      "name": "linkedTemplate",
      "type": "Microsoft.Resources/deployments",
      "properties": {
        "mode": "Incremental",
        "templateLink": {
          "uri": "[uri(deployment().templateLink.uri, 'appservice.json')]",
          "contentVersion": "1.0.0.0"
        },

这当然也不对。

   "error": {
    "code": "InvalidTemplate",
    "message": "Unable to process template language expressions for resource '/subscriptions/ba49bae7-2b37-4504-914b-441763a2bcd3/resourceGroups/cfpexchange-jan-test/providers/Microsoft.Resources/deployments/linkedTemplate' at line '1' and column '1526'. 'The language expression property 'templateLink' doesn't exist, available properties are 'name, properties'.'"
  }

并将其用作变量时,如文档中所述

"variables": {
    "sharedTemplateUrl": "[uri(deployment().properties.templateLink.uri, 'shared-resources.json')]"
  },
...
"properties": {
        "mode": "Incremental",
        "templateLink": {
          "uri": "[variables('sharedTemplateUrl')]",
          "contentVersion": "1.0.0.0"
        },

我也遇到错误。

  

2018-07-04T19:14:34.4204720Z ## [错误]部署模板验证失败:“模板变量'sharedTemplateUrl'无效:语言表达属性'templateLink'不存在,可用属性为'模板,参数,模式,debugSetting,provisioningState”。请参见https://aka.ms/arm-template-expressions以了解用法详细信息。”

这时候我有点迷路了。据我从文档中了解到的,看来我在做所有正确的事情。显然我不是。有关如何继续进行操作的任何想法?

为完整起见,我目前正在使用两个实际文件:

为了尝试对其进行修复,已经对其进行了多次迭代,但是如上所述,到目前为止,它似乎仍在工作。

1 个答案:

答案 0 :(得分:1)

首先,this是如何在嵌套模板中使用KV的方法。管理员密码示例:

"adminPassword": {
    "reference": {
        "keyVault": {
            "id": "kv_resource_id"
        },
        "secretName": "[concat('secret', copyindex(1))]"
    }
},

本节在您调用它时应该位于嵌套模板参数中(只需查看示例链接)。

您的错误似乎在变量中。因此,如果您使用本地文件部署主模板,则从URL部署主模板时,templateLink属性才可用。

enter image description here

将此与远程模板执行进行比较:

New-AzureRmResourceGroupDeployment -ResourceGroupName xxx -TemplateUri 'https://paste.ee/d/XI1Rc/0'

由于这是一个远程URL,它应该向您显示相同的输出,但是这次具有templateLink属性。

Name             Type                       Value
===============  =========================  ==========
test             Object                     {

"name": "theDeploymentName",
"properties": {
  "templateLink": {
    "uri": "theRemoteTemplateUri"
  },
  "template": {
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {},
    "variables": {},
    "resources": [],
    "outputs": {
      "test": {
        "type": "Object",
        "value": "[deployment()]"
      }
    }
  },
  "parameters": {},
  "mode": "Incremental",
  "provisioningState": "Accepted"
}
}