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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
|
#!/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_hg
version_added: '1.0.0'
short_description: Manage hostgroups on Pure Storage FlashArrays
description:
- Create, delete or modifiy hostgroups on Pure Storage FlashArrays.
author:
- Pure Storage ansible Team (@sdodsley) <pure-ansible-team@purestorage.com>
options:
name:
description:
- The name of the hostgroup.
type: str
required: true
aliases: [ hostgroup ]
state:
description:
- Define whether the hostgroup should exist or not.
type: str
default: present
choices: [ absent, present ]
host:
type: list
elements: str
description:
- List of existing hosts to add to hostgroup.
- Note that hostnames are case-sensitive however FlashArray hostnames are unique
and ignore case - you cannot have I(hosta) and I(hostA)
volume:
type: list
elements: str
description:
- List of existing volumes to add to hostgroup.
- Note that volumes are case-sensitive however FlashArray volume names are unique
and ignore case - you cannot have I(volumea) and I(volumeA)
lun:
description:
- LUN ID to assign to volume for hostgroup. Must be unique.
- Only applicable when only one volume is specified for connection.
- If not provided the ID will be automatically assigned.
- Range for LUN ID is 1 to 4095.
type: int
rename:
description:
- New name of hostgroup
type: str
version_added: '1.10.0'
extends_documentation_fragment:
- purestorage.flasharray.purestorage.fa
"""
EXAMPLES = r"""
- name: Create empty hostgroup
purestorage.flasharray.purefa_hg:
name: foo
fa_url: 10.10.10.2
api_token: e31060a7-21fc-e277-6240-25983c6c4592
- name: Add hosts and volumes to existing or new hostgroup
purestorage.flasharray.purefa_hg:
name: foo
host:
- host1
- host2
volume:
- vol1
- vol2
fa_url: 10.10.10.2
api_token: e31060a7-21fc-e277-6240-25983c6c4592
- name: Delete hosts and volumes from hostgroup
purestorage.flasharray.purefa_hg:
name: foo
host:
- host1
- host2
volume:
- vol1
- vol2
fa_url: 10.10.10.2
api_token: e31060a7-21fc-e277-6240-25983c6c4592
state: absent
# This will disconnect all hosts and volumes in the hostgroup
- name: Delete hostgroup
purestorage.flasharray.purefa_hg:
name: foo
fa_url: 10.10.10.2
api_token: e31060a7-21fc-e277-6240-25983c6c4592
state: absent
- name: Rename hostgroup
purestorage.flasharray.purefa_hg:
name: foo
rename: bar
fa_url: 10.10.10.2
api_token: e31060a7-21fc-e277-6240-25983c6c4592
- name: Create host group with hosts and volumes
purestorage.flasharray.purefa_hg:
name: bar
host:
- host1
- host2
volume:
- vol1
- vol2
fa_url: 10.10.10.2
api_token: e31060a7-21fc-e277-6240-25983c6c4592
"""
RETURN = r"""
"""
from ansible.module_utils.basic import AnsibleModule
from ansible_collections.purestorage.flasharray.plugins.module_utils.purefa import (
get_system,
purefa_argument_spec,
)
def rename_exists(module, array):
"""Determine if rename target already exists"""
exists = False
new_name = module.params["rename"]
for hgroup in array.list_hgroups():
if hgroup["name"].casefold() == new_name.casefold():
exists = True
break
return exists
def get_hostgroup(module, array):
hostgroup = None
for host in array.list_hgroups():
if host["name"].casefold() == module.params["name"].casefold():
hostgroup = host
break
return hostgroup
def make_hostgroup(module, array):
if module.params["rename"]:
module.fail_json(
msg="Hostgroup {0} does not exist - rename failed.".format(
module.params["name"]
)
)
changed = True
if not module.check_mode:
try:
array.create_hgroup(module.params["name"])
except Exception:
module.fail_json(
msg="Failed to create hostgroup {0}".format(module.params["name"])
)
if module.params["host"]:
array.set_hgroup(module.params["name"], hostlist=module.params["host"])
if module.params["volume"]:
if len(module.params["volume"]) == 1 and module.params["lun"]:
try:
array.connect_hgroup(
module.params["name"],
module.params["volume"][0],
lun=module.params["lun"],
)
except Exception:
module.fail_json(
msg="Failed to add volume {0} with LUN ID {1}".format(
module.params["volume"][0], module.params["lun"]
)
)
else:
for vol in module.params["volume"]:
try:
array.connect_hgroup(module.params["name"], vol)
except Exception:
module.fail_json(msg="Failed to add volume to hostgroup")
module.exit_json(changed=changed)
def update_hostgroup(module, array):
changed = False
renamed = False
hgroup = get_hostgroup(module, array)
current_hostgroup = module.params["name"]
volumes = array.list_hgroup_connections(module.params["name"])
if module.params["state"] == "present":
if module.params["rename"]:
if not rename_exists(module, array):
try:
if not module.check_mode:
array.rename_hgroup(
module.params["name"], module.params["rename"]
)
current_hostgroup = module.params["rename"]
renamed = True
except Exception:
module.fail_json(
msg="Rename to {0} failed.".format(module.params["rename"])
)
else:
module.warn(
"Rename failed. Hostgroup {0} already exists. Continuing with other changes...".format(
module.params["rename"]
)
)
if module.params["host"]:
cased_hosts = list(module.params["host"])
cased_hghosts = list(hgroup["hosts"])
new_hosts = list(set(cased_hosts).difference(cased_hghosts))
if new_hosts:
try:
if not module.check_mode:
array.set_hgroup(current_hostgroup, addhostlist=new_hosts)
changed = True
except Exception:
module.fail_json(msg="Failed to add host(s) to hostgroup")
if module.params["volume"]:
if volumes:
current_vols = [vol["vol"] for vol in volumes]
cased_vols = list(module.params["volume"])
new_volumes = list(set(cased_vols).difference(set(current_vols)))
if len(new_volumes) == 1 and module.params["lun"]:
try:
if not module.check_mode:
array.connect_hgroup(
current_hostgroup,
new_volumes[0],
lun=module.params["lun"],
)
changed = True
except Exception:
module.fail_json(
msg="Failed to add volume {0} with LUN ID {1}".format(
new_volumes[0], module.params["lun"]
)
)
else:
for cvol in new_volumes:
try:
if not module.check_mode:
array.connect_hgroup(current_hostgroup, cvol)
changed = True
except Exception:
module.fail_json(
msg="Failed to connect volume {0} to hostgroup {1}.".format(
cvol, current_hostgroup
)
)
else:
if len(module.params["volume"]) == 1 and module.params["lun"]:
try:
if not module.check_mode:
array.connect_hgroup(
current_hostgroup,
module.params["volume"][0],
lun=module.params["lun"],
)
changed = True
except Exception:
module.fail_json(
msg="Failed to add volume {0} with LUN ID {1}".format(
module.params["volume"], module.params["lun"]
)
)
else:
for cvol in module.params["volume"]:
try:
if not module.check_mode:
array.connect_hgroup(current_hostgroup, cvol)
changed = True
except Exception:
module.fail_json(
msg="Failed to connect volume {0} to hostgroup {1}.".format(
cvol, current_hostgroup
)
)
else:
if module.params["host"]:
cased_old_hosts = list(module.params["host"])
cased_hosts = list(hgroup["hosts"])
old_hosts = list(set(cased_old_hosts).intersection(cased_hosts))
if old_hosts:
try:
if not module.check_mode:
array.set_hgroup(current_hostgroup, remhostlist=old_hosts)
changed = True
except Exception:
module.fail_json(
msg="Failed to remove hosts {0} from hostgroup {1}".format(
old_hosts, current_hostgroup
)
)
if module.params["volume"]:
cased_old_vols = list(module.params["volume"])
old_volumes = list(
set(cased_old_vols).intersection(set([vol["vol"] for vol in volumes]))
)
if old_volumes:
changed = True
for cvol in old_volumes:
try:
if not module.check_mode:
array.disconnect_hgroup(current_hostgroup, cvol)
except Exception:
module.fail_json(
msg="Failed to disconnect volume {0} from hostgroup {1}".format(
cvol, current_hostgroup
)
)
changed = changed or renamed
module.exit_json(changed=changed)
def delete_hostgroup(module, array):
changed = True
try:
vols = array.list_hgroup_connections(module.params["name"])
except Exception:
module.fail_json(
msg="Failed to get volume connection for hostgroup {0}".format(
module.params["hostgroup"]
)
)
if not module.check_mode:
for vol in vols:
try:
array.disconnect_hgroup(module.params["name"], vol["vol"])
except Exception:
module.fail_json(
msg="Failed to disconnect volume {0} from hostgroup {1}".format(
vol["vol"], module.params["name"]
)
)
host = array.get_hgroup(module.params["name"])
if not module.check_mode:
try:
array.set_hgroup(module.params["name"], remhostlist=host["hosts"])
try:
array.delete_hgroup(module.params["name"])
except Exception:
module.fail_json(
msg="Failed to delete hostgroup {0}".format(
module.params["name"]
)
)
except Exception:
module.fail_json(
msg="Failed to remove hosts {0} from hostgroup {1}".format(
host["hosts"], module.params["name"]
)
)
module.exit_json(changed=changed)
def main():
argument_spec = purefa_argument_spec()
argument_spec.update(
dict(
name=dict(type="str", required=True, aliases=["hostgroup"]),
state=dict(type="str", default="present", choices=["absent", "present"]),
host=dict(type="list", elements="str"),
lun=dict(type="int"),
rename=dict(type="str"),
volume=dict(type="list", elements="str"),
)
)
module = AnsibleModule(argument_spec, supports_check_mode=True)
state = module.params["state"]
array = get_system(module)
hostgroup = get_hostgroup(module, array)
if module.params["host"]:
try:
for hst in module.params["host"]:
array.get_host(hst)
except Exception:
module.fail_json(msg="Host {0} not found".format(hst))
if module.params["lun"] and len(module.params["volume"]) > 1:
module.fail_json(msg="LUN ID cannot be specified with multiple volumes.")
if module.params["lun"] and not 1 <= module.params["lun"] <= 4095:
module.fail_json(
msg="LUN ID of {0} is out of range (1 to 4095)".format(module.params["lun"])
)
if module.params["volume"]:
try:
for vol in module.params["volume"]:
array.get_volume(vol)
except Exception:
module.exit_json(changed=False)
if hostgroup and state == "present":
update_hostgroup(module, array)
elif hostgroup and module.params["volume"] and state == "absent":
update_hostgroup(module, array)
elif hostgroup and module.params["host"] and state == "absent":
update_hostgroup(module, array)
elif hostgroup and state == "absent":
delete_hostgroup(module, array)
elif hostgroup is None and state == "absent":
module.exit_json(changed=False)
else:
make_hostgroup(module, array)
module.exit_json(changed=False)
if __name__ == "__main__":
main()
|