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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
|
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright: (c) 2017, 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_host
version_added: '2.4'
short_description: Manage hosts on Pure Storage FlashArrays
description:
- Create, delete or modify hosts on Pure Storage FlashArrays.
author:
- Pure Storage Ansible Team (@sdodsley) <pure-ansible-team@purestorage.com>
notes:
- If specifying C(lun) option ensure host support requested value
options:
host:
description:
- The name of the host.
type: str
required: true
state:
description:
- Define whether the host should exist or not.
- When removing host all connected volumes will be disconnected.
type: str
default: present
choices: [ absent, present ]
protocol:
description:
- Defines the host connection protocol for volumes.
type: str
default: iscsi
choices: [ fc, iscsi, nvme, mixed ]
wwns:
type: list
description:
- List of wwns of the host if protocol is fc or mixed.
iqn:
type: list
description:
- List of IQNs of the host if protocol is iscsi or mixed.
nqn:
type: list
description:
- List of NQNs of the host if protocol is nvme or mixed.
version_added: '2.8'
volume:
type: str
description:
- Volume name to map to the host.
lun:
description:
- LUN ID to assign to volume for host. Must be unique.
- If not provided the ID will be automatically assigned.
- Range for LUN ID is 1 to 4095.
type: int
version_added: '2.8'
personality:
type: str
description:
- Define which operating system the host is. Recommended for
ActiveCluster integration.
default: ''
choices: ['hpux', 'vms', 'aix', 'esxi', 'solaris', 'hitachi-vsp', 'oracle-vm-server', 'delete', '']
version_added: '2.7'
preferred_array:
type: list
description:
- List of preferred arrays in an ActiveCluster environment.
- To remove existing preferred arrays from the host, specify I(delete).
version_added: '2.9'
extends_documentation_fragment:
- purestorage.fa
'''
EXAMPLES = r'''
- name: Create new AIX host
purefa_host:
host: foo
personality: aix
fa_url: 10.10.10.2
api_token: e31060a7-21fc-e277-6240-25983c6c4592
- name: Delete host
purefa_host:
host: foo
fa_url: 10.10.10.2
api_token: e31060a7-21fc-e277-6240-25983c6c4592
state: absent
- name: Make host bar with wwn ports
purefa_host:
host: bar
protocol: fc
wwns:
- 00:00:00:00:00:00:00
- 11:11:11:11:11:11:11
fa_url: 10.10.10.2
api_token: e31060a7-21fc-e277-6240-25983c6c4592
- name: Make host bar with iSCSI ports
purefa_host:
host: bar
protocol: iscsi
iqn:
- iqn.1994-05.com.redhat:7d366003913
fa_url: 10.10.10.2
api_token: e31060a7-21fc-e277-6240-25983c6c4592
- name: Make host bar with NVMe ports
purefa_host:
host: bar
protocol: nvme
nqn:
- nqn.2014-08.com.vendor:nvme:nvm-subsystem-sn-d78432
fa_url: 10.10.10.2
api_token: e31060a7-21fc-e277-6240-25983c6c4592
- name: Make mixed protocol host
purefa_host:
host: bar
protocol: mixed
nqn:
- nqn.2014-08.com.vendor:nvme:nvm-subsystem-sn-d78432
iqn:
- iqn.1994-05.com.redhat:7d366003914
wwns:
- 00:00:00:00:00:00:01
- 11:11:11:11:11:11:12
fa_url: 10.10.10.2
api_token: e31060a7-21fc-e277-6240-25983c6c4592
- name: Map host foo to volume bar as LUN ID 12
purefa_host:
host: foo
volume: bar
lun: 12
fa_url: 10.10.10.2
api_token: e31060a7-21fc-e277-6240-25983c6c4592
- name: Add preferred arrays to host foo
purefa_host:
host: foo
preferred_array:
- array1
- array2
fa_url: 10.10.10.2
api_token: e31060a7-21fc-e277-6240-25983c6c4592
- name: Delete preferred arrays from host foo
purefa_host:
host: foo
preferred_array: delete
fa_url: 10.10.10.2
api_token: e31060a7-21fc-e277-6240-25983c6c4592
'''
RETURN = r'''
'''
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.pure import get_system, purefa_argument_spec
AC_REQUIRED_API_VERSION = '1.14'
PREFERRED_ARRAY_API_VERSION = '1.15'
NVME_API_VERSION = '1.16'
def _is_cbs(module, array, is_cbs=False):
"""Is the selected array a Cloud Block Store"""
model = ''
ct0_model = array.get_hardware('CT0')['model']
if ct0_model:
model = ct0_model
else:
ct1_model = array.get_hardware('CT1')['model']
model = ct1_model
if 'CBS' in model:
is_cbs = True
return is_cbs
def _set_host_initiators(module, array):
"""Set host initiators."""
if module.params['protocol'] in ['nvme', 'mixed']:
if module.params['nqn']:
try:
array.set_host(module.params['host'],
nqnlist=module.params['nqn'])
except Exception:
module.fail_json(msg='Setting of NVMe NQN failed.')
if module.params['protocol'] in ['iscsi', 'mixed']:
if module.params['iqn']:
try:
array.set_host(module.params['host'],
iqnlist=module.params['iqn'])
except Exception:
module.fail_json(msg='Setting of iSCSI IQN failed.')
if module.params['protocol'] in ['fc', 'mixed']:
if module.params['wwns']:
try:
array.set_host(module.params['host'],
wwnlist=module.params['wwns'])
except Exception:
module.fail_json(msg='Setting of FC WWNs failed.')
def _update_host_initiators(module, array, answer=False):
"""Change host initiator if iscsi or nvme or add new FC WWNs"""
if module.params['protocol'] in ['nvme', 'mixed']:
if module.params['nqn']:
current_nqn = array.get_host(module.params['host'])['nqn']
if current_nqn != module.params['nqn']:
try:
array.set_host(module.params['host'],
nqnlist=module.params['nqn'])
answer = True
except Exception:
module.fail_json(msg='Change of NVMe NQN failed.')
if module.params['protocol'] in ['iscsi', 'mixed']:
if module.params['iqn']:
current_iqn = array.get_host(module.params['host'])['iqn']
if current_iqn != module.params['iqn']:
try:
array.set_host(module.params['host'],
iqnlist=module.params['iqn'])
answer = True
except Exception:
module.fail_json(msg='Change of iSCSI IQN failed.')
if module.params['protocol'] in ['fc', 'mixed']:
if module.params['wwns']:
module.params['wwns'] = [wwn.replace(':', '') for wwn in module.params['wwns']]
module.params['wwns'] = [wwn.upper() for wwn in module.params['wwns']]
current_wwn = array.get_host(module.params['host'])['wwn']
if current_wwn != module.params['wwns']:
try:
array.set_host(module.params['host'],
wwnlist=module.params['wwns'])
answer = True
except Exception:
module.fail_json(msg='FC WWN change failed.')
return answer
def _connect_new_volume(module, array, answer=False):
"""Connect volume to host"""
api_version = array._list_available_rest_versions()
if AC_REQUIRED_API_VERSION in api_version and module.params['lun']:
try:
array.connect_host(module.params['host'],
module.params['volume'],
lun=module.params['lun'])
answer = True
except Exception:
module.fail_json(msg='LUN ID {0} invalid. Check for duplicate LUN IDs.'.format(module.params['lun']))
else:
array.connect_host(module.params['host'], module.params['volume'])
answer = True
return answer
def _set_host_personality(module, array):
"""Set host personality. Only called when supported"""
if module.params['personality'] != 'delete':
array.set_host(module.params['host'],
personality=module.params['personality'])
else:
array.set_host(module.params['host'], personality='')
def _set_preferred_array(module, array):
"""Set preferred array list. Only called when supported"""
if module.params['preferred_array'] != ['delete']:
array.set_host(module.params['host'],
preferred_array=module.params['preferred_array'])
else:
array.set_host(module.params['host'], personality='')
def _update_host_personality(module, array, answer=False):
"""Change host personality. Only called when supported"""
personality = array.get_host(module.params['host'], personality=True)['personality']
if personality is None and module.params['personality'] != 'delete':
try:
array.set_host(module.params['host'],
personality=module.params['personality'])
answer = True
except Exception:
module.fail_json(msg='Personality setting failed.')
if personality is not None:
if module.params['personality'] == 'delete':
try:
array.set_host(module.params['host'], personality='')
answer = True
except Exception:
module.fail_json(msg='Personality deletion failed.')
elif personality != module.params['personality']:
try:
array.set_host(module.params['host'],
personality=module.params['personality'])
answer = True
except Exception:
module.fail_json(msg='Personality change failed.')
return answer
def _update_preferred_array(module, array, answer=False):
"""Update existing preferred array list. Only called when supported"""
preferred_array = array.get_host(module.params['host'], preferred_array=True)['preferred_array']
if preferred_array == [] and module.params['preferred_array'] != ['delete']:
try:
array.set_host(module.params['host'],
preferred_array=module.params['preferred_array'])
answer = True
except Exception:
module.fail_json(msg='Preferred array list creation failed for {0}.'.format(module.params['host']))
elif preferred_array != []:
if module.params['preferred_array'] == ['delete']:
try:
array.set_host(module.params['host'], preferred_array=[])
answer = True
except Exception:
module.fail_json(msg='Preferred array list deletion failed for {0}.'.format(module.params['host']))
elif preferred_array != module.params['preferred_array']:
try:
array.set_host(module.params['host'],
preferred_array=module.params['preferred_array'])
answer = True
except Exception:
module.fail_json(msg='Preferred array list change failed for {0}.'.format(module.params['host']))
return answer
def get_host(module, array):
host = None
for hst in array.list_hosts():
if hst["name"] == module.params['host']:
host = hst
break
return host
def make_host(module, array):
changed = True
if not module.check_mode:
try:
array.create_host(module.params['host'])
except Exception:
module.fail_json(msg='Host {0} creation failed.'.format(module.params['host']))
try:
_set_host_initiators(module, array)
api_version = array._list_available_rest_versions()
if AC_REQUIRED_API_VERSION in api_version and module.params['personality']:
_set_host_personality(module, array)
if PREFERRED_ARRAY_API_VERSION in api_version and module.params['preferred_array']:
_set_preferred_array(module, array)
if module.params['volume']:
if module.params['lun']:
array.connect_host(module.params['host'],
module.params['volume'],
lun=module.params['lun'])
else:
array.connect_host(module.params['host'], module.params['volume'])
except Exception:
module.fail_json(msg='Host {0} configuration failed.'.format(module.params['host']))
module.exit_json(changed=changed)
def update_host(module, array):
changed = True
if not module.check_mode:
init_changed = vol_changed = pers_changed = pref_changed = False
volumes = array.list_host_connections(module.params['host'])
if module.params['iqn'] or module.params['wwns'] or module.params['nqn']:
init_changed = _update_host_initiators(module, array)
if module.params['volume']:
current_vols = [vol['vol'] for vol in volumes]
if not module.params['volume'] in current_vols:
vol_changed = _connect_new_volume(module, array)
api_version = array._list_available_rest_versions()
if AC_REQUIRED_API_VERSION in api_version:
if module.params['personality']:
pers_changed = _update_host_personality(module, array)
if PREFERRED_ARRAY_API_VERSION in api_version:
if module.params['preferred_array']:
pref_changed = _update_preferred_array(module, array)
changed = init_changed or vol_changed or pers_changed or pref_changed
module.exit_json(changed=changed)
def delete_host(module, array):
changed = True
if not module.check_mode:
try:
for vol in array.list_host_connections(module.params['host']):
array.disconnect_host(module.params['host'], vol["vol"])
array.delete_host(module.params['host'])
except Exception:
module.fail_json(msg='Host {0} deletion failed'.format(module.params['host']))
module.exit_json(changed=changed)
def main():
argument_spec = purefa_argument_spec()
argument_spec.update(dict(
host=dict(type='str', required=True),
state=dict(type='str', default='present', choices=['absent', 'present']),
protocol=dict(type='str', default='iscsi', choices=['fc', 'iscsi', 'nvme', 'mixed']),
nqn=dict(type='list'),
iqn=dict(type='list'),
wwns=dict(type='list'),
volume=dict(type='str'),
lun=dict(type='int'),
personality=dict(type='str', default='',
choices=['hpux', 'vms', 'aix', 'esxi', 'solaris',
'hitachi-vsp', 'oracle-vm-server', 'delete', '']),
preferred_array=dict(type='list'),
))
module = AnsibleModule(argument_spec, supports_check_mode=True)
array = get_system(module)
if _is_cbs(module, array) and module.params['wwns'] or module.params['nqn']:
module.fail_json(msg='Cloud block Store only support iSCSI as a protocol')
api_version = array._list_available_rest_versions()
if module.params['nqn'] is not None and NVME_API_VERSION not in api_version:
module.fail_json(msg='NVMe protocol not supported. Please upgrade your array.')
state = module.params['state']
host = get_host(module, array)
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:
array.get_volume(module.params['volume'])
except Exception:
module.fail_json(msg='Volume {0} not found'.format(module.params['volume']))
if module.params['preferred_array']:
try:
if module.params['preferred_array'] != ['delete']:
all_connected_arrays = array.list_array_connections()
if not all_connected_arrays:
module.fail_json(msg='No target arrays connected to source array. Setting preferred arrays not possible.')
else:
current_arrays = [array.get()['array_name']]
for current_array in range(0, len(all_connected_arrays)):
if all_connected_arrays[current_array]['type'] == "sync-replication":
current_arrays.append(all_connected_arrays[current_array]['array_name'])
for array_to_connect in range(0, len(module.params['preferred_array'])):
if module.params['preferred_array'][array_to_connect] not in current_arrays:
module.fail_json(msg='Array {0} is not a synchronously connected array.'.format(module.params['preferred_array'][array_to_connect]))
except Exception:
module.fail_json(msg='Failed to get existing array connections.')
if host is None and state == 'present':
make_host(module, array)
elif host and state == 'present':
update_host(module, array)
elif host and state == 'absent':
delete_host(module, array)
elif host is None and state == 'absent':
module.exit_json(changed=False)
if __name__ == '__main__':
main()
|