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

问题描述 投票:0回答:1

当使用New-AzureRmDeployment命令行开关部署通过主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不匹配。

EG

"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]"
    }
  }
}

PowerShell用于部署ARM模板:

$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
azure azure-resource-manager arm-template
1个回答
2
投票

我现在发现Microsoft文档说“不支持resourceGroup()函数。”用于https://docs.microsoft.com/en-us/azure/azure-resource-manager/deploy-to-subscription#using-template-functions的订阅级别部署。我使用的命令行开关是New-AzureRmDeployment,用于在当前订阅范围内部署资源。看起来我需要采取不同的方法。

© www.soinside.com 2019 - 2024. All rights reserved.