【问题标题】:Azure ARM dependencies between nested templates嵌套模板之间的 Azure ARM 依赖项
【发布时间】:2017-06-24 22:23:15
【问题描述】:

我创建了一个包含两个嵌套模板的模板。第一个嵌套模板将 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"
    }
}

但是在尝试部署它时会出错:

模板中未定义资源“Microsoft.Sql/servers/mysqlserver”。

我怎样才能把它放到数据库中才能看到兄弟模板中的sql server?它确实使用正确的名称部署了 sql server,所以这不是命名问题。

谢谢

【问题讨论】:

    标签: azure azure-resource-manager


    【解决方案1】:

    为这些资源创建嵌套模板绝对没有意义,但是如果你想这样做,你应该定义实际的嵌套模板调用以相互依赖(所以第二个嵌套模板应该依赖于第一个) 在 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版本的情况下使用引用函数

    【讨论】:

    • 我的计划是创建一个“共享”模板(第一个),它将定义其他嵌套模板(sql server、服务总线等)将使用的所有内容。但我想它们可以在父模板中定义
    • 除非您需要解决 ARM 模板限制,否则您不需要创建嵌套模板(为什么要复杂化?)。
    • 我有很多资源要创建,所以我按逻辑区域将它们分成单独的模板。
    • 我尝试将依赖项添加到您建议的父模板中,但仍然出现错误。我更新了我的问题以显示父模板,有什么问题吗?
    • 你需要从嵌套模板中删除dependsOn,就像我说的那样
    猜你喜欢
    • 2020-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-28
    • 1970-01-01
    • 2011-03-19
    相关资源
    最近更新 更多