| 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: purefb_dsrole |
| 20 |
version_added: '1.0.0' |
| 21 |
short_description: Configure FlashBlade Management 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 |
state: |
| 28 |
description: |
| 29 |
- Create or delete directory service role |
| 30 |
default: present |
| 31 |
type: str |
| 32 |
choices: [ absent, present ] |
| 33 |
role: |
| 34 |
description: |
| 35 |
- The directory service role to work on |
| 36 |
choices: [ array_admin, ops_admin, readonly, storage_admin ] |
| 37 |
type: str |
| 38 |
required: true |
| 39 |
group_base: |
| 40 |
description: |
| 41 |
- Specifies where the configured group is located in the directory |
| 42 |
tree. This field consists of Organizational Units (OUs) that combine |
| 43 |
with the base DN attribute and the configured group CNs to complete |
| 44 |
the full Distinguished Name of the groups. The group base should |
| 45 |
specify OU= for each OU and multiple OUs should be separated by commas. |
| 46 |
The order of OUs is important and should get larger in scope from left |
| 47 |
to right. |
| 48 |
- Each OU should not exceed 64 characters in length. |
| 49 |
type: str |
| 50 |
group: |
| 51 |
description: |
| 52 |
- Sets the common Name (CN) of the configured directory service group |
| 53 |
containing users for the FlashBlade. This name should be just the |
| 54 |
Common Name of the group without the CN= specifier. |
| 55 |
- Common Names should not exceed 64 characters in length. |
| 56 |
type: str |
| 57 |
extends_documentation_fragment: |
| 58 |
- purestorage.flashblade.purestorage.fb |
| 59 |
""" |
| 60 |
|
| 61 |
EXAMPLES = r""" |
| 62 |
- name: Delete existing array_admin directory service role |
| 63 |
purestorage.flashblade.purefb_dsrole: |
| 64 |
role: array_admin |
| 65 |
state: absent |
| 66 |
fb_url: 10.10.10.2 |
| 67 |
api_token: e31060a7-21fc-e277-6240-25983c6c4592 |
| 68 |
|
| 69 |
- name: Create array_admin directory service role |
| 70 |
purestorage.flashblade.purefb_dsrole: |
| 71 |
role: array_admin |
| 72 |
group_base: "OU=PureGroups,OU=SANManagers" |
| 73 |
group: pureadmins |
| 74 |
fb_url: 10.10.10.2 |
| 75 |
api_token: e31060a7-21fc-e277-6240-25983c6c4592 |
| 76 |
|
| 77 |
- name: Update ops_admin directory service role |
| 78 |
purestorage.flashblade.purefb_dsrole: |
| 79 |
role: ops_admin |
| 80 |
group_base: "OU=PureGroups" |
| 81 |
group: opsgroup |
| 82 |
fb_url: 10.10.10.2 |
| 83 |
api_token: e31060a7-21fc-e277-6240-25983c6c4592 |
| 84 |
""" |
| 85 |
|
| 86 |
RETURN = r""" |
| 87 |
""" |
| 88 |
|
| 89 |
|
| 90 |
HAS_PURITY_FB = True |
| 91 |
try: |
| 92 |
from pypureclient.flashblade import DirectoryServiceRole |
| 93 |
except ImportError: |
| 94 |
HAS_PURITY_FB = False |
| 95 |
|
| 96 |
from ansible.module_utils.basic import AnsibleModule |
| 97 |
from ansible_collections.purestorage.flashblade.plugins.module_utils.purefb import ( |
| 98 |
get_system, |
| 99 |
purefb_argument_spec, |
| 100 |
) |
| 101 |
|
| 102 |
|
| 103 |
def update_role(module, blade): |
| 104 |
"""Update Directory Service Role""" |
| 105 |
changed = False |
| 106 |
res = blade.get_directory_services_roles(names=[module.params["role"]]) |
| 107 |
if res.status_code != 200: |
| 108 |
module.fail_json( |
| 109 |
msg="fetch Directory Service Role {0} failed. Error: {1}".format( |
| 110 |
module.params["role"], res.errors[0].message |
| 111 |
) |
| 112 |
) |
| 113 |
role = list(res.items)[0] |
| 114 |
if ( |
| 115 |
role.group_base != module.params["group_base"] |
| 116 |
or role.group != module.params["group"] |
| 117 |
): |
| 118 |
changed = True |
| 119 |
if not module.check_mode: |
| 120 |
role = DirectoryServiceRole( |
| 121 |
group_base=module.params["group_base"], group=module.params["group"] |
| 122 |
) |
| 123 |
res = blade.patch_directory_services_roles( |
| 124 |
names=[module.params["role"]], directory_service_roles=role |
| 125 |
) |
| 126 |
if res.status_code != 200: |
| 127 |
module.fail_json( |
| 128 |
msg="Update Directory Service Role {0} failed. Error: {1}".format( |
| 129 |
module.params["role"], res.errors[0].message |
| 130 |
) |
| 131 |
) |
| 132 |
module.exit_json(changed=changed) |
| 133 |
|
| 134 |
|
| 135 |
def delete_role(module, blade): |
| 136 |
"""Delete Directory Service Role""" |
| 137 |
changed = True |
| 138 |
if not module.check_mode: |
| 139 |
role = DirectoryServiceRole(group_base="", group="") |
| 140 |
res = blade.patch_directory_services_roles( |
| 141 |
names=[module.params["role"]], directory_service_roles=role |
| 142 |
) |
| 143 |
if res.status_code != 200: |
| 144 |
module.fail_json( |
| 145 |
msg="Delete Directory Service Role {0} failed. Error: {1}".format( |
| 146 |
module.params["role"], res.errors[0].message |
| 147 |
) |
| 148 |
) |
| 149 |
module.exit_json(changed=changed) |
| 150 |
|
| 151 |
|
| 152 |
def create_role(module, blade): |
| 153 |
"""Create Directory Service Role""" |
| 154 |
changed = True |
| 155 |
if not module.check_mode: |
| 156 |
role = DirectoryServiceRole( |
| 157 |
group_base=module.params["group_base"], group=module.params["group"] |
| 158 |
) |
| 159 |
res = blade.patch_directory_services_roles( |
| 160 |
names=[module.params["role"]], directory_service_roles=role |
| 161 |
) |
| 162 |
if res.status_code != 200: |
| 163 |
module.fail_json( |
| 164 |
msg="Create Directory Service Role {0} failed. Error: {1}".format( |
| 165 |
module.params["role"], res.errors[0].message |
| 166 |
) |
| 167 |
) |
| 168 |
module.exit_json(changed=changed) |
| 169 |
|
| 170 |
|
| 171 |
def main(): |
| 172 |
argument_spec = purefb_argument_spec() |
| 173 |
argument_spec.update( |
| 174 |
dict( |
| 175 |
role=dict( |
| 176 |
required=True, |
| 177 |
type="str", |
| 178 |
choices=["array_admin", "ops_admin", "readonly", "storage_admin"], |
| 179 |
), |
| 180 |
state=dict(type="str", default="present", choices=["absent", "present"]), |
| 181 |
group_base=dict(type="str"), |
| 182 |
group=dict(type="str"), |
| 183 |
) |
| 184 |
) |
| 185 |
|
| 186 |
required_together = [["group", "group_base"]] |
| 187 |
|
| 188 |
module = AnsibleModule( |
| 189 |
argument_spec, required_together=required_together, supports_check_mode=True |
| 190 |
) |
| 191 |
|
| 192 |
if not HAS_PURITY_FB: |
| 193 |
module.fail_json(msg="py-pure-client sdk is required for this module") |
| 194 |
|
| 195 |
state = module.params["state"] |
| 196 |
blade = get_system(module) |
| 197 |
role_configured = False |
| 198 |
res = blade.get_directory_services_roles(names=[module.params["role"]]) |
| 199 |
if res.status_code == 200: |
| 200 |
role_configured = True |
| 201 |
|
| 202 |
if state == "absent" and role_configured: |
| 203 |
delete_role(module, blade) |
| 204 |
elif role_configured and state == "present": |
| 205 |
update_role(module, blade) |
| 206 |
elif not role_configured and state == "present": |
| 207 |
create_role(module, blade) |
| 208 |
else: |
| 209 |
module.exit_json(changed=False) |
| 210 |
|
| 211 |
|
| 212 |
if __name__ == "__main__": |
| 213 |
main() |