azure - 使用New-AzureRmDeployment和链接的ARM模板创建资源组和资源时如何获取资源组名称?

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

使用 New-AzureRmDeployment commandlet 部署通过主 ARM 模板模板化的链接 ARM 时,尝试使用 resourceGroup().name 时出现错误功能和属性。错误信息是:

Unable to process template language expressions for resource
'/subscriptions/<subscriptionGuid>/resourceGroups/Zxy- 
Test/providers/Microsoft.Resources/deployments/storageDeployment' at line 
'29' and column '5'. 'The template function
'RESOURCEGROUP' is not expected at this location.`

作为替代方案,我尝试在不调用资源组函数的情况下使用 resourceId(...) 函数,但这给出了错误的资源 ID,其中资源组信息丢失且不匹配从 Azure 门户获取的资源 ID。

例如

"value": "[resourceId('Microsoft.Storage/storageAccounts', variables('storageName'))]"

缺少资源组的结果:

/subscriptions/<subscriptionGuid>/providers/Microsoft.Storage/storageAccounts/linktestdata

这是我所期望的,Azure 门户也报告了这一点

/subscriptions/<subscriptionGuid>/resourceGroups/Zxy-Test/providers/Microsoft.Storage/storageAccounts/linktestdata

我有以下 TestMaster.jsonTestLinked0.json 模板作为示例来演示该问题。

TestMaster.json

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "linkedTemplateUri": {
      "type": "string",
      "metadata": {
        "description": "URI to the linked ARM template file."
      }
    }
  },
  "resources": [
    {
      "apiVersion": "2018-05-01",
      "name": "testMasterDeployment",
      "type": "Microsoft.Resources/deployments",
      "location": "West US",
      "properties": {
        "mode": "Incremental",
        "templateLink": {
          "uri": "[parameters('linkedTemplateUri')]",
          "contentVersion": "1.0.0.0"
        },
        "parameters": {
          "tagValues": { "value": {
              "TagA": "A-Tag",
              "TagB": "B-Tag"
          }}
        }
      }
    }
  ],
  "outputs": {
    "messageFromMaster00": {
      "type": "string",
      "value": "Master-00 reporting"
    },
    "messageFromLinkedTemplate": {
      "type": "object",
      "value": "[reference('testMasterDeployment').outputs.messageFromNestedTemplate]"
    }
  }
}

TestLinked0.json

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "rgName": {
      "defaultValue": "Zxy-Test",
      "type": "string"
    },
    "location": {
      "defaultValue": "West US",
      "type": "string"
    },
    "tagValues": {
      "type": "object"
    }
  },
  "variables": {
    "storageName": "linktestdata"
  },
  "resources": [
    {
      "type": "Microsoft.Resources/resourceGroups",
      "apiVersion": "2018-05-01",
      "location": "[parameters('location')]",
      "name": "[parameters('rgName')]",
      "properties": {},
      "tags": "[parameters('tagValues')]"
    },
    {
      "type": "Microsoft.Resources/deployments",
      "apiVersion": "2017-05-10",
      "name": "storageDeployment",
      "resourceGroup": "[parameters('rgName')]",
      "dependsOn": [
        "[resourceId('Microsoft.Resources/resourceGroups/', parameters('rgName'))]"
      ],
      "properties": {
        "mode": "Incremental",
        "template": {
          "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
          "contentVersion": "1.0.0.0",
          "resources": [
            {
              "type": "Microsoft.Storage/storageAccounts",
              "apiVersion": "2017-10-01",
              "name": "[variables('storageName')]",
              "location": "[parameters('location')]",
              "tags": "[parameters('tagValues')]",
              "kind": "StorageV2",
              "sku": {
                "name": "Standard_LRS"
              }
            }
          ],
          "outputs": {
            "storageAccount": {
              "type": "string",
              "value": "[variables('storageName')]"
            },
            "resourceInfo0": {
              "type": "string",
              "value": "[resourceId('Microsoft.Storage/storageAccounts', variables('storageName'))]"
            },
            "resourceInfo1": {
              "type": "string",
              "value": "[resourceId(resourceGroup().name, 'Microsoft.Storage/storageAccounts', variables('storageName'))]"
            }
          }
        }
      }
    }
  ],
  "outputs": {
    "messageFromNestedTemplate": {
      "type": "object",
      "value": "[reference('storageDeployment').outputs]"
    }
  }
}

用于部署 ARM 模板的 PowerShell:

$uri = 'https://<containername>.blob.core.windows.net/azure-resource-templates/TestMaster.json'
$linkedTemplateUri = 'https://<containername>.blob.core.windows.net/azure-resource-templates/TestLinked0.json'
New-AzureRmDeployment -Location 'West US' -TemplateUri $uri -linkedTemplateUri $linkedTemplateUri

最佳答案

我现在找到了 Microsoft 文档,其中写着“不支持 ResourceGroup() 函数”。用于 https://learn.microsoft.com/en-us/azure/azure-resource-manager/deploy-to-subscription#using-template-functions 的订阅级别部署。我使用的 commandlet 是 New-AzureRmDeployment,用于在当前订阅范围内部署资源。看来我需要采取不同的方法。

关于azure - 使用New-AzureRmDeployment和链接的ARM模板创建资源组和资源时如何获取资源组名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52285566/

相关文章:

azure - ARM 模板,段长度不正确

database - 为基于日期的全局 DocumentDB 应用程序选择正确的 PartitionKey

azure - 在常规 Azure AD 租户内创建 Azure AD B2C 应用程序注册的目的是什么?

powershell - 禁止显示 Azure Stop-AzureRMVM cmdlet 警告消息框

azure - 使用 Azure 资源管理器模板配置 Azure Batch

azure - 迭代现有资源以获取其 MSI 主体 ID

azure - 使用 ARM 模板部署多个资源

适用于 Centos 的 Azure 快照调试器

azure - 如何使用 Bicep 配置 "All APIs"策略?

azure - 获取用于使用链接模板进行 ARM 部署的 Blob 存储容器的 SAS token