You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: articles/governance/policy/assign-policy-terraform.md
+42-41Lines changed: 42 additions & 41 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -12,7 +12,7 @@ This quickstart steps you through the process of creating a policy assignment to
12
12
machines that aren't using managed disks.
13
13
14
14
At the end of this process, you'll successfully identify virtual machines that aren't using managed
15
-
disks. They're _non-compliant_ with the policy assignment.
15
+
disks across subscription. They're _non-compliant_ with the policy assignment.
16
16
17
17
## Prerequisites
18
18
@@ -37,54 +37,55 @@ for Azure Policy use the
37
37
38
38
1. Create a new folder named `policy-assignment` and change directories into it.
39
39
40
-
1. Create `main.tf` with the following code:
41
-
42
-
```hcl
43
-
provider "azurerm" {
44
-
features {}
45
-
}
46
-
47
-
terraform {
48
-
required_providers {
49
-
azurerm = {
50
-
source = "hashicorp/azurerm"
51
-
version = ">= 2.96.0"
52
-
}
53
-
}
54
-
}
40
+
2. Create `main.tf` with the following code:
41
+
42
+
> [!NOTE]
43
+
> To create a Policy Assignment at a Management Group use the [azurerm_management_group_policy_assignment](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/management_group_policy_assignment) resource, for a Resource Group use the [azurerm_resource_group_policy_assignment](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/resource_group_policy_assignment) and for a Subscription use the [azurerm_subscription_policy_assignment](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/subscription_policy_assignment) resource.
description = "Shows all virtual machines not using managed disks"
61
-
display_name = "Audit VMs without managed disks assignment"
70
+
```terraform
71
+
variable "cust_scope" {
72
+
default = "{scope}"
62
73
}
63
74
```
64
75
65
-
1. Create `variables.tf` with the following code:
66
-
67
-
```hcl
68
-
variable "cust_scope" {
69
-
default = "{scope}"
70
-
}
71
-
```
72
-
73
-
A scope determines what resources or grouping of resources the policy assignment gets enforced
74
-
on. It could range from a management group to an individual resource. Be sure to replace
75
-
`{scope}` with one of the following patterns:
76
+
A scope determines what resources or grouping of resources the policy assignment gets enforced on. It could range from a management group to an individual resource. Be sure to replace `{scope}` with one of the following patterns based on the declared resource:
value = azurerm_resource_policy_assignment.auditvms.id
86
-
}
87
-
```
84
+
```terraform
85
+
output "assignment_id" {
86
+
value = azurerm_resource_policy_assignment.auditvms.id
87
+
}
88
+
```
88
89
89
90
## Initialize Terraform and create plan
90
91
@@ -140,11 +141,11 @@ returned.
140
141
## Identify non-compliant resources
141
142
142
143
To view the resources that aren't compliant under this new assignment, use the _assignment\_id_
143
-
returned by `terraform apply`. With it, run the following command to get the resource IDs of the
144
+
returned by ```terraform apply```. With it, run the following command to get the resource IDs of the
144
145
non-compliant resources that are output into a JSON file:
145
146
146
147
```console
147
-
armclient post "/subscriptions/<subscriptionID>/resourceGroups/<rgName>/providers/Microsoft.PolicyInsights/policyStates/latest/queryResults?api-version=2019-10-01&$filter=IsCompliant eq false and PolicyAssignmentId eq '<policyAssignmentID>'&$apply=groupby((ResourceId))" > <json file to direct the output with the resource IDs into>
148
+
armclient post "/subscriptions/<subscriptionID>/providers/Microsoft.PolicyInsights/policyStates/latest/queryResults?api-version=2019-10-01&$filter=IsCompliant eq false and PolicyAssignmentId eq '<policyAssignmentID>'&$apply=groupby((ResourceId))" > <json file to direct the output with the resource IDs into>
0 commit comments