【问题标题】:reference to 'Microsoft.Resources/deployments/' requires an API version对“Microsoft.Resources/deployments/”的引用需要 API 版本
【发布时间】:2022-02-09 00:29:53
【问题描述】:

如果我在为虚拟网络部署模板之前添加条件,我总是会收到此错误:如果我删除条件,它会起作用???

模板部署返回以下错误: 错误:代码=无效模板;消息=部署模板验证失败:“模板资源 'Microsoft.Resources/deployments/dm5DbServer' 引用 'Microsoft.Resources/deployments/dm5VirtualNetwork' 需要 API 版本。

"resources": [
{
  "condition": "[equals(parameters('BuildDatabaseServer'), 'yes')]",
  "apiVersion": "2016-02-01",
  "name": "[variables('virtualNetworkName')]",
  "type": "Microsoft.Resources/deployments",
  "dependsOn": [],
  "properties": {
    "mode": "Incremental",
    "templateLink": {
      "uri": "[concat(parameters('_artifactsLocation'), '/', variables('virtualNetworkTemplateFileName'), parameters('_artifactsLocationSasToken'))]",
      "contentVersion": "1.0.0.0"
    },
    "parameters": {
      "virtualNetworkName": { "value": "[variables('virtualNetworkName')]" },
      "vNetPrefix": { "value": "[variables('vNetPrefix')]" },
      "databaseSubnetPrimaryName": { "value": "[variables('databaseSubnetPrimaryName')]" },
      "databaseSubnetPrimaryPrefix": { "value": "[variables('databaseSubnetPrimaryPrefix')]" },
      "databaseSubnetPrimaryNsgName": { "value": "[variables('databaseSubnetPrimaryNsgName')]" }
    }
  }
},

正在调用的模板:

  {
  "name": "[parameters('virtualNetworkName')]",
  "type": "Microsoft.Network/virtualNetworks",
  "location": "[resourceGroup().location]",
  "apiVersion": "2016-03-30",
  "dependsOn": [
    "[resourceId('Microsoft.Network/networkSecurityGroups', parameters('databaseSubnetPrimaryNsgName'))]",
  ],
  "tags": {
    "displayName": "[parameters('virtualNetworkName')]"
  },
  "properties": {
    "addressSpace": {
      "addressPrefixes": [
        "[parameters('vNetPrefix')]"
      ]
    },
    "subnets": [

      {
        "name": "[parameters('databaseSubnetPrimaryName')]",
        "properties": {
          "addressPrefix": "[parameters('databaseSubnetPrimaryPrefix')]",
          "networkSecurityGroup": {
            "id": "[resourceId('Microsoft.Network/networkSecurityGroups', parameters('databaseSubnetPrimaryNsgName'))]"
          }
        }
      }

  {
  "condition": "[equals(parameters('BuildDatabaseServer'), 'yes')]",
  "apiVersion": "2016-02-01",
  "name": "[variables('databaseServerName')]",
  "type": "Microsoft.Resources/deployments",
  "dependsOn": [
    "[resourceId('Microsoft.Resources/deployments', variables('virtualNetworkName'))]"
  ],
  "properties": {
    "mode": "Incremental",
    "templateLink": {
      "uri": "[concat(parameters('_artifactsLocation'), '/', variables('commonTemplateArchiveFolder'), '/', variables('virtualMachineTemplateFileName'), parameters('_artifactsLocationSasToken'))]",
      "contentVersion": "1.0.0.0"
    },
    "parameters": {
      "serverName": { "value": "[variables('databaseServerName')]" },
      "adminUserName": { "value": "[variables('databaseServerAdminUserName')]" },
      "adminPassword": { "value": "[parameters('databaseServerAdminPassword')]" },
      "serverWindowsOSVersion": { "value": "[parameters('databaseServerWindowsOSVersion')]" },
      "serverVmSize": { "value": "[variables('databaseServerVmSize')]" },
      "primaryNetworkSecurityGroupName": { "value": "[variables('databaseSubnetPrimaryNsgName')]" },
      "primarySubnetRef": { "value": "[reference(variables('virtualNetworkName')).outputs.databaseSubnetPrimaryRef.value]" },
      "primaryPrivateIPAddress": { "value": "[variables('databaseServerPrimaryPrivateIPAddress')]" },
      "serverOsDiskStorageAccountType": { "value": "[variables('databaseServerOSDiskStorageAccountType')]" },
      "serverDataDiskStorageAccountType": { "value": "[variables('databaseServerDataDiskStorageAccountType')]" },
      "serverDataDiskSizeGB": { "value": "[variables('databaseServerDataDiskSizeGB')]" },
      "monitoringAgentWorkspaceID": { "value": "[parameters('monitoringAgentWorkspaceID')]" },
      "monitoringAgentWorkspaceKey": { "value": "[parameters('monitoringAgentWorkspaceKey')]" },
      "customscripts": { "value": "[variables('customScripts')]" }
    }
  }
},

【问题讨论】:

    标签: azure


    【解决方案1】:

    好的,根据错误判断,您在同一个模板中有另一个子部署 (Microsoft.Resources/deployments/dm5DbServer),并且您正在使用参考函数从中获取一些数据,但它失败了,因为您没有提供 API 版本它失败。 Check the docs 对此。如果您引用的资源未部署在同一模板中,则需要为引用函数提供 api-version。

    reference(xxx, '2017-01-01`)
    

    【讨论】:

    • 我在上面的代码中添加了 Microsoft.Resources/deployments/dm5DbServer 资源。我需要更改哪个 API 资源?
    • 你能把这两个模板上传到它们可读的地方吗?像 paste.ee 或 pastebin 什么的?像这样检查它们真的很难吗?
    【解决方案2】:

    在我的例子中,我正在部署一个有条件的资源。如果不满足此条件,部署将失败并显示所述错误消息。

    原因是我使用reference(...) 语句在其他地方引用了条件资源。即使不应部署资源,似乎仍会评估引用调用。

    在引用调用修复问题之前添加条件:

    if({condition}, {original Statement}, 'resource not deployed')

    【讨论】:

    • 这不是说找不到名为“资源未部署”的资源吗?如果有的话,我认为你会想要一个空字符串。
    【解决方案3】:

    应用@DeveloperExceptionError 答案,这是一个使用aks_enable_private_cluster 变量进行条件部署的二头肌示例:

    // User Managed Identity
    resource idAccessToAks 'Microsoft.ManagedIdentity/userAssignedIdentities@2018-11-30' = if (aks_enable_private_cluster) {
      name: 'id_aks_access_n'
      location: resourceGroup().location
    }
    
    // User Managed Identity RBAC assignment
    resource idAccessToAksRoleAssignment 'Microsoft.Authorization/roleAssignments@2020-08-01-preview' = if (aks_enable_private_cluster) {
      name: guid(subscription().id, 'dev', '3498e952-d568-435e-9b2c-8d77e338d7f7')
      properties: {
        principalId: aks_enable_private_cluster ? idAccessToAks.properties.principalId : ''
        roleDefinitionId: '${subscription().id}/providers/Microsoft.Authorization/roleDefinitions/3498e952-d568-435e-9b2c-8d77e338d7f7'
        principalType: 'ServicePrincipal'
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-31
      • 1970-01-01
      • 1970-01-01
      • 2013-11-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-17
      相关资源
      最近更新 更多