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
# Quickstart: Create a policy assignment to identify non-compliant resources by using a Bicep file
10
10
11
-
In this quickstart, you use a Bicep file to create a policy assignment that validates resource's compliance with an Azure policy. The policy is assigned to a resource group scope and audits if virtual machines use managed disks. Virtual machines deployed in the resource group that don't use managed disks are _non-compliant_ with the policy assignment.
11
+
In this quickstart, you use a Bicep file to create a policy assignment that validates resource's compliance with an Azure policy. The policy is assigned to a resource group and audits virtual machines that don't use managed disks. After you create the policy assignment, you identify non-compliant virtual machines.
-[Azure PowerShell](/powershell/azure/install-az-ps) or [Azure CLI](/cli/azure/install-azure-cli).
23
20
-[Visual Studio Code](https://code.visualstudio.com/) and the [Bicep extension for Visual Studio Code](https://marketplace.visualstudio.com/items?itemName=ms-azuretools.vscode-bicep).
24
-
-`Microsoft.PolicyInsights` must be [registered](../../azure-resource-manager/management/resource-providers-and-types.md) in your Azure subscription.
21
+
-`Microsoft.PolicyInsights` must be [registered](../../azure-resource-manager/management/resource-providers-and-types.md) in your Azure subscription. To register a resource provider, you must have permission to register resource providers. That permission is included in the Contributor and Owner roles.
22
+
- A resource group with at least one virtual machine that doesn't use managed disks.
25
23
26
24
## Review the Bicep file
27
25
@@ -31,7 +29,7 @@ Create the following Bicep file as _policy-assignment.bicep_.
31
29
32
30
1. Open Visual Studio Code and select **File** > **New Text File**.
33
31
1. Copy and paste the Bicep file into Visual Studio Code.
34
-
1. Select **File** > **Save** and use the filename _policy-policy-assignment.bicep_.
32
+
1. Select **File** > **Save** and use the filename _policy-assignment.bicep_.
The resource type defined in the Bicep file is [Microsoft.Authorization/policyAssignments](/azure/templates/microsoft.authorization/policyassignments).
56
+
The resource type defined in the Bicep file is [Microsoft.Authorization/policyAssignments](/azure/templates/microsoft.authorization/policyassignments). The Bicep file creates a policy assignment named _audit-vm-managed-disks_.
59
57
60
58
For more information about Bicep files:
61
59
@@ -91,122 +89,236 @@ az account set --subscription <subscriptionID>
91
89
92
90
---
93
91
94
-
The following commands create a resource group and deploy the policy definition.
92
+
You can verify if `Microsoft.PolicyInsights` is registered. If it isn't, you can run a command to register the resource provider.
The `$rg` variable stores properties for the resource group. The `$deployparms` variable uses [splatting](/powershell/module/microsoft.powershell.core/about/about_splatting) to create parameter values and improve readability. The `New-AzResourceGroupDeployment` command uses the parameter values defined in the `$deployparms` variable.
133
+
134
+
-`Name` is the deployment name displayed in the output and in Azure for the resource group's deployments.
135
+
-`ResourceGroupName` uses the `$rg.ResourceGroupName` property to get the name of your resource group where the policy is assigned.
136
+
-`TemplateFile` specifies the Bicep file's name and location on your local computer.
137
+
138
+
# [Azure CLI](#tab/azure-cli)
139
+
140
+
```azurecli
141
+
rgname=$(az group show --resource-group <resourceGroupName> --query name --output tsv)
111
142
112
143
az deployment group create \
113
144
--name PolicyDeployment \
114
-
--resource-group PolicyGroup \
145
+
--resource-group $rgname \
115
146
--template-file policy-assignment.bicep
116
147
```
117
148
149
+
The `rgname` variable uses an expression to get your resource group's name used in the deployment command. The Azure CLI commands use a backslash (`\`) for line continuation to improve readability.
150
+
151
+
-`name` is the deployment name displayed in the output and in Azure for the resource group's deployments.
152
+
-`resource-group` is the name of your resource group where the policy is assigned.
153
+
-`template-file` specifies the Bicep file's name and location on your local computer.
154
+
118
155
---
119
156
120
-
The Bicep file outputs the policy `assignmentId`. You create a variable for the policy assignment ID in the commands that validate the deployment.
157
+
You can verify the policy assignment's deployment with the following command:
158
+
159
+
# [PowerShell](#tab/azure-powershell)
160
+
161
+
The command uses the `$rg.ResourceId` property to get the resource group's ID.
The `rgid` variable uses an expression to get the resource group's ID used to show the policy assignment.
181
+
182
+
```azurecli
183
+
rgid=$(az group show --resource-group $rgname --query id --output tsv)
123
184
124
-
After the policy assignment is deployed, virtual machines that are deployed to the _PolicyGroup_ resource group are audited for compliance with the managed disk policy.
185
+
az policy assignment show --name "audit-vm-managed-disks" --scope $rgid
186
+
```
125
187
126
-
1. Sign in to [Azure portal](https://portal.azure.com)
127
-
1. Go to **Policy** and select **Compliance** on the left side of the page.
128
-
1. Search for the _audit-vm-managed-disks_ policy assignment.
188
+
The output is verbose but resembles the following example:
189
+
190
+
```output
191
+
"description": "Policy assignment to resource group scope created with Bicep file",
"message": "Virtual machines should use managed disks",
207
+
"policyDefinitionReferenceId": null
208
+
}
209
+
]
210
+
```
129
211
130
-
The **Compliance state** for a new policy assignment is shown as **Not started** because it takes a few minutes to become active.
212
+
---
131
213
132
-
:::image type="content" source="./media/assign-policy-bicep/policy-compliance.png" alt-text="Screenshot of compliance details on the Policy Compliance page.":::
214
+
## Identify non-compliant resources
133
215
134
-
For more information, go to [How compliance works](./concepts/compliance-states.md).
216
+
After the policy assignment is deployed, virtual machines that are deployed to the resource group are audited for compliance with the managed disk policy.
135
217
136
-
You can also get the compliance state with Azure PowerShell or Azure CLI.
218
+
The compliance state for a new policy assignment takes a few minutes to become active and provide results about the policy's state.
The `$rg` variable stores the resource group's properties and `Get-AzPolicyAssignment` shows your policy assignment. The `$policyid` variable stores the policy assignment's resource ID, and `Get-AzPolicyStateSummary` shows the number of non-compliant resources and policies.
231
+
The `$complianceparms` variable creates parameter values used in the `Get-AzPolicyState` command.
232
+
233
+
-`ResourceGroupName` gets the resource group name from the `$rg.ResourceGroupName` property.
234
+
-`PolicyAssignmentName` specifies the name used when the policy assignment was created.
235
+
-`Filter` uses an expression to find resources that aren't compliant with the policy assignment.
236
+
237
+
Your results resemble the following example and `ComplianceState` shows `NonCompliant`:
rg=$(az group show --resource-group PolicyGroup --query id --output tsv)
156
-
az policy assignment show --name "audit-vm-managed-disks" --scope $rg
264
+
policyid=$(az policy assignment show \
265
+
--name "audit-vm-managed-disks" \
266
+
--scope $rgid \
267
+
--query id \
268
+
--output tsv)
157
269
158
-
# Shows the number of non-compliant resources and policies
159
-
policyid=$(az policy assignment show --name "audit-vm-managed-disks" --scope $rg --query id --output tsv)
160
-
az policy state summarize --resource $policyid
270
+
az policy state list --resource $policyid --filter "(isCompliant eq false)"
161
271
```
162
272
163
-
The `$rg` variable stores the resource group's properties and `az policy assignment show` displays your policy assignment. The `$policyid` variable stores the policy assignment's resource ID and `az policy state summarize` shows the number of non-compliant resources and policies.
273
+
The `policyid` variable uses an expression to get the policy assignment's ID.
164
274
165
-
---
275
+
The `filter` parameter limits the output to non-compliant resources.
166
276
167
-
## Clean up resources
168
-
169
-
To remove the assignment from Azure, follow these steps:
277
+
The `az policy state list` output is verbose, but for this article the `complianceState` shows `NonCompliant`.
170
278
171
-
1. Select **Compliance** in the left side of the Azure Policy page.
172
-
1. Locate the _audit-vm-managed-disks_ policy assignment.
173
-
1. Right-click the _audit-vm-managed-disks_ policy assignment and select **Delete
174
-
assignment**.
175
-
176
-
:::image type="content" source="./media/assign-policy-bicep/delete-assignment.png" alt-text="Screenshot of the context menu to delete an assignment from the Policy Compliance page.":::
279
+
```output
280
+
"complianceState": "NonCompliant",
281
+
"components": null,
282
+
"effectiveParameters": "",
283
+
"isCompliant": false,
284
+
```
177
285
178
-
1. Delete the resource group _PolicyGroup_. Go to the Azure resource group and select **Delete resource group**.
179
-
1. Delete the _policy-assignment.bicep_ file.
286
+
---
180
287
181
-
You can also delete the policy assignment and resource group with Azure PowerShell or Azure CLI.
az policy assignment delete --name "audit-vm-managed-disks" --scope $rg
196
-
az group delete --name PolicyGroup
305
+
az policy assignment delete --name "audit-vm-managed-disks" --scope $rgid
306
+
```
307
+
308
+
To sign out of your Azure CLI session:
197
309
198
-
# Sign out of Azure
310
+
```azurecli
199
311
az logout
200
312
```
201
313
202
314
---
203
315
204
316
## Next steps
205
317
206
-
In this quickstart, you assigned a built-in policy definition to a resource group scope and reviewed its compliance report. The policy definition audits if the virtual machine resources in the resource group are compliant and identifies resources that aren't compliant.
318
+
In this quickstart, you assigned a built-in policy definition to a resource group scope and reviewed its compliance state. The policy definition audits if the virtual machines in the resource group are compliant and identifies resources that aren't compliant.
207
319
208
320
To learn more about assigning policies to validate that new resources are compliant, continue to the
209
321
tutorial.
210
322
211
323
> [!div class="nextstepaction"]
212
-
> [Creating and managing policies](./tutorials/create-and-manage.md)
324
+
> [Tutorial: Create and manage policies to enforce compliance](./tutorials/create-and-manage.md)
0 commit comments