gpt4 book ai didi

azure - 地形 : Create an Azure Policy Initiative with multiple custom Azure Policies with Parameters

转载 作者:行者123 更新时间:2023-12-03 06:36:25 44 4
gpt4 key购买 nike

我想创建一个包含多个自定义 Azure 策略的 Azure 策略计划

我有以下自定义政策

# Azure Provider source and version being used
terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "~> 3.30.0"
}
}
}

# Configure the Microsoft Azure Provider
provider "azurerm" {
features {}
}

// Policy Category
variable "policy_definition_category" {
type = string
description = "The category to use for all PolicySet defintions"
default = "Custom"
}

// Policy Definition for Role Audit
resource "azurerm_policy_definition" "auditRoleAssignmentType" {
name = "auditRoleAssignmentType"
policy_type = "Custom"
mode = "All"
display_name = "Audit user role assignments"
description = "This policy checks for any Role Assignments of Type [User] - useful to catch individual IAM assignments to resources/RGs which are out of compliance with the RBAC standards e.g. using Groups for RBAC."

metadata = <<METADATA
{
"category": "${var.policy_definition_category}",
"version" : "1.0.0"
}
METADATA

policy_rule = <<POLICY_RULE
{
"if": {
"allOf": [
{
"field": "type",
"equals": "Microsoft.Authorization/roleAssignments"
},
{
"field": "Microsoft.Authorization/roleAssignments/principalType",
"equals": "[parameters('principalType')]"
}
]
},
"then": {
"effect": "audit"
}
}
POLICY_RULE

parameters = <<PARAMETERS
{
"principalType": {
"type": "String",
"metadata": {
"displayName": "principalType",
"description": "Which principalType to audit against e.g. 'User'"
},
"allowedValues": [
"User",
"Group",
"ServicePrincipal"
],
"defaultValue": "User"
}
}
PARAMETERS

}

// Display the Policy ID
output "auditRoleAssignmentType" {
value = "${azurerm_policy_definition.auditRoleAssignmentType.id}"
description = "The policy definition id for auditRoleAssignmentType"
}

我有以下倡议定义

// List of Policy Definitions for the Custom Initiative
variable "list_of_policies_definitions" {
type = list
description = "List of policy definitions"
default = [
"auditRoleAssignmentType"
]
}

// Get the list of Policies
data "azurerm_policy_definition" "custom_policies_definitions" {
count = length(var.list_of_policies_definitions)
display_name = var.list_of_policies_definitions[count.index]

depends_on = [
azurerm_policy_definition.auditRoleAssignmentType
]
}

// Define parameters for the Custom Initiative
variable "custom_initiative_parameters" {
type = list
description = "List of policy definitions"
default = [
<<PARAMETERS
{
"principalType": {
"value": "User"
}
}
PARAMETERS,
<<PARAMETERS
{
"paramfornextpolicy": {
"value": "valuefornextpolicy"
}
}
PARAMETERS
]
}

// Initiative Category
variable "policyset_definition_category" {
type = string
description = "The category to use for all PolicySet defintions"
default = "Custom"
}

// Initiative Definition
resource "azurerm_policy_set_definition" "custom_initiative" {

name = "custom_initiative"
policy_type = "Custom"
display_name = "Custom Initiative"
description = "Contains common policies"

metadata = <<METADATA
{
"category": "${var.policyset_definition_category}"
}
METADATA

dynamic "policy_definition_reference" {
for_each = data.azurerm_policy_definition.custom_policies_definitions
content {
policy_definition_id = policy_definition_reference.value["id"]
reference_id = policy_definition_reference.value["id"]
parameters = var.custom_initiative_parameters["id"]
}
}

depends_on = [
data.azurerm_policy_definition.custom_policies_definitions
]
}

我收到以下错误

│ Error: Unsupported argument ││ on main.tf line 151, in resource"azurerm_policy_set_definition" "custom_initiative": │
151: parameters = var.custom_initiative_parameters["id"] │
│ An argument named "parameters" is not expected here.

我知道应该使用“parameter_values”,但我不确定如何使用?

最佳答案

这我怎么你Pass parameter_values :

您可以传递parameter_values,如下所示:

PARAMETERS
policy_definition_reference {
policy_definition_id = azurerm_policy_definition.test.id
parameter_values = jsonencode({ //"`jsonencode` -> Converts into json parameter file"
allowedLocations = { value: "[parameters('allowedLocations')]"}
})
}
}

添加值后,我修改并尝试创建自定义策略定义以查看是否给出了参数值

它按预期为我工作,并在下面添加了 terraform 代码:​​

vi main.tf:

terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "3.35.0"
}
}
}
provider "azurerm" {
features {}
}
resource "azurerm_policy_definition" "example" {
name = "xxxxx"
policy_type = "Custom"
mode = "All"
display_name = "xxxx"
policy_rule = <<POLICY_RULE
{
"if": {
"not": {
"field": "location",
"in": "[parameters('allowedLocations')]"
}
},
"then": {
"effect": "audit"
}
}
POLICY_RULE
parameters = <<PARAMETERS
{
"allowedLocations": {
"type": "Array",
"metadata": {
"description": "The list of allowed locations for resources.",
"displayName": "Allowed locations",
"strongType": "location"
}
}
}
PARAMETERS
}
resource "azurerm_policy_set_definition" "example" {
name = "xxxx-example"
policy_type = "Custom"
display_name = "xxxxxx-example"
parameters = <<PARAMETERS
{
"allowedLocations": {
"type": "Array",
"metadata": {
"description": "List of allowed locations.",
"displayName": "Allowed locations",
"strongType": "location"
}
}
}
PARAMETERS
policy_definition_reference {
policy_definition_id = azurerm_policy_definition.example.id
parameter_values = jsonencode({
allowedLocations = { value: "[parameters('allowedLocations')]"}
})
}
}

地形计划:

enter image description here

地形应用:

enter image description here

在门户中添加的策略定义以及参数值:

enter image description here

关于azure - 地形 : Create an Azure Policy Initiative with multiple custom Azure Policies with Parameters,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74813834/

44 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com