| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
from __future__ import absolute_import, division, print_function |
| 8 |
|
| 9 |
__metaclass__ = type |
| 10 |
|
| 11 |
ANSIBLE_METADATA = { |
| 12 |
"metadata_version": "1.1", |
| 13 |
"status": ["preview"], |
| 14 |
"supported_by": "community", |
| 15 |
} |
| 16 |
|
| 17 |
DOCUMENTATION = r""" |
| 18 |
--- |
| 19 |
module: purefa_dsrole |
| 20 |
version_added: '1.0.0' |
| 21 |
short_description: Configure FlashArray Directory Service Roles |
| 22 |
description: |
| 23 |
- Set or erase directory services role configurations. |
| 24 |
author: |
| 25 |
- Pure Storage Ansible Team (@sdodsley) <[email protected]> |
| 26 |
options: |
| 27 |
name: |
| 28 |
description: |
| 29 |
- Name of role |
| 30 |
- If not providied, will be assigned to the same as I(role) |
| 31 |
type: str |
| 32 |
version_added: 1.32.0 |
| 33 |
state: |
| 34 |
description: |
| 35 |
- Create or delete directory service role |
| 36 |
type: str |
| 37 |
default: present |
| 38 |
choices: [ absent, present ] |
| 39 |
role: |
| 40 |
description: |
| 41 |
- The directory service role to work on |
| 42 |
type: str |
| 43 |
choices: [ array_admin, ops_admin, readonly, storage_admin ] |
| 44 |
group_base: |
| 45 |
type: str |
| 46 |
description: |
| 47 |
- Specifies where the configured group is located in the directory |
| 48 |
tree. This field consists of Organizational Units (OUs) that combine |
| 49 |
with the base DN attribute and the configured group CNs to complete |
| 50 |
the full Distinguished Name of the groups. The group base should |
| 51 |
specify OU= for each OU and multiple OUs should be separated by commas. |
| 52 |
The order of OUs is important and should get larger in scope from left |
| 53 |
to right. |
| 54 |
- Each OU should not exceed 64 characters in length. |
| 55 |
group: |
| 56 |
type: str |
| 57 |
description: |
| 58 |
- Sets the common Name (CN) of the configured directory service group |
| 59 |
containing users for the FlashBlade. This name should be just the |
| 60 |
Common Name of the group without the CN= specifier. |
| 61 |
- Common Names should not exceed 64 characters in length. |
| 62 |
context: |
| 63 |
description: |
| 64 |
- Name of fleet member on which to perform the operation. |
| 65 |
- This requires the array receiving the request is a member of a fleet |
| 66 |
and the context name to be a member of the same fleet. |
| 67 |
type: str |
| 68 |
default: "" |
| 69 |
version_added: '1.39.0' |
| 70 |
extends_documentation_fragment: |
| 71 |
- purestorage.flasharray.purestorage.fa |
| 72 |
""" |
| 73 |
|
| 74 |
EXAMPLES = r""" |
| 75 |
- name: Delete existing array_admin directory service role |
| 76 |
purestorage.flasharray.purefa_dsrole: |
| 77 |
role: array_admin |
| 78 |
state: absent |
| 79 |
fa_url: 10.10.10.2 |
| 80 |
api_token: e31060a7-21fc-e277-6240-25983c6c4592 |
| 81 |
|
| 82 |
- name: Create observability directory service role with readonly policy |
| 83 |
purestorage.flasharray.purefa_dsrole: |
| 84 |
name: observability |
| 85 |
role: readonly |
| 86 |
group_base: "OU=PureGroups,OU=ReadOnly" |
| 87 |
group: o11y |
| 88 |
fa_url: 10.10.10.2 |
| 89 |
api_token: e31060a7-21fc-e277-6240-25983c6c4592 |
| 90 |
|
| 91 |
- name: Update system-defined array_admin directory service role |
| 92 |
purestorage.flasharray.purefa_dsrole: |
| 93 |
role: array_admin |
| 94 |
group_base: "OU=PureGroups,OU=SANManagers" |
| 95 |
group: pureadmins |
| 96 |
fa_url: 10.10.10.2 |
| 97 |
api_token: e31060a7-21fc-e277-6240-25983c6c4592 |
| 98 |
|
| 99 |
- name: Update directory service role policy |
| 100 |
purestorage.flasharray.purefa_dsrole: |
| 101 |
name: observability |
| 102 |
role: ops_admin |
| 103 |
fa_url: 10.10.10.2 |
| 104 |
api_token: e31060a7-21fc-e277-6240-25983c6c4592 |
| 105 |
""" |
| 106 |
|
| 107 |
RETURN = r""" |
| 108 |
""" |
| 109 |
|
| 110 |
MIN_DSROLE_API_VERSION = "2.30" |
| 111 |
POLICY_API_VERSION = "2.36" |
| 112 |
CONTEXT_VERSION = "2.42" |
| 113 |
|
| 114 |
HAS_PYPURECLIENT = True |
| 115 |
try: |
| 116 |
from pypureclient.flasharray import ( |
| 117 |
DirectoryServiceRole, |
| 118 |
DirectoryServiceRolePost, |
| 119 |
Reference, |
| 120 |
ReferenceNoId, |
| 121 |
) |
| 122 |
except ImportError: |
| 123 |
HAS_PYPURECLIENT = False |
| 124 |
|
| 125 |
|
| 126 |
from ansible.module_utils.basic import AnsibleModule |
| 127 |
from ansible_collections.purestorage.flasharray.plugins.module_utils.purefa import ( |
| 128 |
get_array, |
| 129 |
purefa_argument_spec, |
| 130 |
) |
| 131 |
from ansible_collections.purestorage.flasharray.plugins.module_utils.version import ( |
| 132 |
LooseVersion, |
| 133 |
) |
| 134 |
|
| 135 |
|
| 136 |
def update_role(module, array): |
| 137 |
"""Update Directory Service Role""" |
| 138 |
changed = False |
| 139 |
api_version = array.get_rest_version() |
| 140 |
|
| 141 |
|
| 142 |
if module.params["state"] == "absent": |
| 143 |
if not module.check_mode: |
| 144 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 145 |
res = array.patch_directory_services_roles( |
| 146 |
names=[module.params["name"]], |
| 147 |
directory_service_roles=DirectoryServiceRole( |
| 148 |
group_base="", |
| 149 |
group="", |
| 150 |
), |
| 151 |
context_names=[module.params["context"]], |
| 152 |
) |
| 153 |
else: |
| 154 |
res = array.patch_directory_services_roles( |
| 155 |
names=[module.params["name"]], |
| 156 |
directory_service_roles=DirectoryServiceRole( |
| 157 |
group_base="", |
| 158 |
group="", |
| 159 |
), |
| 160 |
) |
| 161 |
if res.status_code != 200: |
| 162 |
module.fail_json( |
| 163 |
msg="Deleting system-defined Directory Service Role " |
| 164 |
"{0} failed.Error: {1}".format( |
| 165 |
module.params["name"], res.errors[0].message |
| 166 |
) |
| 167 |
) |
| 168 |
module.exit_json(changed=True) |
| 169 |
|
| 170 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 171 |
role = list( |
| 172 |
array.get_directory_services_roles( |
| 173 |
names=[module.params["name"]], context_names=[module.params["context"]] |
| 174 |
).items |
| 175 |
)[0] |
| 176 |
else: |
| 177 |
role = list( |
| 178 |
array.get_directory_services_roles(names=[module.params["name"]]).items |
| 179 |
)[0] |
| 180 |
if module.params["name"] not in [ |
| 181 |
"array_admin", |
| 182 |
"storage_admin", |
| 183 |
"ops_admin", |
| 184 |
"readonly", |
| 185 |
]: |
| 186 |
if ( |
| 187 |
getattr(role, "group_base", None) != module.params["group_base"] |
| 188 |
or getattr(role, "group", None) != module.params["group"] |
| 189 |
or role.role.name != module.params["role"] |
| 190 |
): |
| 191 |
changed = True |
| 192 |
if not module.check_mode: |
| 193 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 194 |
res = array.patch_directory_services_roles( |
| 195 |
names=[module.params["name"]], |
| 196 |
directory_service_roles=DirectoryServiceRole( |
| 197 |
group_base=module.params["group_base"], |
| 198 |
group=module.params["group"], |
| 199 |
role=Reference(name=module.params["role"]), |
| 200 |
), |
| 201 |
context_names=[module.params["context"]], |
| 202 |
) |
| 203 |
else: |
| 204 |
res = array.patch_directory_services_roles( |
| 205 |
names=[module.params["name"]], |
| 206 |
directory_service_roles=DirectoryServiceRole( |
| 207 |
group_base=module.params["group_base"], |
| 208 |
group=module.params["group"], |
| 209 |
role=Reference(name=module.params["role"]), |
| 210 |
), |
| 211 |
) |
| 212 |
if res.status_code != 200: |
| 213 |
module.fail_json( |
| 214 |
msg="Update Directory Service Role {0} failed.Error: {1}".format( |
| 215 |
module.params["name"], res.errors[0].message |
| 216 |
) |
| 217 |
) |
| 218 |
else: |
| 219 |
if ( |
| 220 |
getattr(role, "group_base", None) != module.params["group_base"] |
| 221 |
or getattr(role, "group", None) != module.params["group"] |
| 222 |
): |
| 223 |
changed = True |
| 224 |
if not module.check_mode: |
| 225 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 226 |
res = array.patch_directory_services_roles( |
| 227 |
names=[module.params["name"]], |
| 228 |
directory_service_roles=DirectoryServiceRole( |
| 229 |
group_base=module.params["group_base"], |
| 230 |
group=module.params["group"], |
| 231 |
), |
| 232 |
context_names=[module.params["context"]], |
| 233 |
) |
| 234 |
else: |
| 235 |
res = array.patch_directory_services_roles( |
| 236 |
names=[module.params["name"]], |
| 237 |
directory_service_roles=DirectoryServiceRole( |
| 238 |
group_base=module.params["group_base"], |
| 239 |
group=module.params["group"], |
| 240 |
), |
| 241 |
) |
| 242 |
if res.status_code != 200: |
| 243 |
module.fail_json( |
| 244 |
msg="Update Directory Service Role {0} failed.Error: {1}".format( |
| 245 |
module.params["name"], res.errors[0].message |
| 246 |
) |
| 247 |
) |
| 248 |
module.exit_json(changed=changed) |
| 249 |
|
| 250 |
|
| 251 |
def delete_role(module, array): |
| 252 |
"""Delete Directory Service Role""" |
| 253 |
changed = True |
| 254 |
api_version = array.get_rest_version() |
| 255 |
if not module.check_mode: |
| 256 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 257 |
res = array.delete_directory_services_roles( |
| 258 |
names=[module.params["name"]], context_names=[module.params["context"]] |
| 259 |
) |
| 260 |
else: |
| 261 |
res = array.delete_directory_services_roles(names=[module.params["name"]]) |
| 262 |
if res.status_code != 200: |
| 263 |
module.fail_json( |
| 264 |
msg="Delete Directory Service Role {0} failed. Error: {1}".format( |
| 265 |
module.params["name"], res.errors[0].message |
| 266 |
) |
| 267 |
) |
| 268 |
module.exit_json(changed=changed) |
| 269 |
|
| 270 |
|
| 271 |
def create_role(module, array): |
| 272 |
"""Create Directory Service Role""" |
| 273 |
changed = False |
| 274 |
api_version = array.get_rest_version() |
| 275 |
if not module.params["group"] == "" or not module.params["group_base"] == "": |
| 276 |
changed = True |
| 277 |
if not module.check_mode: |
| 278 |
if LooseVersion(api_version) >= LooseVersion(POLICY_API_VERSION): |
| 279 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 280 |
res = array.post_directory_services_roles( |
| 281 |
names=[module.params["name"]], |
| 282 |
directory_service_roles=DirectoryServiceRolePost( |
| 283 |
group_base=module.params["group_base"], |
| 284 |
group=module.params["group"], |
| 285 |
role=ReferenceNoId(name=module.params["role"]), |
| 286 |
), |
| 287 |
context_names=[module.params["context"]], |
| 288 |
) |
| 289 |
else: |
| 290 |
res = array.post_directory_services_roles( |
| 291 |
names=[module.params["name"]], |
| 292 |
directory_service_roles=DirectoryServiceRolePost( |
| 293 |
group_base=module.params["group_base"], |
| 294 |
group=module.params["group"], |
| 295 |
role=ReferenceNoId(name=module.params["role"]), |
| 296 |
), |
| 297 |
) |
| 298 |
else: |
| 299 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 300 |
res = array.post_directory_services_roles( |
| 301 |
names=[module.params["name"]], |
| 302 |
directory_service_roles=DirectoryServiceRole( |
| 303 |
group_base=module.params["group_base"], |
| 304 |
group=module.params["group"], |
| 305 |
), |
| 306 |
context_names=[module.params["context"]], |
| 307 |
) |
| 308 |
else: |
| 309 |
res = array.post_directory_services_roles( |
| 310 |
names=[module.params["name"]], |
| 311 |
directory_service_roles=DirectoryServiceRole( |
| 312 |
group_base=module.params["group_base"], |
| 313 |
group=module.params["group"], |
| 314 |
), |
| 315 |
) |
| 316 |
if res.status_code != 200: |
| 317 |
module.fail_json( |
| 318 |
msg="Create Directory Service Role {0} failed. Error: {1}".format( |
| 319 |
module.params["role"], |
| 320 |
res.errors[0].message, |
| 321 |
) |
| 322 |
) |
| 323 |
module.exit_json(changed=changed) |
| 324 |
|
| 325 |
|
| 326 |
def main(): |
| 327 |
argument_spec = purefa_argument_spec() |
| 328 |
argument_spec.update( |
| 329 |
dict( |
| 330 |
name=dict(type="str"), |
| 331 |
role=dict( |
| 332 |
type="str", |
| 333 |
choices=["array_admin", "ops_admin", "readonly", "storage_admin"], |
| 334 |
), |
| 335 |
state=dict(type="str", default="present", choices=["absent", "present"]), |
| 336 |
group_base=dict(type="str"), |
| 337 |
group=dict(type="str"), |
| 338 |
context=dict(type="str", default=""), |
| 339 |
) |
| 340 |
) |
| 341 |
|
| 342 |
required_if = [["state", "present", ["role"]]] |
| 343 |
required_together = [["group", "group_base"]] |
| 344 |
|
| 345 |
module = AnsibleModule( |
| 346 |
argument_spec, |
| 347 |
required_together=required_together, |
| 348 |
required_if=required_if, |
| 349 |
supports_check_mode=True, |
| 350 |
) |
| 351 |
|
| 352 |
if not HAS_PYPURECLIENT: |
| 353 |
module.fail_json(msg="pypureclient sdk is required for this module") |
| 354 |
|
| 355 |
state = module.params["state"] |
| 356 |
array = get_array(module) |
| 357 |
if not module.params["name"]: |
| 358 |
module.params["name"] = module.params["role"] |
| 359 |
api_version = array.get_rest_version() |
| 360 |
if LooseVersion(MIN_DSROLE_API_VERSION) > LooseVersion(api_version): |
| 361 |
module.fail_json( |
| 362 |
msg="This module requires Purity//FA 6.6.3 and higher. " |
| 363 |
"For older Purity versions please use the ``purefa_dsrole_old`` module" |
| 364 |
) |
| 365 |
role_configured = False |
| 366 |
role = {} |
| 367 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 368 |
res = array.get_directory_services_roles( |
| 369 |
names=[module.params["name"]], |
| 370 |
context_names=[module.params["context"]], |
| 371 |
) |
| 372 |
else: |
| 373 |
res = array.get_directory_services_roles(names=[module.params["name"]]) |
| 374 |
if res.status_code == 200: |
| 375 |
role = list(res.items)[0] |
| 376 |
if getattr(role, "group", None) is not None: |
| 377 |
role_configured = True |
| 378 |
|
| 379 |
if state == "absent" and role_configured: |
| 380 |
if module.params["name"] in [ |
| 381 |
"array_admin", |
| 382 |
"storage_admin", |
| 383 |
"ops_admin", |
| 384 |
"readonly", |
| 385 |
]: |
| 386 |
update_role(module, array) |
| 387 |
else: |
| 388 |
delete_role(module, array) |
| 389 |
elif role_configured and state == "present": |
| 390 |
update_role(module, array) |
| 391 |
elif not role_configured and state == "present": |
| 392 |
|
| 393 |
if module.params["name"] in [ |
| 394 |
"array_admin", |
| 395 |
"storage_admin", |
| 396 |
"ops_admin", |
| 397 |
"readonly", |
| 398 |
]: |
| 399 |
update_role(module, array) |
| 400 |
else: |
| 401 |
create_role(module, array) |
| 402 |
else: |
| 403 |
module.exit_json(changed=False) |
| 404 |
|
| 405 |
|
| 406 |
if __name__ == "__main__": |
| 407 |
main() |