| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
from __future__ import absolute_import, division, print_function |
| 9 |
__metaclass__ = type |
| 10 |
|
| 11 |
|
| 12 |
DOCUMENTATION = ''' |
| 13 |
--- |
| 14 |
module: azure_rm_roleassignment_info |
| 15 |
version_added: "0.1.2" |
| 16 |
short_description: Gets Azure Role Assignment facts |
| 17 |
description: |
| 18 |
- Gets facts of Azure Role Assignment. |
| 19 |
|
| 20 |
options: |
| 21 |
assignee: |
| 22 |
description: |
| 23 |
- Object id of a user, group or service principal. |
| 24 |
- Mutually exclusive with I(name) and I(id). |
| 25 |
type: str |
| 26 |
aliases: |
| 27 |
- assignee_object_id |
| 28 |
id: |
| 29 |
description: |
| 30 |
- Fqid of role assignment to look up. |
| 31 |
- If set, I(role_definition_id) and I(scope) will be silently ignored. |
| 32 |
- Mutually exclusive with I(assignee) and I(name). |
| 33 |
type: str |
| 34 |
name: |
| 35 |
description: |
| 36 |
- Name of role assignment. |
| 37 |
- Requires that I(scope) also be set. |
| 38 |
- Mutual exclusive with I(assignee) and I(id). |
| 39 |
type: str |
| 40 |
role_definition_id: |
| 41 |
description: |
| 42 |
- Resource id of role definition. |
| 43 |
type: str |
| 44 |
scope: |
| 45 |
description: |
| 46 |
- The scope to query for role assignments. |
| 47 |
- For example, use /subscriptions/{subscription-id}/ for a subscription. |
| 48 |
- /subscriptions/{subscription-id}/resourceGroups/{resourcegroup-name} for a resource group. |
| 49 |
- /subscriptions/{subscription-id}/resourceGroups/{resourcegroup-name}/providers/{resource-provider}/{resource-type}/{resource-name} for a resource. |
| 50 |
- By default will return all inhereted assignments from parent scopes, see I(strict_scope_match). |
| 51 |
type: str |
| 52 |
strict_scope_match: |
| 53 |
description: |
| 54 |
- If strict_scope_match is True, role assignments will only be returned for the exact scope defined. |
| 55 |
- Inherited role assignments will be excluded from results. |
| 56 |
- Option will be silently ignored if no scope is provided. |
| 57 |
type: bool |
| 58 |
default: False |
| 59 |
|
| 60 |
extends_documentation_fragment: |
| 61 |
- azure.azcollection.azure |
| 62 |
|
| 63 |
author: |
| 64 |
- Yunge Zhu(@yungezz) |
| 65 |
- Paul Aiton(@paultaiton) |
| 66 |
|
| 67 |
''' |
| 68 |
|
| 69 |
EXAMPLES = ''' |
| 70 |
- name: Get role assignments for specific service principal |
| 71 |
azure_rm_roleassignment_info: |
| 72 |
assignee: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx |
| 73 |
|
| 74 |
- name: Get role assignments for specific scope that matches specific role definition |
| 75 |
azure_rm_roleassignment_info: |
| 76 |
scope: /subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx |
| 77 |
role_definition_id: /subscriptions/xxx-sub-guid-xxx/providers/Microsoft.Authorization/roleDefinitions/xxx-role-guid-xxxx |
| 78 |
|
| 79 |
- name: Get role assignments for specific scope with no inherited assignments |
| 80 |
azure_rm_roleassignment_info: |
| 81 |
scope: /subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx |
| 82 |
strict_scope_match: true |
| 83 |
|
| 84 |
- name: Get role assignments by name |
| 85 |
azure_rm_roleassignment_info: |
| 86 |
scope: /subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx |
| 87 |
name: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx |
| 88 |
|
| 89 |
- name: Get role assignments by id |
| 90 |
azure_rm_roleassignment_info: |
| 91 |
id: /subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/providers/Microsoft.Authorization/roleAssignments/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx |
| 92 |
''' |
| 93 |
|
| 94 |
RETURN = ''' |
| 95 |
roleassignments: |
| 96 |
description: |
| 97 |
- List of role assignments. |
| 98 |
returned: always |
| 99 |
type: complex |
| 100 |
contains: |
| 101 |
id: |
| 102 |
description: |
| 103 |
- Id of role assignment. |
| 104 |
type: str |
| 105 |
returned: always |
| 106 |
sample: "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/providers/Microsoft.Authorization/roleAssignments/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" |
| 107 |
name: |
| 108 |
description: |
| 109 |
- Name of role assignment. |
| 110 |
type: str |
| 111 |
returned: always |
| 112 |
sample: myRoleAssignment |
| 113 |
type: |
| 114 |
description: |
| 115 |
- Type of role assignment. |
| 116 |
type: str |
| 117 |
returned: always |
| 118 |
sample: custom |
| 119 |
principal_id: |
| 120 |
description: |
| 121 |
- Principal Id of the role assigned to. |
| 122 |
type: str |
| 123 |
returned: always |
| 124 |
sample: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx |
| 125 |
principal_type: |
| 126 |
description: |
| 127 |
- Principal type of the role assigned to. |
| 128 |
type: str |
| 129 |
returned: always |
| 130 |
sample: ServicePrincipal |
| 131 |
role_definition_id: |
| 132 |
description: |
| 133 |
- Role definition id that was assigned to principal_id. |
| 134 |
type: str |
| 135 |
returned: always |
| 136 |
sample: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx |
| 137 |
scope: |
| 138 |
description: |
| 139 |
- The role assignment scope. |
| 140 |
type: str |
| 141 |
returned: always |
| 142 |
sample: /subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx |
| 143 |
''' |
| 144 |
|
| 145 |
try: |
| 146 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase |
| 147 |
except ImportError: |
| 148 |
|
| 149 |
pass |
| 150 |
|
| 151 |
|
| 152 |
class AzureRMRoleAssignmentInfo(AzureRMModuleBase): |
| 153 |
|
| 154 |
def __init__(self): |
| 155 |
self.module_arg_spec = dict( |
| 156 |
assignee=dict(type='str', aliases=['assignee_object_id']), |
| 157 |
id=dict(type='str'), |
| 158 |
name=dict(type='str'), |
| 159 |
role_definition_id=dict(type='str'), |
| 160 |
scope=dict(type='str'), |
| 161 |
strict_scope_match=dict(type='bool', default=False) |
| 162 |
) |
| 163 |
|
| 164 |
self.assignee = None |
| 165 |
self.id = None |
| 166 |
self.name = None |
| 167 |
self.role_definition_id = None |
| 168 |
self.scope = None |
| 169 |
self.strict_scope_match = None |
| 170 |
|
| 171 |
self.results = dict( |
| 172 |
changed=False, |
| 173 |
roleassignments=[] |
| 174 |
) |
| 175 |
|
| 176 |
mutually_exclusive = [['name', 'assignee', 'id']] |
| 177 |
|
| 178 |
super(AzureRMRoleAssignmentInfo, self).__init__(derived_arg_spec=self.module_arg_spec, |
| 179 |
supports_check_mode=True, |
| 180 |
supports_tags=False, |
| 181 |
facts_module=True, |
| 182 |
mutually_exclusive=mutually_exclusive) |
| 183 |
|
| 184 |
def exec_module(self, **kwargs): |
| 185 |
"""Main module execution method""" |
| 186 |
|
| 187 |
for key in self.module_arg_spec: |
| 188 |
setattr(self, key, kwargs[key]) |
| 189 |
|
| 190 |
if self.id: |
| 191 |
self.results['roleassignments'] = self.get_by_id() |
| 192 |
elif self.name and self.scope: |
| 193 |
self.results['roleassignments'] = self.get_by_name() |
| 194 |
elif self.name and not self.scope: |
| 195 |
self.fail("Parameter Error: Name requires a scope to also be set.") |
| 196 |
elif self.scope: |
| 197 |
self.results['roleassignments'] = self.list_by_scope() |
| 198 |
elif self.assignee: |
| 199 |
self.results['roleassignments'] = self.list_by_assignee() |
| 200 |
else: |
| 201 |
self.results['roleassignments'] = self.list_assignments() |
| 202 |
|
| 203 |
return self.results |
| 204 |
|
| 205 |
def get_by_id(self): |
| 206 |
''' |
| 207 |
Gets the role assignments by specific assignment id. |
| 208 |
|
| 209 |
:return: deserialized role assignment dictionary |
| 210 |
''' |
| 211 |
self.log("Lists role assignment by id {0}".format(self.id)) |
| 212 |
|
| 213 |
results = [] |
| 214 |
try: |
| 215 |
response = [self.authorization_client.role_assignments.get_by_id(role_id=self.id)] |
| 216 |
response = [self.roleassignment_to_dict(a) for a in response] |
| 217 |
results = response |
| 218 |
|
| 219 |
except Exception as ex: |
| 220 |
self.log("Didn't find role assignments id {0}".format(self.scope)) |
| 221 |
|
| 222 |
return results |
| 223 |
|
| 224 |
def get_by_name(self): |
| 225 |
''' |
| 226 |
Gets the properties of the specified role assignment by name. |
| 227 |
|
| 228 |
:return: deserialized role assignment dictionary |
| 229 |
''' |
| 230 |
self.log("Gets role assignment {0} by name".format(self.name)) |
| 231 |
|
| 232 |
results = [] |
| 233 |
|
| 234 |
try: |
| 235 |
response = [self.authorization_client.role_assignments.get(scope=self.scope, role_assignment_name=self.name)] |
| 236 |
response = [self.roleassignment_to_dict(a) for a in response] |
| 237 |
|
| 238 |
|
| 239 |
if self.role_definition_id: |
| 240 |
response = [role_assignment for role_assignment in response if (role_assignment.get('role_definition_id').split('/')[-1].lower() |
| 241 |
== self.role_definition_id.split('/')[-1].lower())] |
| 242 |
|
| 243 |
results = response |
| 244 |
|
| 245 |
except Exception as ex: |
| 246 |
self.log("Didn't find role assignment {0} in scope {1}".format(self.name, self.scope)) |
| 247 |
|
| 248 |
return results |
| 249 |
|
| 250 |
def list_by_assignee(self): |
| 251 |
''' |
| 252 |
Gets the role assignments by assignee. |
| 253 |
|
| 254 |
:return: deserialized role assignment dictionary |
| 255 |
''' |
| 256 |
self.log("Gets role assignment {0} by name".format(self.name)) |
| 257 |
|
| 258 |
filter = "principalId eq '{0}'".format(self.assignee) |
| 259 |
return self.list_assignments(filter=filter) |
| 260 |
|
| 261 |
def list_assignments(self, filter=None): |
| 262 |
''' |
| 263 |
Returns a list of assignments. |
| 264 |
''' |
| 265 |
results = [] |
| 266 |
|
| 267 |
try: |
| 268 |
response = list(self.authorization_client.role_assignments.list(filter=filter)) |
| 269 |
response = [self.roleassignment_to_dict(a) for a in response] |
| 270 |
|
| 271 |
|
| 272 |
if self.role_definition_id: |
| 273 |
response = [role_assignment for role_assignment in response if (role_assignment.get('role_definition_id').split('/')[-1].lower() |
| 274 |
== self.role_definition_id.split('/')[-1].lower())] |
| 275 |
|
| 276 |
results = response |
| 277 |
|
| 278 |
except Exception as ex: |
| 279 |
self.log("Didn't find role assignments in subscription {0}.".format(self.subscription_id)) |
| 280 |
|
| 281 |
return results |
| 282 |
|
| 283 |
def list_by_scope(self): |
| 284 |
''' |
| 285 |
Lists the role assignments by specific scope. |
| 286 |
|
| 287 |
:return: deserialized role assignment dictionary |
| 288 |
''' |
| 289 |
self.log("Lists role assignment by scope {0}".format(self.scope)) |
| 290 |
|
| 291 |
results = [] |
| 292 |
try: |
| 293 |
|
| 294 |
response = list(self.authorization_client.role_assignments.list_for_scope(scope=self.scope, filter='atScope()')) |
| 295 |
|
| 296 |
response = [self.roleassignment_to_dict(role_assignment) for role_assignment in response] |
| 297 |
|
| 298 |
|
| 299 |
if self.assignee: |
| 300 |
response = [role_assignment for role_assignment in response if role_assignment.get('principal_id').lower() == self.assignee.lower()] |
| 301 |
|
| 302 |
|
| 303 |
if self.strict_scope_match: |
| 304 |
response = [role_assignment for role_assignment in response if role_assignment.get('scope').lower() == self.scope.lower()] |
| 305 |
|
| 306 |
|
| 307 |
if self.role_definition_id: |
| 308 |
response = [role_assignment for role_assignment in response if (role_assignment.get('role_definition_id').split('/')[-1].lower() |
| 309 |
== self.role_definition_id.split('/')[-1].lower())] |
| 310 |
|
| 311 |
results = response |
| 312 |
|
| 313 |
except Exception as ex: |
| 314 |
self.log("Didn't find role assignments at scope {0}".format(self.scope)) |
| 315 |
|
| 316 |
return results |
| 317 |
|
| 318 |
def roleassignment_to_dict(self, assignment): |
| 319 |
return dict( |
| 320 |
assignee_object_id=assignment.principal_id, |
| 321 |
id=assignment.id, |
| 322 |
name=assignment.name, |
| 323 |
principal_id=assignment.principal_id, |
| 324 |
principal_type=assignment.principal_type, |
| 325 |
role_definition_id=assignment.role_definition_id, |
| 326 |
scope=assignment.scope, |
| 327 |
type=assignment.type |
| 328 |
) |
| 329 |
|
| 330 |
|
| 331 |
def main(): |
| 332 |
"""Main execution""" |
| 333 |
AzureRMRoleAssignmentInfo() |
| 334 |
|
| 335 |
|
| 336 |
if __name__ == '__main__': |
| 337 |
main() |