在使用资源迭代部署arm模板时,我希望将资源属性作为对象传递。
这样做将允许复制数组中的每个元素中存在一组不同的参数。原因是,根据其他属性的值,可能需要有条件地包含或排除某些属性。例如,对于一个api管理产品,文档说明了关于
subscriptionsLimit
财产-
仅当SubscriptionRequired属性存在且其值为false时才可以存在。
但是,在azure中部署部署挂起下面的示例模板时。在查看相关事件时,我可以看到部署资源的操作由于内部服务器错误(500)而不断失败,但没有其他详细信息。
如果我引用
properties
对象使用
variables('productsJArray')[copyIndex()].whatever
然后部署成功。然而,这是不可取的,因为它意味着
性质
对象必须包含相同的参数,这并不总是允许的,并且可能导致部署失败。
示例模板
注意,我已经输出了
variables('productsJArray')[copyIndex()]
它是一个有效的对象。我甚至将输出复制到模板中并成功地部署了它。
{
"$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"apiManagementServiceName": {
"type": "string",
"metadata": {
"description": "The name of the API Management instance."
}
},
"productsJson": {
"type": "string",
"metadata": {
"description": "A JSON representation of the Products to add."
}
}
},
"variables": {
"productsJArray": "[json(parameters('productsJson'))]"
},
"resources": [
{
"condition": "[greater(length(variables('productsJArray')), 0)]",
"type": "Microsoft.ApiManagement/service/products",
"name": "[concat(parameters('apiManagementServiceName'), '/', variables('productsJArray')[copyIndex()].name)]",
"apiVersion": "2018-06-01-preview",
"properties": "[variables('productsJArray')[copyIndex()]]",
"copy": {
"name": "productscopy",
"count": "[if(greater(length(variables('productsJArray')), 0), length(variables('productsJArray')), 1)]"
}
}
]
}
示例参数
{
"$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"apiManagementServiceName": {
"value": "my-api-management"
},
"productsJson": {
"value": "[{\"name\":\"my-product\",\"displayName\":\"My Product\",\"description\":\"My product is awesome.\",\"state\":\"published\",\"subscriptionRequired\":true,\"approvalRequired\":false}]"
}
}
}
变量“productsjarray[0]的输出
"outputs": {
"properties": {
"type": "Object",
"value": {
"approvalRequired": false,
"description": "My product is awesome.",
"displayName": "My Product",
"name": "my-product",
"state": "published",
"subscriptionRequired": true
}
}
}