复制Azure ARM部署模板中复制元素中的元素以创建资源的多个实例

时间:2017-12-06 05:40:39

标签: azure templates arm-template

我正在尝试通过copy element功能在Azure ARM部署模板中创建“嵌套for循环”,以创建相同资源类型的多个实例(在我的情况下为Microsoft.Web/sites/hostnameBindings)< / p>

更具体地说,我正在尝试将多个主机名绑定到多个应用程序(天蓝色应用程序服务网站)。

这可能吗?或者我需要走linked templates路径?

到目前为止,这是我的尝试,但我无法让它发挥作用。

parameters.json

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {    
    "appList": {
      "value": [
        { "appName": "app1", "hostNames": [ "app1.qqq.example.com", "app1.ttt.example.com" ] },
        { "appName": "app2", "hostNames": [ "app2.qqq.example.com" , "app2.ttt.example.com" ] },
        { "appName": "app3", "hostNames": [ "app3.qqq.example.com", "app3.ttt.example.com" ] }    
      ]
    }    
  }
}

template.json

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {    
    "appList": { "type": "array"  }
  },  
  "resources": [
    {
      "type": "Microsoft.Web/sites/hostnameBindings",
      "name": "parameters('appList')[copyIndex('webAppCopy')]/parameters('appList')[copyIndex('webAppCopy')].hostNames",
      "copy": [
        {
          "name": "webAppCopy",
          "count": "[length(parameters('appList'))]"
        }        
      ],
      "apiVersion": "2016-03-01",
      "location": "[resourceGroup().location]"
    }
  ],
  "outputs": {}
}

2 个答案:

答案 0 :(得分:0)

我从未在文档中看到任何关于直接支持嵌套的内容 循环。但您可以使用numeric functions解决此问题,如下所示:

  1. 定义一个模板变量,即应用程序数*每个应用程序的地址数。我们称这个变量为bindingCount
  2. 使用bindingCount作为“复制”的计数
  3. 构造资源名称时,使用div和mod函数获取copyIndex并将其转回“app index”和“hostname”索引。一世 我认为你必须在构造名称的公式中进行内联计算。

答案 1 :(得分:0)

我最终采用了另一种方法来解决此问题,这导致名称重复很多,但灵活性和可读性更好

parameters.json

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "webAppAzureNamePrefix": { "value": "mycompanyprefix-" },    
    "appList": { "value": [ "app1", "app2", "app3"]  },
    "hostBindings": {
      "metadata": { "description": "List of host bindings" },
      "value": [
        { "appName": "app1", "hostName": "app1.qqq.example.com" },
        { "appName": "app1", "hostName": "app1.ttt.example.com" },        
        { "appName": "app2", "hostName": "app2.qqq.example.com" },        
        { "appName": "app2", "hostName": "app2.ttt.example.com" },        
        { "appName": "app3", "hostName": "app3.qqq.example.com" },        
        { "appName": "app3", "hostName": "app3.ttt.example.com" },        
      ]
    }
  }
}

template.json

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "aspName": {
      "type": "string",
      "minLength": 1,
      "metadata": { "description": "Name of App Service Plan" }
    },
    "aspSkuName": {
      "type": "string",
      "allowedValues": [ "F1", "D1", "B1", "B2", "B3", "S1", "S2", "S3", "P1", "P2", "P3", "P4" ],
      "metadata": { "description": "Describes plan's pricing tier and capacity. Check details at https://azure.microsoft.com/en-us/pricing/details/app-service/" }
    },
    "appList": { "type": "array" },
    "hostBindings": { "type": "array" },
    "webAppAzureNamePrefix": { "type": "string" }    
  },
  "resources": [
    {
      "name": "[parameters('aspName')]",
      "type": "Microsoft.Web/serverfarms",
      "location": "[resourceGroup().location]",
      "apiVersion": "2015-08-01",
      "sku": { "name": "[parameters('aspSkuName')]" },
      "properties": {
        "name": "[parameters('aspName')]",
        "numberOfWorkers": 1
      }
    },
    {
      "name": "[concat(parameters('webAppAzureNamePrefix'), parameters('appList')[copyIndex()])]",
      "copy": {
        "name": "webAppCopy",
        "count": "[length(parameters('appList'))]"
      },
      "type": "Microsoft.Web/sites",
      "location": "[resourceGroup().location]",
      "apiVersion": "2015-08-01",
      "dependsOn": [ "[resourceId('Microsoft.Web/serverfarms', parameters('aspName'))]" ],
      "properties": {
        "name": "[concat(parameters('webAppAzureNamePrefix'), parameters('appList')[copyIndex()])]",
        "serverFarmId": "[resourceId('Microsoft.Web/serverfarms', parameters('aspName'))]"
      },
      "resources": []
    },    
    {
      "type": "Microsoft.Web/sites/hostnameBindings",
      "name": "[concat(parameters('webAppAzureNamePrefix'),parameters('hostBindings')[copyIndex()].appName, '/',parameters('hostBindings')[copyIndex()].hostName)]",
      "copy": {
        "name": "hostnameCopy",
        "count": "[length(parameters('hostBindings'))]",
        "mode": "Serial",
        "batchSize": 1
      },
      "apiVersion": "2016-03-01",
      "location": "[resourceGroup().location]",
      "properties": {
        "sslState": "SniEnabled",
        "thumbprint": "[reference(resourceId('Microsoft.Web/certificates', parameters('certificateName'))).Thumbprint]"
      },
      "dependsOn": [ "webAppCopy" ]
    }
  ],
  "outputs": {}
}