| 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_token |
| 20 |
version_added: '1.0.0' |
| 21 |
short_description: Create or delete an API token for an existing admin user |
| 22 |
description: |
| 23 |
- Create or delete an API token for an existing admin user. |
| 24 |
- Uses username/password to create/delete the API token. |
| 25 |
author: |
| 26 |
- Pure Storage Ansible Team (@sdodsley) <[email protected]> |
| 27 |
options: |
| 28 |
state: |
| 29 |
description: |
| 30 |
- Create or delete API token |
| 31 |
type: str |
| 32 |
default: present |
| 33 |
choices: [ present, absent ] |
| 34 |
recreate: |
| 35 |
description: |
| 36 |
- Recreates the API token, overwriting the existing API token if present |
| 37 |
type: bool |
| 38 |
default: false |
| 39 |
username: |
| 40 |
description: |
| 41 |
- Username of the admin user to create API token for |
| 42 |
type: str |
| 43 |
password: |
| 44 |
description: |
| 45 |
- Password of the admin user to create API token for. |
| 46 |
type: str |
| 47 |
fa_url: |
| 48 |
description: |
| 49 |
- FlashArray management IPv4 address or Hostname. |
| 50 |
type: str |
| 51 |
timeout: |
| 52 |
description: |
| 53 |
- The duration of API token validity. |
| 54 |
- Valid values are weeks (w), days(d), hours(h), minutes(m) and seconds(s). |
| 55 |
type: str |
| 56 |
disable_warnings: |
| 57 |
description: |
| 58 |
- Disable insecure certificate warnings in debug logs |
| 59 |
type: bool |
| 60 |
default: false |
| 61 |
version_added: '1.31.0' |
| 62 |
""" |
| 63 |
|
| 64 |
EXAMPLES = r""" |
| 65 |
- name: Create API token with no expiration |
| 66 |
purefa_token: |
| 67 |
username: pureuser |
| 68 |
password: secret |
| 69 |
state: present |
| 70 |
fa_url: 10.10.10.2 |
| 71 |
- name: Create API token with 23 days expiration |
| 72 |
purefa_token: |
| 73 |
username: pureuser |
| 74 |
password: secret |
| 75 |
state: present |
| 76 |
timeout: 23d |
| 77 |
fa_url: 10.10.10.2 |
| 78 |
- name: Delete API token |
| 79 |
purefa_token: |
| 80 |
username: pureuser |
| 81 |
password: secret |
| 82 |
state: absent |
| 83 |
fa_url: 10.10.10.2 |
| 84 |
""" |
| 85 |
|
| 86 |
RETURN = r""" |
| 87 |
purefa_token: |
| 88 |
description: API token for user |
| 89 |
returned: changed |
| 90 |
type: str |
| 91 |
sample: e649f439-49be-3806-f774-a35cbbc4c2d2 |
| 92 |
""" |
| 93 |
|
| 94 |
from ansible.module_utils.basic import AnsibleModule |
| 95 |
from ansible_collections.purestorage.flasharray.plugins.module_utils.purefa import ( |
| 96 |
get_array, |
| 97 |
) |
| 98 |
from ansible_collections.purestorage.flasharray.plugins.module_utils.common import ( |
| 99 |
convert_time_to_millisecs, |
| 100 |
) |
| 101 |
from os import environ |
| 102 |
import platform |
| 103 |
|
| 104 |
VERSION = 1.5 |
| 105 |
USER_AGENT_BASE = "Ansible_token" |
| 106 |
TIMEOUT_API_VERSION = "2.2" |
| 107 |
|
| 108 |
HAS_URLLIB3 = True |
| 109 |
try: |
| 110 |
import urllib3 |
| 111 |
except ImportError: |
| 112 |
HAS_URLLIB3 = False |
| 113 |
|
| 114 |
HAS_DISTRO = True |
| 115 |
try: |
| 116 |
import distro |
| 117 |
except ImportError: |
| 118 |
HAS_DISTRO = False |
| 119 |
|
| 120 |
HAS_PURESTORAGE = True |
| 121 |
try: |
| 122 |
from purestorage import purestorage |
| 123 |
except ImportError: |
| 124 |
HAS_PURESTORAGE = False |
| 125 |
|
| 126 |
|
| 127 |
def get_session(module): |
| 128 |
"""Return System Object or Fail""" |
| 129 |
if HAS_URLLIB3 and module.params["disable_warnings"]: |
| 130 |
urllib3.disable_warnings() |
| 131 |
if HAS_DISTRO: |
| 132 |
user_agent = "%(base)s %(class)s/%(version)s (%(platform)s)" % { |
| 133 |
"base": USER_AGENT_BASE, |
| 134 |
"class": __name__, |
| 135 |
"version": VERSION, |
| 136 |
"platform": distro.name(pretty=True), |
| 137 |
} |
| 138 |
else: |
| 139 |
user_agent = "%(base)s %(class)s/%(version)s (%(platform)s)" % { |
| 140 |
"base": USER_AGENT_BASE, |
| 141 |
"class": __name__, |
| 142 |
"version": VERSION, |
| 143 |
"platform": platform.platform(), |
| 144 |
} |
| 145 |
|
| 146 |
array_name = module.params["fa_url"] |
| 147 |
username = module.params["username"] |
| 148 |
password = module.params["password"] |
| 149 |
|
| 150 |
if HAS_PURESTORAGE: |
| 151 |
if array_name and username and password: |
| 152 |
system = purestorage.FlashArray( |
| 153 |
array_name, username=username, password=password, user_agent=user_agent |
| 154 |
) |
| 155 |
elif environ.get("PUREFA_URL"): |
| 156 |
if environ.get("PUREFA_USERNAME") and environ.get("PUREFA_PASSWORD"): |
| 157 |
url = environ.get("PUREFA_URL") |
| 158 |
username = environ.get("PUREFA_USERNAME") |
| 159 |
password = environ.get("PUREFA_PASSWORD") |
| 160 |
system = purestorage.FlashArray( |
| 161 |
url, username=username, password=password, user_agent=user_agent |
| 162 |
) |
| 163 |
else: |
| 164 |
module.fail_json( |
| 165 |
msg="You must set PUREFA_URL and PUREFA_USERNAME, PUREFA_PASSWORD " |
| 166 |
"environment variables or the fa_url, username and password " |
| 167 |
"module arguments" |
| 168 |
) |
| 169 |
try: |
| 170 |
system.get() |
| 171 |
except Exception: |
| 172 |
module.fail_json( |
| 173 |
msg="Pure Storage FlashArray authentication failed. Check your credentials" |
| 174 |
) |
| 175 |
else: |
| 176 |
module.fail_json(msg="purestorage SDK is not installed.") |
| 177 |
return system |
| 178 |
|
| 179 |
|
| 180 |
def main(): |
| 181 |
argument_spec = dict( |
| 182 |
fa_url=dict(required=False), |
| 183 |
username=dict(type="str", required=False), |
| 184 |
password=dict(no_log=True, required=False), |
| 185 |
state=dict(type="str", default="present", choices=["absent", "present"]), |
| 186 |
recreate=dict(type="bool", default=False), |
| 187 |
timeout=dict(type="str"), |
| 188 |
disable_warnings=dict(type="bool", default=False), |
| 189 |
) |
| 190 |
|
| 191 |
module = AnsibleModule(argument_spec, supports_check_mode=False) |
| 192 |
array = get_session(module) |
| 193 |
changed = False |
| 194 |
|
| 195 |
if module.params["username"]: |
| 196 |
username = module.params["username"] |
| 197 |
else: |
| 198 |
username = environ.get("PUREFA_USERNAME") |
| 199 |
state = module.params["state"] |
| 200 |
recreate = module.params["recreate"] |
| 201 |
|
| 202 |
result = array.get_api_token(admin=username) |
| 203 |
api_version = array._list_available_rest_versions() |
| 204 |
if state == "present" and result["api_token"] is None: |
| 205 |
result = array.create_api_token(admin=username) |
| 206 |
changed = True |
| 207 |
elif state == "present" and recreate: |
| 208 |
result = array.delete_api_token(admin=username) |
| 209 |
result = array.create_api_token(admin=username) |
| 210 |
changed = True |
| 211 |
elif state == "absent" and result["api_token"]: |
| 212 |
result = array.delete_api_token(admin=username) |
| 213 |
changed = True |
| 214 |
|
| 215 |
api_token = result["api_token"] |
| 216 |
|
| 217 |
if ( |
| 218 |
TIMEOUT_API_VERSION in api_version |
| 219 |
and module.params["timeout"] |
| 220 |
and state == "present" |
| 221 |
): |
| 222 |
module.params["api_token"] = api_token |
| 223 |
array6 = get_array(module) |
| 224 |
ttl = convert_time_to_millisecs(module.params["timeout"]) |
| 225 |
if ttl != 0: |
| 226 |
changed = True |
| 227 |
array6.delete_admins_api_tokens(names=[username]) |
| 228 |
res = array6.post_admins_api_tokens(names=[username], timeout=ttl) |
| 229 |
if res.status_code != 200: |
| 230 |
module.fail_json( |
| 231 |
msg="Failed to set token lifetime. Error: {0}".format( |
| 232 |
res.errors[0].message |
| 233 |
) |
| 234 |
) |
| 235 |
else: |
| 236 |
api_token = list(res.items)[0].api_token.token |
| 237 |
module.exit_json(changed=changed, purefa_token=api_token) |
| 238 |
|
| 239 |
|
| 240 |
if __name__ == "__main__": |
| 241 |
main() |