1
1
---
2
2
title : " Quickstart: New policy assignment with Python"
3
3
description : In this quickstart, you use Python to create an Azure Policy assignment to identify non-compliant resources.
4
- ms.date : 08/17 /2021
4
+ ms.date : 10/01 /2021
5
5
ms.topic : quickstart
6
6
ms.custom : devx-track-python
7
7
---
@@ -54,6 +54,9 @@ Python can be used, including [bash on Windows 10](/windows/wsl/install-win10) o
54
54
55
55
# Add the CLI Core library for Python for authentication (development only!)
56
56
pip install azure-cli-core
57
+
58
+ # Add the Azure identity library for Python
59
+ pip install azure.identity
57
60
```
58
61
59
62
> [ !NOTE]
@@ -65,7 +68,7 @@ Python can be used, including [bash on Windows 10](/windows/wsl/install-win10) o
65
68
66
69
``` bash
67
70
# Check each installed library
68
- pip show azure-mgmt-policyinsights azure-mgmt-resource azure-cli-core
71
+ pip show azure-mgmt-policyinsights azure-mgmt-resource azure-cli-core azure.identity
69
72
```
70
73
71
74
## Create a policy assignment
@@ -78,16 +81,21 @@ Run the following code to create a new policy assignment:
78
81
79
82
``` python
80
83
# Import specific methods and models from other libraries
81
- from azure.common.credentials import get_azure_cli_credentials
82
- from azure.common.client_factory import get_client_from_cli_profile
83
84
from azure.mgmt.resource.policy import PolicyClient
84
- from azure.mgmt.resource.policy.models import PolicyAssignment
85
+ from azure.mgmt.resource.policy.models import PolicyAssignment, Identity, UserAssignedIdentitiesValue, PolicyAssignmentUpdate
86
+ from azure.identity import AzureCliCredential
87
+
88
+ # Set subscription
89
+ subId = " {subId} "
90
+ assignmentLocation = " westus2"
85
91
86
92
# Get your credentials from Azure CLI (development only!) and get your subscription list
87
- policyClient = get_client_from_cli_profile(PolicyClient)
93
+ credential = AzureCliCredential()
94
+ policyClient = PolicyClient(credential, subId, base_url = none)
88
95
89
96
# Create details for the assignment
90
- policyAssignmentDetails = PolicyAssignment(display_name = " Audit VMs without managed disks Assignment" , policy_definition_id = " /providers/Microsoft.Authorization/policyDefinitions/06a78e20-9358-41c9-923c-fb736d382a4d" , scope = " {scope} " , description = " Shows all virtual machines not using managed disks" )
97
+ policyAssignmentIdentity = Identity(type = " SystemAssigned" )
98
+ policyAssignmentDetails = PolicyAssignment(display_name = " Audit VMs without managed disks Assignment" , policy_definition_id = " /providers/Microsoft.Authorization/policyDefinitions/06a78e20-9358-41c9-923c-fb736d382a4d" , description = " Shows all virtual machines not using managed disks" , identity = policyAssignmentIdentity, location = assignmentLocation)
91
99
92
100
# Create new policy assignment
93
101
policyAssignment = policyClient.policy_assignments.create(" {scope} " , " audit-vm-manageddisks" , policyAssignmentDetails)
@@ -99,6 +107,8 @@ print(policyAssignment)
99
107
The preceding commands use the following information:
100
108
101
109
Assignment details:
110
+ - ** subId** - Your subscription. Needed for authentication. Replace ` {subId} ` with your
111
+ subscription.
102
112
- ** display_name** - Display name for the policy assignment. In this case, you're using _ Audit VMs
103
113
without managed disks Assignment_ .
104
114
- ** policy_definition_id** - The policy definition path, based on which you're using to create the
@@ -131,25 +141,29 @@ you created. Run the following code:
131
141
132
142
``` python
133
143
# Import specific methods and models from other libraries
134
- from azure.common.client_factory import get_client_from_cli_profile
135
144
from azure.mgmt.policyinsights._policy_insights_client import PolicyInsightsClient
136
145
from azure.mgmt.policyinsights.models import QueryOptions
146
+ from azure.identity import AzureCliCredential
147
+
148
+ # Set subscription
149
+ subId = " {subId} "
137
150
138
151
# Get your credentials from Azure CLI (development only!) and get your subscription list
139
- policyInsightsClient = get_client_from_cli_profile(PolicyInsightsClient)
152
+ credential = AzureCliCredential()
153
+ policyClient = PolicyInsightsClient(credential, subId, base_url = none)
140
154
141
155
# Set the query options
142
156
queryOptions = QueryOptions(filter = " IsCompliant eq false and PolicyAssignmentId eq 'audit-vm-manageddisks'" ,apply = " groupby((ResourceId))" )
143
157
144
158
# Fetch 'latest' results for the subscription
145
- results = policyInsightsClient.policy_states.list_query_results_for_subscription(policy_states_resource = " latest" , subscription_id = " {subscriptionId} " , query_options = queryOptions)
159
+ results = policyInsightsClient.policy_states.list_query_results_for_subscription(policy_states_resource = " latest" , subscription_id = subId , query_options = queryOptions)
146
160
147
161
# Show results
148
162
print (results)
149
163
```
150
164
151
- Replace ` {subscriptionId } ` with the subscription you want to see the compliance results for this
152
- policy assignment. For a list of other scopes and ways to summarize the data, see
165
+ Replace ` {subId } ` with the subscription you want to see the compliance results for this policy
166
+ assignment. For a list of other scopes and ways to summarize the data, see
153
167
[ Policy State methods] ( /python/api/azure-mgmt-policyinsights/azure.mgmt.policyinsights.operations.policystatesoperations#methods ) .
154
168
155
169
Your results resemble the following example:
@@ -174,11 +188,15 @@ To remove the assignment created, use the following command:
174
188
175
189
``` python
176
190
# Import specific methods and models from other libraries
177
- from azure.common.client_factory import get_client_from_cli_profile
178
191
from azure.mgmt.resource.policy import PolicyClient
192
+ from azure.identity import AzureCliCredential
193
+
194
+ # Set subscription
195
+ subId = " {subId} "
179
196
180
197
# Get your credentials from Azure CLI (development only!) and get your subscription list
181
- policyClient = get_client_from_cli_profile(PolicyClient)
198
+ credential = AzureCliCredential()
199
+ policyClient = PolicyClient(credential, subId, base_url = none)
182
200
183
201
# Delete the policy assignment
184
202
policyAssignment = policyClient.policy_assignments.delete(" {scope} " , " audit-vm-manageddisks" )
@@ -187,7 +205,8 @@ policyAssignment = policyClient.policy_assignments.delete("{scope}", "audit-vm-m
187
205
print (policyAssignment)
188
206
```
189
207
190
- Replace ` {scope} ` with the same scope you used to create the policy assignment.
208
+ Replace ` {subId} ` with your subscription and ` {scope} ` with the same scope you used to create the
209
+ policy assignment.
191
210
192
211
## Next steps
193
212
0 commit comments