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

串友

当使用Commandlet部署通过主ARM模板通过模板链接的ARM模板New-AzureRmDeployment时,尝试使用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://docs.microsoft.com/zh-cn/azure/azure-resource-manager/deploy-to-subscription#using-template-functions我正在使用的commandlet是New-AzureRmDeployment,用于在当前订阅范围内部署资源。看来我需要采取其他方法。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用Azure Management C#API创建资源组

如何使用VSTS版本管理部署到Azure资源组

Azure ARM模板:创建资源组

如何使用Azure资源管理API按资源类型和资源组获取资源列表

如何使用foreach脚本锁定Azure资源组中的所有资源

使用PowerShell从Azure资源组导出类型

Pulumi正在使用另一个名称创建一个Azure资源组

使用ARM模板创建Microsoft.Web / serverfarms资源

如何使用Java在资源组中创建IoTHub资源?

使用boto3创建AWS资源组

如何使用ARM模板为Azure资源创建活动日志诊断设置

如何使用PowerShell在Azure订阅之间列出资源组?

使用ARM模板部署多个资源

使用调度组时如何使公共资源线程安全?

如何使用Azure Mgmt SDK流利的获取空资源组的列表

如何使用Python锁定Azure资源组中的所有资源

使用PowerShell覆盖Azure资源组上的标记名称

使用C#在Azure中创建资源组

使用Maven时如何获取资源的路径?

为什么使用资源组库模板在Azure中创建Wordpress网站时,msdeploy失败?

使用 Kendo 调度程序资源分组时查找插槽的组

使用 Powershell 在资源组中的资源上实现标签

我们如何在 MS CRM 门户中使用“验证组”和“内联呈现 Web 资源”?

ARM 模板:如何引用使用参数设置名称的资源

使用引用表时如何构建 Laravel 模型和资源?

在 Blob 容器中读取链接的 ARM 模板时,Azure 资源管理器使用哪些 IP 范围

如何使用 Azure 不破坏 Terraform 中导入的资源组

使用 ARM 模板部署资源

使用 Bicep 创建 Azure 资源组