You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Azure Policy中如何编写Redis maxmemory-policy的existenceCondition

问题:Azure Policy 配置Redis缓存maxmemory-policy为allkeys-lru的存在性判断问题

我需要通过Azure Policy将Redis缓存的maxmemory-policy设置为allkeys-lru,但编写existenceCondition时遇到了困难——需要判断该配置是否已设为目标值,但找不到对应的属性别名,只能找到父级的redisConfiguration(Object类型)。

用于查看该别名的PowerShell命令如下:

$redisAliases = (Get-AzPolicyAlias | Where-Object {$_.namespace -eq 'Microsoft.Cache' -and $_.ResourceType -eq 'Redis'}).Aliases

($redisAliases | Where-Object {$_.name -match 'redisConfiguration$'}).DefaultMetadata

我尝试过以下几种写法,均无法正常工作:

  • 通过redisConfiguration.maxmemory-policy和redisConfiguration['maxmemory-policy']访问属性,被判定为无效别名
  • 直接匹配整个redisConfiguration对象:
"existenceCondition": {
    "field": "Microsoft.Cache/Redis/redisConfiguration",
    "equals": {"maxmemory-policy": "allkeys-lru"}
  },

我已向微软提交工单,希望有人能告知正确的实现方式。以下是包含失效existenceCondition的完整策略定义:

{
    "mode": "All",
    "policyRule": {
      "if": {
          "field": "type",
          "equals": "Microsoft.Cache/redis"
      },
      "then": {
        "effect": "[parameters('effect')]",
        "details": {
          "type": "Microsoft.Cache/redis",
          "name": "[field('name')]",
          "evaluationDelay": "AfterProvisioning",
          "existenceCondition": {
            "field": "Microsoft.Cache/Redis/redisConfiguration.maxmemory-policy",
            "equals": "allkeys-lru"
          },
          "deployment": {
            "properties": {
              "mode": "incremental",
              "parameters": {
                "redisName": {
                  "value": "[field('name')]"
                },
                "location": {
                  "value": "[field('location')]"
                },
                "redisCapacity": {
                    "value": "[field('Microsoft.Cache/redis/sku.capacity')]"
                },
                "redisTier": {
                    "value": "[field('Microsoft.Cache/redis/sku.name')]"
                },
                "redisFamily": {
                    "value": "[field('Microsoft.Cache/redis/sku.family')]"
                }
              },
              "template": {
                "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
                "contentVersion": "1.0.0.0",
                "parameters": {
                    "redisName": {
                        "type": "string"
                    },
                    "location": {
                        "defaultValue": "northeurope",
                        "type": "string"
                    },
                    "redisCapacity": {
                        "type": "int",
                        "allowedValues": [0,1,2,3,4,5,6],
                        "defaultValue": 1
                    },
                    "redisFamily": {
                        "type": "string",
                        "allowedValues": ["C","P"],
                        "defaultValue": "C"
                    },
                    "redisTier": {
                        "type": "string",
                        "allowedValues": ["Standard","Basic","Premium"],
                        "defaultValue": "Standard"
                    },
                    "redisMaxMemoryPolicy": {
                        "type": "string",
                        "allowedValues": ["allkeys-lru","volatile-lru","noeviction","allkeys-lfu","volatile-lfu"],
                        "defaultValue": "allkeys-lru"
                    }
                },
                "resources": [
                    {
                        "apiVersion": "2022-06-01",
                        "type": "Microsoft.Cache/Redis",
                        "name": "[parameters('redisName')]",
                        "location": "[parameters('location')]",
                        "properties": {
                            "sku": {
                                "name": "[parameters('redisTier')]",
                                "family": "[parameters('redisFamily')]",
                                "capacity": "[parameters('redisCapacity')]"
                            },
                            "redisConfiguration": {
                                "maxmemory-policy": "[parameters('redisMaxMemoryPolicy')]"
                            }
                        }
                    }
                  ]
                }
              }
          },
          "roleDefinitionIds": [
            "/providers/Microsoft.Authorization/roleDefinitions/11111111-2222-3333-4444-555555555555"
          ]
        }
      }
    },
    "parameters": {
      "effect": {
        "type": "String",
        "metadata": {
          "displayName": "Effect",
          "description": "Choose Audit or Deny for the effect of this policy"
        },
        "allowedValues": [
          "Audit",
          "DeployIfNotExists"
        ]
      }
    }
  }

内容的提问来源于stack exchange,提问作者aberdeen angus

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 01:50:16