嵌套模板之间的 Azure ARM 依赖关系

Azure ARM dependencies between nested templates

我创建了一个包含两个嵌套模板的模板。第一个嵌套模板将 SQL 服务器定义为:

{
  "name": "[variables('sqlServerName')]",
  "type": "Microsoft.Sql/servers",
  "location": "[resourceGroup().location]",
  "apiVersion": "2014-04-01-preview",
  "dependsOn": [],
  "tags": {
    "displayName": "SqlServer"
  },
  "properties": {
    "administratorLogin": "[parameters('sqlAdministratorLogin')]",
    "administratorLoginPassword": "[parameters('adminLoginPassword')]"
  },
  "resources": [
    {
      "name": "AllowAllWindowsAzureIps",
      "type": "firewallrules",
      "location": "[resourceGroup().location]",
      "apiVersion": "2014-04-01-preview",
      "dependsOn": [
        "[resourceId('Microsoft.Sql/servers', variables('sqlServerName'))]"
      ],
      "properties": {
        "startIpAddress": "0.0.0.0",
        "endIpAddress": "0.0.0.0"
      }
    }
  ]
}

第二个嵌套模板定义了将托管在上述服务器上的数据库:

{
  "name": "[concat(parameters('sqlServerName'), '/', 'Admin')]",
  "type": "Microsoft.Sql/servers/databases",
  "location": "[resourceGroup().location]",
  "apiVersion": "2014-04-01-preview",
  "dependsOn": [],
  "tags": {
    "displayName": "AdminApiDb"
  },
  "properties": {
    "collation": "[parameters('AdminApiDbCollation')]",
    "edition": "[parameters('AdminApiDbEdition')]",
    "maxSizeBytes": "1073741824",
    "requestedServiceObjectiveName": "[parameters('AdminApiDbRequestedServiceObjectiveName')]"
  }
}

然后我的父模板如下所示:

 {
  "name": "Shared",
  "type": "Microsoft.Resources/deployments",
  "apiVersion": "2016-09-01",
  "dependsOn": [],
  "properties": {
    "mode": "Incremental",
    "templateLink": {
      "uri": "[concat(parameters('_artifactsLocation'), '/', variables('SharedTemplateFolder'), '/', variables('SharedTemplateFileName'), parameters('_artifactsLocationSasToken'))]",
      "contentVersion": "1.0.0.0"
    }
},
{
  "name": "Admin",
  "type": "Microsoft.Resources/deployments",
  "apiVersion": "2016-09-01",
  "dependsOn": [
    "[concat('Microsoft.Resources/deployments/', 'Shared')]"
  ],
  "properties": {
    "mode": "Incremental",
    "templateLink": {
      "uri": "[concat(parameters('_artifactsLocation'), '/', variables('AdminTemplateFolder'), '/', variables('AdminTemplateFileName'), parameters('_artifactsLocationSasToken'))]",
      "contentVersion": "1.0.0.0"
    }
}

但是当尝试部署它时出现错误:

The resource 'Microsoft.Sql/servers/mysqlserver' is not defined in the template.

如何获取数据库以查看兄弟模板中的 sql 服务器?它确实部署了具有正确名称的 sql 服务器,因此这不是命名问题。

谢谢

为这些资源创建嵌套模板绝对没有意义,但如果你想那样做,你应该定义实际的嵌套模板调用以相互依赖(所以第二个嵌套模板应该依赖于第一个)在 parent 模板中,而不是 inside 嵌套模板中的资源。

您只能对模板中的资源使用dependsOn,那些资源不在模板中。

好的,你的模板还有一个bug,如果不指定api版本,你不能对不在同一个模板中的资源使用引用函数。

"[concat('Data Source=tcp:', reference(concat('Microsoft.Sql/servers/', parameters('sqlserverName')), '2014-04-01-preview').fullyQualifiedDomainName, ',1433;Initial Catalog=Admin', ';User Id=', parameters('sqlAdministratorLogin'), '@', reference(concat('Microsoft.Sql/servers/', parameters('sqlserverName')), '2014-04-01-preview').fullyQualifiedDomainName, ';Password=', parameters('sqlAdministratorLoginPassword'), ';')]",

如果资源在同一模板中,您可以使用没有 api 版本的引用函数