从 Azure ARM 模板中检索订阅标签

Retrieve subscription tag from within Azure ARM template

我正在尝试从 ARM 模板中检索订阅标签。我尝试了各种方法,但无法检索它们,有什么想法吗?

我试过没有成功...

{
  "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {  },
  "variables": {  },
  "resources": [ ],
  "outputs": {
    "subscriptionId": {
      "type": "string",
      "value": "[subscription().subscriptionId]"
    },
    "subObj": {
      "type": "object",
      "value": "[subscription()]"
    },
    "subResource": {
      "type": "object",
      "value": "[reference(subscriptionResourceId('Microsoft.Resources/tags', 'default'), '2019-10-01', 'Full')]"
    },
    "rgName": {
      "type": "string",
      "value": "[reference(subscriptionResourceId('Microsoft.Resources/tags', 'default'), '2019-10-01', 'Full').resourceGroupName]"
    },
    "ext": {
      "type": "string",
      "value": "[extensionResourceId(concat('/subscriptions/', subscription().subscriptionId) , 'Microsoft.Resources/tags', 'default')]"
    },
    "subResource2": {
      "type": "Object",
      "value": "[reference(subscriptionResourceId(subscription().subscriptionId, 'Microsoft.Resources/tags', 'default'), '2019-10-01')]"
    }
  }
}

任何帮助,非常感谢。

是的,可以与 Azure Policy 一起使用。

这是一个例子:

{
  "mode": "All",
  "parameters": {
    "diagnosticsSettingsName": {
      "type": "String",
      "metadata": {
        "displayName": "Setting name",
        "description": "Name of the policy for the diagnostics settings."
      },
      "defaultValue": "centalizedLogs"
    }
  },
  "policyRule": {
    "if": {
      "field": "type",
      "equals": "Microsoft.Network/networkSecurityGroups"
    },
    "then": {
      "effect": "deployIfNotExists",
      "details": {
        "type": "Microsoft.Insights/diagnosticSettings",
        "roleDefinitionIds": [
          "/providers/Microsoft.Authorization/roleDefinitions/b24988ac-6180-42a0-ab88-20f7382dd24c"
        ],
        "existenceCondition": {
          "allOf": [
            {
              "field": "Microsoft.Insights/diagnosticSettings/logs.enabled",
              "equals": "True"
            },
            {
              "field": "Microsoft.Insights/diagnosticSettings/metrics.enabled",
              "equals": "True"
            },
            {
              "field": "name",
              "matchInsensitively": "[parameters('diagnosticsSettingsName')]"
            }
          ]
        },
        "deployment": {
          "properties": {
            "mode": "incremental",
            "template": {
              "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
              "contentVersion": "1.0.0.0",
              "parameters": {
                "diagnosticsSettingsName": {
                  "type": "string"
                },
                "resourceName": {
                  "type": "string"
                },
                "location": {
                  "type": "string"
                }
              },
              "variables": {
                "referenceSubscriptionTagsResourceId": "[concat('/subscriptions/', subscription().subscriptionId, '/providers/Microsoft.Resources/tags/default')]",
                "referenceSubscriptionTagsApi": "2019-10-01"
              },
              "resources": [
                {
                  "type": "Microsoft.Network/networkSecurityGroups/providers/diagnosticSettings",
                  "name": "[concat(parameters('resourceName'), '/microsoft.insights/', parameters('diagnosticsSettingsName'))]",
                  "apiVersion": "2017-05-01-preview",
                  "location": "[parameters('location')]",
                  "dependsOn": [],
                  "properties": {
                    "workspaceId": "[resourceId(subscription().subscriptionId, 'hub', 'Microsoft.OperationalInsights/workspaces', concat('logws-', reference(variables('referenceSubscriptionTagsResourceId'), variables('referenceSubscriptionTagsApi')).tags.applicationName))]",
                    "logs": [
                      {
                        "category": "NetworkSecurityGroupEvent",
                        "enabled": true
                      },
                      {
                        "category": "NetworkSecurityGroupRuleCounter",
                        "enabled": true
                      }
                    ],
                    "metrics": []
                  }
                }
              ],
              "outputs": {}
            },
            "parameters": {
              "diagnosticsSettingsName": {
                "value": "[parameters('diagnosticsSettingsName')]"
              },
              "location": {
                "value": "[field('location')]"
              },
              "resourceName": {
                "value": "[field('fullName')]"
              }
            }
          }
        }
      }
    }
  }
}