【问题标题】:ARM Template resources tags with condition带有条件的 ARM 模板资源标签
【发布时间】:2020-01-14 04:28:49
【问题描述】:

我已经创建了我的 ARM 模板,以便在我的开发环境中发布 Azure 资源。

现在我需要在 template.json 中为资源标签添加一个条件,即仅当 subscription().displayName 为 'Dev' 时才会创建标签。

除了'Dev',它不应该从资源下的那个template.json创建任何标签。

"resources": [
        {
            "type": "Microsoft.Web/connections",
            "apiVersion": "2016-06-01",
            "name": "[parameters('connections_office365_name')]",
            "location": "[resourceGroup().location]",
            "properties": {
                "displayName": "manish.jain@gmail.com",
                "customParameterValues": {},
                "api": {
                    "id": "[concat('/subscriptions/',subscription().subscriptionId,'/providers/Microsoft.Web/locations/',resourceGroup().location,'/managedApis/office365')]"
                }
            },
            "tags": {
                "Creator": "Manish Jain",
                "Environment": "Dev",
                "Date": "08/31/2019"
            }
        }

【问题讨论】:

  • 我创建了一个赏金,希望有人能帮助我解决这个问题。然后我自己想通了。希望对您有所帮助!

标签: tags arm conditional-statements


【解决方案1】:

以下 ARM 模板演示了如何使整个标签字典成为条件:


{
    "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "location": {
            "type": "string"
        },
        "storageAccountName": {
            "type": "string"
        },
        "accountType": {
            "type": "string"
        },
        "kind": {
            "type": "string"
        },
        "accessTier": {
            "type": "string"
        },
        "supportsHttpsTrafficOnly": {
            "type": "bool"
        },
        "createTag": {
            "type": "bool"
        }
    },
    "variables": {},
    "resources": [
        {
            "name": "[parameters('storageAccountName')]",
            "type": "Microsoft.Storage/storageAccounts",
            "apiVersion": "2019-06-01",
            "location": "[parameters('location')]",
            "properties": {
                "accessTier": "[parameters('accessTier')]",
                "supportsHttpsTrafficOnly": "[parameters('supportsHttpsTrafficOnly')]"
            },
            "dependsOn": [],
            "sku": {
                "name": "[parameters('accountType')]"
            },
            "kind": "[parameters('kind')]",
            "tags": "[if(parameters('createTag'),json('{\"tag1\": \"value1\",\"tag2\":\"value2\"}'), json('{}'))]"
        }
    ],
    "outputs": {}
}


要将其应用于您的特定场景,而不是[if(parameters('createTag')...,它将是[if(equals(subscription().displayName,'Dev')...

【讨论】:

    【解决方案2】:

    使用 if 块的条件标签有效;但是,这是最佳做法吗?

    如果您是根据环境有条件地创建标签,为什么不创建标签值作为参数并将它们传递到您的 ARM 模板中。

    这将避免整个条件片段,而是将参数模板中定义的特定标记值插入到 ARM 模板中。

    这种方法的缺点是每个环境都需要标记值,而不仅仅是较低的环境。然而;您的一致性会提高,因为每个环境都可以具有相同的标签和不同的值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-17
      • 1970-01-01
      • 1970-01-01
      • 2017-07-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多