如何在ARM模板的标签对象类型中使用utcNow()



我在资源标签中使用utcNow(),但不是获得日期和时间,而是获得"[utcNow()]"只有一次。下面是我使用它的Arm模板的格式。

模板参数:
"resourceTags": {
"type": "object"
}

参数。json值:

"resourceTags": {
"value": {
"criticality": "Tier1",
"applicationName": "Devzone",
"owner": "abcd",
"costCenter": "1234",
"contactEmail": "abcd@gmail.com",
"dataClassification": "Confidential",
"environment": "Dev",
"CreatedDate": "[utcNow()]"
}
}

我试图在管道变量组中使用。我仍然得到相同的"[utcNow()]"而不是值。

我做错了什么?我怎样才能取代utcNow()呢?

根据文档,您只能使用utcNow()函数作为参数的默认值。

该函数不允许出现在模板的其他部分,因为每次调用它都会返回不同的值。使用相同的参数部署相同的模板不会可靠地产生相同的结果。

这是给定对象

的样子
"resourceTags": {
"type": "object",
"defaultValue" : {
"criticality": "Tier1",
"applicationName": "Devzone",
"owner": "abcd",
"costCenter": "1234",
"contactEmail": "abcd@gmail.com",
"dataClassification": "Confidential",
"environment": "Dev",
"CreatedDate": "[utcNow()]"
}
}
但是,如果为参数提供值,则整个对象将被覆盖。所以不是在那里设置它,你需要将对象的值分离到单独的参数中。对于CreatedDate参数,将默认值设置为,而不要在管道中提供该值。然后将标签设置为参数。
"CreatedDate": "[parameters('utcNow')]"

我还找到了其他方法来实现这一点。请找到下面的json.

{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"tagValues": {
"type": "object",
"defaultValue": {
"Dept": "Finance",
"Environment": "Production"
}
}
},
"variables" : {
"customTag" : {"myExtraDifferentTag": "myDifferentTagValue", 
"myAnotherExtraDifferentTag": "myAnotherDifferentTagValue"}
},
"resources": [
{
"apiVersion": "2016-01-01",
"type": "Microsoft.Storage/storageAccounts",
"name": "[concat('storage', uniqueString(resourceGroup().id))]",
"location": "[resourceGroup().location]",
"tags": "[union(parameters('tagValues'),json('{"myExtraTag":"myTagValue "}'))]",  //Concatenates `tagValues` object to inline object    
"sku": {
"name": "Standard_LRS"
},
"kind": "Storage",
"properties": {}
}
{
"apiVersion": "2016-01-01",
"type": "Microsoft.Storage/storageAccounts",
"name": "mySecondResource",
"location": "[resourceGroup().location]",
"tags": "[union(parameters('tagValues'),variables('customTag'))]",     //Concatenates `tagValues` object to `customTag` object
"sku": {
"name": "Standard_LRS"
},
"kind": "Storage",
"properties": {}
}
]
}

我按照下面的步骤,我得到了我想要的结果

"utcShort ": {
"type": "string",
"defaultValue": "[utcNow()]"
},
"resourceTags": {
"type": "object"
}
},
"variables":{
"createdDate": {
"createdDate": "[parameters('utcShort ')]"
}
},

在标签中使用这个变量,如下所示。

"tags": "[union(parameters('resourceTags'), variables('createdDate'))]"

这对我有用。谢谢你

最新更新