1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
|
#!/usr/bin/python
# -*- coding: utf-8 -*-
# (c) 2018, Simon Dodsley (simon@purestorage.com)
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
from __future__ import absolute_import, division, print_function
__metaclass__ = type
ANSIBLE_METADATA = {
"metadata_version": "1.1",
"status": ["preview"],
"supported_by": "community",
}
DOCUMENTATION = r"""
---
module: purefa_user
version_added: '1.0.0'
short_description: Create, modify or delete FlashArray local user account
description:
- Create, modify or delete local users on a Pure Stoage FlashArray.
author:
- Pure Storage Ansible Team (@sdodsley) <pure-ansible-team@purestorage.com>
options:
state:
description:
- Create, delete or update local user account
default: present
type: str
choices: [ absent, present ]
name:
description:
- The name of the local user account
type: str
required: true
role:
description:
- Sets the local user's access level to the array
type: str
choices: [ readonly, ops_admin, storage_admin, array_admin ]
password:
description:
- Password for the local user.
type: str
old_password:
description:
- If changing an existing password, you must provide the old password for security
type: str
api:
description:
- Define whether to create an API token for this user
- Token can be exposed using the I(debug) module
type: bool
default: false
extends_documentation_fragment:
- purestorage.flasharray.purestorage.fa
"""
EXAMPLES = r"""
- name: Create new user ansible with API token
purestorage.flasharray.purefa_user:
name: ansible
password: apassword
role: storage_admin
api: true
fa_url: 10.10.10.2
api_token: e31060a7-21fc-e277-6240-25983c6c4592
register: result
debug:
msg: "API Token: {{ result['user_info']['user_api'] }}"
- name: Change role type for existing user
purestorage.flasharray.purefa_user:
name: ansible
role: array_admin
fa_url: 10.10.10.2
api_token: e31060a7-21fc-e277-6240-25983c6c4592
- name: Change password type for existing user (NOT IDEMPOTENT)
purestorage.flasharray.purefa_user:
name: ansible
password: anewpassword
old_password: apassword
fa_url: 10.10.10.2
api_token: e31060a7-21fc-e277-6240-25983c6c4592
- name: Change API token for existing user
purestorage.flasharray.purefa_user:
name: ansible
api: true
fa_url: 10.10.10.2
api_token: e31060a7-21fc-e277-6240-25983c6c4592
register: result
debug:
msg: "API Token: {{ result['user_info']['user_api'] }}"
"""
RETURN = r"""
"""
import re
from ansible.module_utils.basic import AnsibleModule
from ansible_collections.purestorage.flasharray.plugins.module_utils.purefa import (
get_system,
purefa_argument_spec,
)
MIN_REQUIRED_API_VERSION = "1.14"
def get_user(module, array):
"""Return Local User Account or None"""
user = None
users = array.list_admins()
for acct in range(0, len(users)):
if users[acct]["name"] == module.params["name"]:
user = users[acct]
return user
def create_user(module, array):
"""Create or Update Local User Account"""
changed = api_changed = role_changed = passwd_changed = False
user = get_user(module, array)
role = module.params["role"]
user_token = {}
if not user:
changed = True
if not module.check_mode:
try:
if not role:
role = "readonly"
array.create_admin(
module.params["name"], role=role, password=module.params["password"]
)
if module.params["api"]:
try:
user_token["user_api"] = array.create_api_token(
module.params["name"]
)["api_token"]
except Exception:
array.delete_user(module.params["name"])
module.fail_json(
msg="Local User {0}: Creation failed".format(
module.params["name"]
)
)
except Exception:
module.fail_json(
msg="Local User {0}: Creation failed".format(module.params["name"])
)
else:
if module.params["password"] and not module.params["old_password"]:
module.exit_json(changed=changed)
if module.params["password"] and module.params["old_password"]:
if module.params["old_password"] and (
module.params["password"] != module.params["old_password"]
):
passwd_changed = True
if not module.check_mode:
try:
array.set_admin(
module.params["name"],
password=module.params["password"],
old_password=module.params["old_password"],
)
except Exception:
module.fail_json(
msg="Local User {0}: Password reset failed. "
"Check old password.".format(module.params["name"])
)
else:
module.fail_json(
msg="Local User Account {0}: Password change failed - "
"Check both old and new passwords".format(module.params["name"])
)
if module.params["api"]:
try:
if not array.get_api_token(module.params["name"])["api_token"] is None:
if not module.check_mode:
array.delete_api_token(module.params["name"])
api_changed = True
if not module.check_mode:
user_token["user_api"] = array.create_api_token(
module.params["name"]
)["api_token"]
except Exception:
module.fail_json(
msg="Local User {0}: API token change failed".format(
module.params["name"]
)
)
if module.params["role"] and module.params["role"] != user["role"]:
if module.params["name"] != "pureuser":
role_changed = True
if not module.check_mode:
try:
array.set_admin(
module.params["name"], role=module.params["role"]
)
except Exception:
module.fail_json(
msg="Local User {0}: Role changed failed".format(
module.params["name"]
)
)
else:
module.warn("Role for 'pureuser' cannot be modified.")
changed = bool(passwd_changed or role_changed or api_changed)
module.exit_json(changed=changed, user_info=user_token)
def delete_user(module, array):
"""Delete Local User Account"""
changed = False
if get_user(module, array):
changed = True
if not module.check_mode:
try:
array.delete_admin(module.params["name"])
except Exception:
module.fail_json(
msg="Object Store Account {0}: Deletion failed".format(
module.params["name"]
)
)
module.exit_json(changed=changed)
def main():
argument_spec = purefa_argument_spec()
argument_spec.update(
dict(
name=dict(required=True, type="str"),
role=dict(
type="str",
choices=["readonly", "ops_admin", "storage_admin", "array_admin"],
),
state=dict(type="str", default="present", choices=["absent", "present"]),
password=dict(type="str", no_log=True),
old_password=dict(type="str", no_log=True),
api=dict(type="bool", default=False),
)
)
module = AnsibleModule(argument_spec, supports_check_mode=True)
state = module.params["state"]
array = get_system(module)
api_version = array._list_available_rest_versions()
if MIN_REQUIRED_API_VERSION not in api_version:
module.fail_json(
msg="FlashArray REST version not supported. "
"Minimum version required: {0}".format(MIN_REQUIRED_API_VERSION)
)
pattern = re.compile("^[a-z0-9]([a-z0-9-]{0,30}[a-z0-9])?$")
if not pattern.match(module.params["name"]):
module.fail_json(
msg="name must contain a minimum of 1 and a maximum of 32 characters "
"(alphanumeric or `-`). All letters must be lowercase."
)
if state == "absent":
delete_user(module, array)
elif state == "present":
create_user(module, array)
else:
module.exit_json(changed=False)
if __name__ == "__main__":
main()
|