如何将当前时间标记为 ARM 部署的标记

How to tag Current time as a Tag for an ARM Deployment

我正在尝试使用 ARM 模板和参数文件创建 Log Analytics 工作区。我也在考虑将当前时间标记为资源的 CreatedOn 标记。 下面是我的ARM模板-

{
    "$schema": "http://schema.management.azure.com/schemas/2014-04-01-preview/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "LAWName": {
            "type": "string"
        },
        "LocationName": {
            "type": "string"
        },
        "SKUName": {
            "type": "string"
        },
        "Tags": {
            "type": "object"
        }
    },
    "resources": [
        {
            "apiVersion": "2017-03-15-preview",
            "name": "[parameters('LAWName')]",
            "location": "[parameters('LocationName')]",
            "tags": "[parameters('Tags')]",
            "type": "Microsoft.OperationalInsights/workspaces",
            "properties": {
                "sku": {
                    "name": "[parameters('SKUName')]"
                }
            }
        }
    ]
}

这是我的参数文件-

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "LAWName": {
            "value": "atifmtest1"
        },
        "LocationName": {
            "value": "westeurope"
        },
        "SKUName": {
            "value": "pergb2018"
        }
        "Tags": {
            "value": {
                "CreatedBy": "Atif",
                "CreatedOn": "[utcNow()]",
                "Purpose": "Monitoring"
            }
        }
    }
}

我在此处 https://docs.microsoft.com/en-us/azure/azure-resource-manager/templates/template-functions-date#utcnow 了解到 ARM 模板有 utcNow() 函数,但此处被视为字符串,当前时间未显示为资源的标签。 实现这一目标的另一种方法是什么? 提前致谢!!

这是一个工作示例:

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "utcShort": {
            "type": "string",
            "defaultValue": "[utcNow('d')]"
        },
        "location": {
            "type": "string",
            "defaultValue": "[resourceGroup().location]"
        }
    },
    "resources": [
        {
            "apiVersion": "2019-04-01",
            "type": "Microsoft.Storage/storageAccounts",
            "name": "[concat('storage', uniqueString(resourceGroup().id))]",
            "location": "[parameters('location')]",
            "tags": {
                "Dept": "Finance",
                "Environment": "Production",
                "LastDeployed": "[parameters('utcShort')]"
            },
            "sku": {
                "name": "Standard_LRS"
            },
            "kind": "Storage",
            "properties": {}
        }
    ]
}

Source.

请按照以下步骤操作以获得更好的结果。 在参数中添加 utcShort 并给出默认值“[utcNow()]”,它在参数文件中不起作用。将 utcShort 添加到变量中以创建对象类型。按照以下步骤操作。

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

在如下标签中使用此变量..

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