Azure RM模板。有条件地部署拷贝资源
如果一个数组参数中有4个以上的条目,我需要部署特定的资源。我可以用5个(或更多)条目来完成这项工作,但我还需要部署不会因为3个或更少条目而失败,而是根本不创建资源。现在,我在3个或更少的条目中出现以下错误: 错误:代码=InvalidTemplate;消息=部署模板验证 失败:“第56行和第19列的模板“复制”定义” 具有无效的副本计数。复制计数必须是正整数 值,并且不能超过“800”。请查收 使用详情 我尝试向资源添加条件:Azure RM模板。有条件地部署拷贝资源,azure,azure-resource-manager,arm-template,azure-template,azure-rm,Azure,Azure Resource Manager,Arm Template,Azure Template,Azure Rm,如果一个数组参数中有4个以上的条目,我需要部署特定的资源。我可以用5个(或更多)条目来完成这项工作,但我还需要部署不会因为3个或更少条目而失败,而是根本不创建资源。现在,我在3个或更少的条目中出现以下错误: 错误:代码=InvalidTemplate;消息=部署模板验证 失败:“第56行和第19列的模板“复制”定义” 具有无效的副本计数。复制计数必须是正整数 值,并且不能超过“800”。请查收 使用详情 我尝试向资源添加条件: ... "resources": [ { "
...
"resources": [
{
"condition": "[greater(length(parameters('apps')),4)]",
"name": "[concat(parameters('apps')[copyIndex(4)].name,'-webtest')]",
...
甚至:
...
"resources": [
{
"condition": false,
"name": "[concat(parameters('apps')[copyIndex(4)].name,'-webtest')]",
...
但仍然得到同样的错误。
这是模板:
{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json",
"contentVersion": "1.0.0.0",
"parameters": {
"apps": {
"type": "array",
"defaultValue": [
{
"name": "name1",
"value": "111"
},
{
"name": "name2",
"value": "222"
},
{
"name": "name3",
"value": "333"
},
{
"name": "name4",
"value": "444"
},
{
"name": "webtest5",
"value": "555"
}
]
},
"existingApplicationInsightsName": {
"type": "string",
"defaultValue": "appname1"
}
},
"variables": {},
"resources": [
{
"name": "[concat(parameters('apps')[copyIndex(4)].name,'-webtest')]",
"apiVersion": "2015-05-01",
"type": "microsoft.insights/webtests",
"location": "westeurope",
"tags": {
"[concat('hidden-link:', resourceId('microsoft.insights/components/', parameters('existingApplicationInsightsName')))]": "Resource"
},
"properties": {
"SyntheticMonitorId": "[parameters('apps')[copyIndex(4)].name]",
"Name": "[parameters('apps')[copyIndex(4)].name]",
"Enabled": true,
"Frequency": 300,
"Timeout": 120,
"Kind": "ping",
"RetryEnabled": true,
"Locations": [
{
"Id": "us-ca-sjc-azr"
}
],
"Configuration": {
"WebTest": "[concat('<WebTest Name=\"', parameters('apps')[copyIndex(4)].name, '\"', ' Id=\"', '9d420f1a-f797-427a-804c-f37373eefc82' ,'\" Enabled=\"True\" CssProjectStructure=\"\" CssIteration=\"\" Timeout=\"0\" WorkItemIds=\"\" xmlns=\"http://microsoft.com/schemas/VisualStudio/TeamTest/2010\" Description=\"\" CredentialUserName=\"\" CredentialPassword=\"\" PreAuthenticate=\"True\" Proxy=\"default\" StopOnError=\"False\" RecordedResultFile=\"\" ResultsLocale=\"\"> <Items> <Request Method=\"GET\" Guid=\"a5f10126-e4cd-570d-961c-cea43999a200\" Version=\"1.1\" Url=\"', 'http://www.microsoft.com' ,'\" ThinkTime=\"0\" Timeout=\"300\" ParseDependentRequests=\"True\" FollowRedirects=\"True\" RecordResult=\"True\" Cache=\"False\" ResponseTimeGoal=\"0\" Encoding=\"utf-8\" ExpectedHttpStatusCode=\"', 200 ,'\" ExpectedResponseUrl=\"\" ReportingName=\"\" IgnoreHttpStatusCode=\"False\" /></Items></WebTest>')]"
}
},
"copy": {
"name": "createWebTests",
"count": "[sub(length(parameters('apps')),4)]"
}
}
]
}
{
“$schema”:”https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json",
“内容版本”:“1.0.0.0”,
“参数”:{
“应用程序”:{
“类型”:“数组”,
“默认值”:[
{
“名称”:“名称1”,
“值”:“111”
},
{
“名称”:“名称2”,
“值”:“222”
},
{
“名称”:“名称3”,
“值”:“333”
},
{
“名称”:“名称4”,
“值”:“444”
},
{
“名称”:“webtest5”,
“值”:“555”
}
]
},
“现有应用程序名称”:{
“类型”:“字符串”,
“defaultValue”:“appname1”
}
},
“变量”:{},
“资源”:[
{
“名称”:“[concat(参数('apps')[copyIndex(4)].name,'-webtest')]”,
“apiVersion”:“2015-05-01”,
“类型”:“microsoft.insights/webtests”,
“地点”:“西欧”,
“标签”:{
[concat('hidden-link:',resourceId('microsoft.insights/components/',parameters('existingApplicationInsightsName'))]:“资源”
},
“财产”:{
“SyntheticMonitorId:“[parameters('apps')[copyIndex(4)].name]”,
“名称”:“[parameters('apps')[copyIndex(4)].Name]”,
“启用”:正确,
“频率”:300,
“超时”:120,
“善良”:“平”,
“RetryEnabled”:true,
“地点”:[
{
“Id”:“美国加州sjc azr”
}
],
“配置”:{
“网络测试”:“[concat(“”)]”
}
},
“副本”:{
“名称”:“createWebTests”,
“计数”:“[sub(长度(参数('apps')),4)]”
}
}
]
}
试着这样做:
"condition": "[greater(length(parameters('apps')),4)]",
并将您的复制设置为:
"copy": {
"name": "createWebTests",
"count": "[if(greater(length(parameters('apps')),4), sub(length(parameters('apps')),4), 1)]"
}
这应该可以解决这样一个事实,即在您的案例计数为负值的情况下,当数组中的项目少于4个时,仍然没有部署任何内容使用您的语法和3个条目,我得到:
模板资源“[concat(parameters('apps')[copyIndex(4)]。name'-webtest]第“31”行和第“9”列中的“”无效:语言表达式属性数组索引“4”超出了范围
可用于5个条目。啊,对了,我认为创建嵌套部署和条件更有意义(因此您不必急于解决这些问题)。或者您需要对使用该参数的每个表达式设置条件。我想我确实会使用嵌套部署。你能举个例子吗?我如何“对每个使用该参数的表达式设置条件”?我还想知道为什么“条件”:“[更大(长度(参数('apps')),4)]”,
会被忽略?这个场景的文档中说:“如果您需要指定是否部署了资源,请参阅条件元素。”我相信这就是我们正在做的。它不会被忽略。arm模板分为两个步骤。汇编和执行。您的模板无法编译。条件仅在实现步骤中得到评估。不幸的是,条件在这里无法帮助您。我最终对每个使用copyIndex的参数都使用了if
函数。这可能就是您的意思:“您需要对使用该参数的每个表达式设置条件”<代码>“计数”:“[if(更大(长度(参数('apps')))、4)、sub(长度(参数('apps'))、4)、1)]”是键。非常感谢。