具有条件的ARM模板资源标签

问题描述 投票:1回答:1

我创建了我的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": "[email protected]",
                "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个回答
0
投票

以下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')...

© www.soinside.com 2019 - 2024. All rights reserved.