azure - 部署失败后获取已解析的 ARM 模板

标签 azure azure-devops azure-resource-manager azure-rm-template

是否可以在 Azure 门户中获取运行时时的 ARM 模板,并解析变量和参数?

示例如下:

AzureDeploy.json

{
 "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
 "contentVersion": "1.0.0.0",
 "parameters": {
   "environment": {
     "type": "string",
     "defaultValue": "dev",
   },
   "storageSKU": {
     "type": "string",
     "defaultValue": "Standard_LRS",
     "allowedValues": [
       "Standard_LRS",
       "Standard_GRS",
       "Standard_RAGRS",
       "Standard_ZRS",
       "Premium_LRS",
       "Premium_ZRS",
       "Standard_GZRS",
       "Standard_RAGZRS"
     ]
   },
   "location": {
     "type": "string",
     "defaultValue": "[resourceGroup().location]"
   }
 },
  "variables": {
    "storageAccountName": "[concat('companyname',parameters('environment'),'sa01'))]"
  },
 "resources": [
   {
     "type": "Microsoft.Storage/storageAccounts",
     "apiVersion": "2019-04-01",
     "name": "[variables('storageName')]",
     "location": "[parameters('location')]",
     "sku": {
       "name": "[parameters('storageSKU')]"
     },
     "kind": "StorageV2",
     "properties": {
       "supportsHttpsTrafficOnly": true
     }
   }
 ]
}

AzureDeploy.parameters.json

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "environment": {
      "value": "dev"
    }
  }
}

如果此部署因名称或 SKU 等问题而失败,我是否能够访问门户或以某种方式查看运行脚本时如何解析这些值?

部署发生在 AzureDevops 的 CD 管道中,我可以控制变量组等。因此我知道传入的内容,但不知道它如何解析。在更复杂的模板中,我收到一个错误,声称逻辑应用 API 连接上未设置 Id,但我无法判断该错误是否是由于我在 concat 函数中使用的变量引起的,或者该值是否确实不正确(解决根据传入的数据可以)。

如果有人熟悉如何通过 Azure 中的部署边栏选项卡解决这些问题,那么您可能会获得一些有关如何查看更详细 View 的提示。

谢谢

编辑:

下面的代码在 Visual Studio 2019 中触发 Intellisense,但已在部署期间确认可以正常工作。根据注释,VS Code 中没有警告。为了简洁起见,省略了大部分代码。

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "environment": {
          "type": "string",
          "defaultValue": "dev"
        },
        "increment": {
          "type": "string",
          "defaultValue": "01"
        },
        "keyvaultName": {
          "type": "string",
          "defaultValue": "randomKeyVaultName",
          "metadata": {
            "description": "Keyvault Name for deployment"
          }
        }
    },
    "variables": {
        "uniqueKeyVaultName": "[parameters('keyvaultName')]"
    },
    "resources": [
        {
          "type": "Microsoft.KeyVault/vaults/secrets",
          "apiVersion": "2016-10-01",
          "name": "[concat(variables('uniqueKeyVaultName'), '/407045A0-1B78-47B5-9090-59C0AE9A96F6')]",
          "location": "northeurope",
          "dependsOn": [
            "[resourceId('Microsoft.Resources/deployments', 'cosmosdb_linkedtemplate')]"
          ],
          "properties": {
            "contentType": "Graph",
            "value": "[concat('{''D'': ''DatabaseName'', ''U'': ''https://randomcosmosdb-',parameters('environment'),'-cdb-',parameters('increment'),'.documents.azure.com'', ''C'': ''CollectionName'', ''K'': ''',reference('cosmosdb_linkedtemplate').outputs.accountKey.value,'''}')]",
            "attributes": {
              "enabled": true
            }
          }
        }
    ],
    "outputs": {}
}

Visual Studio Version

最佳答案

如果您想查看评估后的模板,您可以执行以下操作来获取它而无需部署:

1) 调用/validate api: https://learn.microsoft.com/en-us/rest/api/resources/deployments/validate -- 但您目前需要使用较旧的 apiVersion(例如 2017-05-01)...响应将包含完全评估的模板。如果您有旧版本的 PowerShell 或 CLI,则可以使用 -debug 开关查看其余 API 的响应。但请记住,较新版本的 PS/CLI 将使用较新的 apiVersion,并且这些版本不会返回完整模板(此时)。

2)/whatif api 也将返回评估后的 JSON,但如果您所追求的只是评估后的模板,则需要做更多工作: https://learn.microsoft.com/en-us/azure/azure-resource-manager/templates/template-deploy-what-if

有帮助吗?

关于azure - 部署失败后获取已解析的 ARM 模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61140529/

相关文章:

azure-devops - Azure DevOps 与 Jira 集成

azure - [Azure 部署] : Can we filter Azure Resource Group Deployments by name using c#?

azure - 通过 Spark 更快地读取 Blob 存储

azure - 如何删除 MS Graph PowerShell 模块中的目录角色成员?

具有 scriptPath 选项的 Azure DevOps AzureCLI 任务无法通过身份验证来创建 Azure 容器实例

azure - 部署azure函数应用程序时出现ARM模板错误

Azure ARM VNET : How to restrict RDP access to a specific set of IPs

Azure 服务总线 - 随机反序列化问题

azure - 使用 Azure 存储资源管理器在 Azure 表中执行字符串操作的语法

oauth - 使用服务连接获取 OAuth token