azure - ARM 模板条件嵌套资源,无需模板链接

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

我正在尝试创建条件资源模板。开发环境不像生产环境那么强大,而且我在很大程度上成功地做到了这一点。但是,我似乎无法正确获取嵌套资源。

这是我的 ARM 模板的片段:

"webApp-resources": "[variables(concat('webApp-', parameters('env'), '-resources'))]",
"webApp-dev-resources": [],
"webApp-prod-resources": [
  {
    "name": "staging",
    "type": "Microsoft.Web/sites/slots",
    "location": "[resourceGroup().location]",
    "apiVersion": "2015-08-01",
    "dependsOn": [
      "[resourceId('Microsoft.Web/sites', variables('webApp-name'))]"
    ]
  }
],

想法很简单,resources 变量是使用 env 参数组成的。 env 参数可以是 devprod,虽然这有效,但当我尝试部署此模板时,出现以下错误。

{
  "name": "[variables('webApp-name')]",
  "type": "Microsoft.Web/sites",
  ...
  "resources": "[variables('webApp-resources')]" // <- culprit!
},

The request content was invalid and could not be deserialized: 'Error converting value "[variables('webApp-resources')]" to type 'Microsoft.WindowsAzure.ResourceStack.Frontdoor.Templates.Schema.TemplateResource[]'. Path 'properties.template.resources[1].resources', line 195, position 64.'

我还尝试将资源移动到变量中并以类似的条件方式引用该变量,这与我们进行嵌套模板链接但没有模板链接的方式非常相似。

resources: [
  "[variables('webApp-resource')]" // <- this doesn't work!
]

这会导致类似的错误,但如果我没记错的话,会出现不同的错误。

由此我得出的结论是,ARM 模板语法并不是简单地查找和替换,我认为这是不好的,因为它确实使我们更难以推断哪些有效、哪些无效。因为如果是的话,就会产生一个有效的模板。我已通过将正确的值粘贴到资源部分来验证这一点。

有人遇到过类似的问题吗?您是如何解决这个问题的?

最佳答案

您应该能够在没有多个模板文件的情况下执行此操作,但如果不使用嵌套部署则不能。因此,根据您想要避免的情况,尝试以下操作:

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "env": {
      "type": "string",
      "allowedValues": [ "dev", "prod" ]
    }
  },
  "variables": {
    "resourceArray": "[variables(concat('resources', parameters('env')))]",
    "resourcesprod": [
        {
          "name": "as",
          "type": "Microsoft.Compute/availabilitySets",
          "location": "[resourceGroup().location]",
          "apiVersion": "2015-06-15",
          "dependsOn": [],
          "properties": {
          }
        }
      ],
    "resourcesdev": []
  },
    "resources": [
      {
        "name": "nest",
        "type": "Microsoft.Resources/deployments",
        "apiVersion": "2016-09-01",
        "dependsOn": [],
        "properties": {
          "mode": "Incremental",
          "template": {
            "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
            "contentVersion": "1.0.0.0",
            "resources": "[variables('resourceArray')]"
          },
          "parameters": {
          }
        }
      }
    ],
  "outputs": {}
}

关于azure - ARM 模板条件嵌套资源,无需模板链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42385941/

相关文章:

c# - Azure UDP 套接字限制?

c# - 如何发布到 Azure Functions 运行时

当azure应用程序重新启动时,通过SSH安装的Python包被删除

azure - ARM模板: Way to workaround for nested copy in variable section

azure - 如何获取 Azure RM 资源(包括 Azure 中的所有资源)的创建日期

azure - 如何在 ARM 模板中设置服务总线逻辑应用程序操作的连接字符串?

azure - 条件语句不适用于虚拟机扩展

c# - 结合 WPF + WCF + Entity Framework 的好例子

azure - 将访问策略添加到位于另一个资源组中的 Key Vault

azure - 如何在Azure ARM模板中生成unix时间戳