【问题标题】:Azure Policy for NamingAzure 命名策略
【发布时间】:2021-08-16 20:30:19
【问题描述】:

关于我正在创建的 Azure 策略,我在这里做错了一些事情。尝试创建一个命名策略来阻止创建不匹配的资源(在本例中为资源组)。

{
    "properties": {
        "mode": "All",
        "displayName": "Company Naming Convention - Resource Groups",
        "description": "This policy governs the naming standard for resource groups and should be assigned at the resource group scope.  The naming scheme is rg-region-workload name-environment-optional instance number'.",
        "metadata": {
            "category": "Governance"
        },
        "policyRule": {
            "if": {
                "allOf": [
                    {
                        "field": "type",
                        "equals": "Microsoft.Resources/resourceGroups" 
                    },
                    {
                    "allOf": [
                        
                            {
                                "field": "name",
                                "notLike": "rg-useast-*"
                            },
                            {
                                "field": "name",
                                "notLike": "rg-useast2-*"
                            },
                            {
                                "field": "name",
                                "notLike": "rg-uscentral-*"
                            },
                            {
                                "field": "name",
                                "notLike": "rg-uksouth-*"
                            }
                        
                        ]
                    },
                    {
                        "allOf": [
                            {
                                "field": "name",
                                "notLike": "*-production.###"
                            },
                            {
                                "field": "name",
                                "notLike": "*-development.###"
                            },
                            {
                                "field": "name",
                                "notLike": "*-qualityassurance.###"
                            },
                            {
                                "field": "name",
                                "notLike": "*-testing.###"
                            }
                        ]
                    }
                ]
            },
            "then": {
                "effect": "deny"
            }
        }
    }
}

我还想创建一个策略来审核与此名称不匹配的现有资源,但我可以稍后解决这个问题。有人建议我做错了什么或更好的解决方法吗?

【问题讨论】:

  • 那么当您实施此政策时会发生什么?它不会阻止创建命名错误的资源组吗?
  • 不,恐怕不会。我之前测试时完全没有做任何事情。

标签: json azure azure-policy


【解决方案1】:

好的...所以原始政策效果很好...如果我真的查看了正确的资源。应该是“Microsoft.Resources/subscriptions/resourceGroups”而不是“Microsoft.Resources/resourceGroups”。该死的,我觉得自己像个白痴……

【讨论】:

    【解决方案2】:

    您在 Azure 策略定义中使用了不正确的语法。 allOf 语法要求所有条件都为真,您可以将所有条件保留在单个 allOf 运算符中。

    政策修改版供参考:

    {
        "properties": {
            "mode": "All",
            "displayName": "Company Naming Convention - Resource Groups",
            "description": "This policy governs the naming standard for resource groups and should be assigned at the resource group scope.  The naming scheme is rg-region-workload name-environment-optional instance number'.",
            "metadata": {
                "category": "Governance"
            },
            "policyRule": {
                "if": {
                    "allOf": [
                        {
                            "field": "type",
                            "equals": "Microsoft.Resources/resourceGroups"
                        },
                        {
                            "field": "name",
                            "notLike": "rg-useast-*"
                        },
                        {
                            "field": "name",
                            "notLike": "rg-useast2-*"
                        },
                        {
                            "field": "name",
                            "notLike": "rg-uscentral-*"
                        },
                        {
                            "field": "name",
                            "notLike": "rg-uksouth-*"
                        },
                        {
                            "field": "name",
                            "notLike": "*-production.###"
                        },
                        {
                            "field": "name",
                            "notLike": "*-development.###"
                        },
                        {
                            "field": "name",
                            "notLike": "*-qualityassurance.###"
                        },
                        {
                            "field": "name",
                            "notLike": "*-testing.###"
                        }
                    ]
                },
                "then": {
                    "effect": "deny"
                }
            }
        }
    }  
    

    另请注意,将分配应用于定义的范围大约需要 30 分钟。有关 Azure Policy 评估周期的详细信息,请参阅this document

    对于按需评估,请使用 az cli 命令:az policy state trigger-scan

    【讨论】:

    • 让我试一试,然后回复你。
    • 似乎仍然无法正常工作,但这项政策也不会完全按照我的意愿行事。从理论上讲,有人可以只使用命名约定的前半部分,但不能使用此策略的后半部分。
    猜你喜欢
    • 2023-03-21
    • 1970-01-01
    • 2020-02-21
    • 2021-09-05
    • 2017-01-02
    • 1970-01-01
    • 2019-04-16
    • 2016-01-11
    • 1970-01-01
    相关资源
    最近更新 更多