在 Azure ARM 部署模板中复制元素内的复制元素以创建资源的多个实例
Copy element within copy element in Azure ARM deployment template to create multiple instances of a resource
我正在尝试通过 copy element feature to create multiple instance of the same resource type (Microsoft.Web/sites/hostnameBindings 在我的 Azure ARM 部署模板中创建 "nested for loop")
更具体地说,我正在尝试将多个主机名绑定到多个应用程序(Azure 应用程序服务网站)。
这可能吗?或者我需要走 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": {}
}
我从未在文档中看到任何关于直接支持嵌套的内容
循环。但是您可能可以使用 numeric functions 解决此问题,如下所示:
- 定义一个模板变量,它是应用程序数 * 每个应用程序的地址数。我们称这个变量为 bindingCount
- 使用 bindingCount 作为 "copy"
的计数
- 构造资源名称时,使用div和mod函数获取copyIndex并将其转回"app index"和"hostname"索引。我
认为您必须在构造名称的公式中进行内联数学运算。
我最终采用了一种不同的方法来解决这个问题,这导致了稍微重复的名称,但给了我更好的灵活性和可读性
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": {}
}
我正在尝试通过 copy element feature to create multiple instance of the same resource type (Microsoft.Web/sites/hostnameBindings 在我的 Azure ARM 部署模板中创建 "nested for loop")
更具体地说,我正在尝试将多个主机名绑定到多个应用程序(Azure 应用程序服务网站)。
这可能吗?或者我需要走 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": {}
}
我从未在文档中看到任何关于直接支持嵌套的内容 循环。但是您可能可以使用 numeric functions 解决此问题,如下所示:
- 定义一个模板变量,它是应用程序数 * 每个应用程序的地址数。我们称这个变量为 bindingCount
- 使用 bindingCount 作为 "copy" 的计数
- 构造资源名称时,使用div和mod函数获取copyIndex并将其转回"app index"和"hostname"索引。我 认为您必须在构造名称的公式中进行内联数学运算。
我最终采用了一种不同的方法来解决这个问题,这导致了稍微重复的名称,但给了我更好的灵活性和可读性
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": {}
}