首页 > 解决方案 > 适用于 Linux 和 Windows 计算机的 Azure 标记策略

问题描述

有一个 ARM 策略用于检查 Azure 中任何 VM 上是否存在特定标签:

{
  "mode": "Indexed",
  "policyRule": {
    "if": {
      "AllOf": [
        {
          "field": "type",
          "in": [
            "microsoft.compute/virtualmachines",
            "Microsoft.ClassicCompute/virtualMachines"
          ]
        },
        {
          "field": "[concat('tags[', parameters('tagName'), ']')]",
          "exists": "false"
        }
      ]
    },
    "then": {
      "effect": "audit"
    }
  },
  "parameters": {
    "tagName": {
      "type": "String",
      "metadata": {
        "displayName": "tagName",
        "description": "Tag used to unequally identify a VM"
      }
    }
  }
}

问题:

如何创建一个单独的策略,一个用于 Linux,另一个用于 Windows VM?我找不到如何过滤的方法。即对于 Windows,我使用此策略脚本:

{
  "mode": "Indexed",
  "policyRule": {
    "if": {
      "allOf": [
        {
          "field": "type",
          "equals": "Microsoft.Compute/virtualMachines"
        },
        {
          "field": "Microsoft.Compute/imagePublisher",
          "in": [
            "MicrosoftWindowsServer",
            "MicrosoftSQLServer"
          ]
        },
        {
          "field": "[concat('tags[', parameters('tagName'), ']')]",
          "exists": "false"
        }
      ]
    },
    "then": {
      "effect": "audit"
    }
  },
  "parameters": {
    "tagName": {
      "type": "String",
      "metadata": {
        "displayName": "tagName",
        "description": "Tag used to help unequally identify a VM"
      }
    }
  }
}

但上面同时显示了 Windows 和 Linux,因为它的逻辑类似于“如果 VM 是 windows 并且具有特定标签,则表示兼容;否则如果没有特定标签或 VM 是 Linux,则视为不兼容”。

需求:

1x 策略查看只有多少 Windows VM 符合要求(根据标签)。

1x 策略查看只有多少 Linux VM 符合要求(根据标签)。

标签: azureazure-policy

解决方案


尝试使用 Microsoft.Compute/virtualMachines/imagePublisher 而不是 Microsoft.Compute/imagePublisher


推荐阅读