从CLI将URL作为参数传递给ARM模板

时间:2020-01-09 07:35:45

标签: azure azure-resource-manager azure-cli

我正在尝试使用链接的ARM模板部署Azure资源,为此,我将参数文件和模板文件放在Blob存储上。从CLI执行azure命令时,我需要将参数文件和Blob存储的链接作为参数传递。下面是我的示例masterazuredeploy.json

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {},
  "variables": {
    "templateBaseUrl": "[parameters('templateBaseUrl')]",
    "parameterBaseUrl": "[parameters('parameterBaseUrl')]",
    "keyVaultDeployTemplateUrl": "[uri(variables('templateBaseUrl'), 'keyvaultdeploy.json')]"
  },
  "resources": [
    {
      "apiVersion": "[variables('apiVersionResourceDeployment')]",
      "name": "keyVaultDeployment",
      "type": "Microsoft.Resources/deployments",
      "properties": {
        "mode": "Incremental",
        "templateLink": {
          "uri": "[variables('keyVaultDeployTemplateUrl')]"
        },
        "parametersLink": {
          "uri": "[variables('keyVaultparameterFileUrl')]"
        }
      }
    }

  ]
}

要执行此操作,我给出以下CLI命令:

az group deployment create --resource-group abc-devops-test --template-file .\masterazuredeploy.json  --parameters templateBaseUrl="https://test.blob.core
.windows.net/azurestackautomationtest/resourcetemplates/"  parameterBaseUrl="https://test.blob.core.windows.net/azurestackautomationtest/parameters/dev/" --verbose

执行时出现以下错误:

unrecognized template parameter 'templateBaseUrl'. Allowed parameters:
command ran in 1.918 seconds.

我尝试了不带反引号和单引号的参数值。还是行不通。我到底在哪里失踪。

也尝试了另一种方法,将两个参数都放在global.parameters.json中,如下所示,

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "templateBaseUrl": {
      "value": "https://test.blob.core.windows.net/azurestackautomation/resourcetemplates/"
    },
    "parameterBaseUrl": {
      "value": "https://test.blob.core.windows.net/azurestackautomation/parameters/dev/"
    }
  }
}

将此文件上传到blob存储,并以blob存储的路径作为参数

az group deployment create --resource-group abc-devops-test --template-file .\masterazuredeploy.json  --parameters https://test.blob.core.windows.net/azur
estackautomationtest/parameters/dev/global.parameters.json  --verbose

但是出现以下错误:

400 Client Error: Bad Request for url: https://management.azure.com/subscriptions/XXXX-xx-x-x-x--x-x/resourcegroups/abc-devops-test/providers/Microsoft.Resources/deployments/masterazuredeploy?api-version=2018-05-01
command ran in 5.646 seconds.

1 个答案:

答案 0 :(得分:2)

正如我在模板中看到的那样,您错过了设置参数的步骤,无论您同时提供了两个CLI命令,您所做的就是将参数值输入模板中的参数。但是您没有设置参数,因此没有参数可以使用。我建议您将模板更改为以下内容:

    {
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "templateBaseUrl": {
            "type": "string"
        },
        "parameterBaseUrl": {
            "type": "string"
        }
    },
    "variables": {
      "keyVaultDeployTemplateUrl": "[uri(parameters('templateBaseUrl'), 'keyvaultdeploy.json')]"
    },
    "resources": [
      {
        "apiVersion": "[variables('apiVersionResourceDeployment')]",    #1
        "name": "keyVaultDeployment",
        "type": "Microsoft.Resources/deployments",
        "properties": {
          "mode": "Incremental",
          "templateLink": {
            "uri": "[variables('keyVaultDeployTemplateUrl')]"
          },
          "parametersLink": {
            "uri": "[variables('keyVaultparameterFileUrl')]"     #2
          }
        }
      }

    ]
  }

您还会错过设置变量apiVersionResourceDeploymentkeyVaultparameterFileUrl的时间。您可以根据需要使用参数和变量。