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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
|
#!/usr/bin/python
# Copyright: (c) 2020, Dell Technologies
# Apache License version 2.0 (see MODULE-LICENSE or http://www.apache.org/licenses/LICENSE-2.0.txt)
"""Ansible module for managing host on Unity"""
from __future__ import absolute_import, division, print_function
__metaclass__ = type
DOCUMENTATION = r'''
---
module: host
version_added: '1.1.0'
short_description: Manage Host operations on Unity
description:
- The Host module contains the operations
Creation of a Host,
Addition of initiators to Host,
Removal of initiators from Host,
Modification of host attributes,
Get details of a Host,
Deletion of a Host,
Addition of network address to Host,
Removal of network address from Host.
extends_documentation_fragment:
- dellemc.unity.unity
author:
- Rajshree Khare (@kharer5) <ansible.team@dell.com>
options:
host_name:
description:
- Name of the host.
- Mandatory for host creation.
type: str
host_id:
description:
- Unique identifier of the host.
- Host Id is auto generated during creation.
- Except create, all other operations require either I(host_id) or Ihost_name).
type: str
description:
description:
- Host description.
type: str
host_os:
description:
- Operating system running on the host.
choices: ['AIX', 'Citrix XenServer', 'HP-UX', 'IBM VIOS', 'Linux',
'Mac OS', 'Solaris', 'VMware ESXi', 'Windows Client', 'Windows Server']
type: str
new_host_name:
description:
- New name for the host.
- Only required in rename host operation.
type: str
initiators:
description:
- List of initiators to be added/removed to/from host.
type: list
elements: str
initiator_state:
description:
- State of the initiator.
choices: [present-in-host , absent-in-host]
type: str
network_address:
description:
- Network address to be added/removed to/from the host.
- Enter valid IPV4 or host name.
type: str
network_address_state:
description:
- State of the Network address.
choices: [present-in-host , absent-in-host]
type: str
state:
description:
- State of the host.
choices: [present , absent]
type: str
required: true
notes:
- The I(check_mode) is not supported.
'''
EXAMPLES = r'''
- name: Create empty Host
dellemc.unity.host:
unispherehost: "{{unispherehost}}"
username: "{{username}}"
password: "{{password}}"
validate_certs: "{{validate_certs}}"
host_name: "ansible-test-host"
host_os: "Linux"
description: "ansible-test-host"
state: "present"
- name: Create Host with Initiators
dellemc.unity.host:
unispherehost: "{{unispherehost}}"
username: "{{username}}"
password: "{{password}}"
validate_certs: "{{validate_certs}}"
host_name: "ansible-test-host-1"
host_os: "Linux"
description: "ansible-test-host-1"
initiators:
- "iqn.1994-05.com.redhat:c38e6e8cfd81"
- "20:00:00:90:FA:13:81:8D:10:00:00:90:FA:13:81:8D"
initiator_state: "present-in-host"
state: "present"
- name: Modify Host using host_id
dellemc.unity.host:
unispherehost: "{{unispherehost}}"
username: "{{username}}"
password: "{{password}}"
validate_certs: "{{validate_certs}}"
host_id: "Host_253"
new_host_name: "ansible-test-host-2"
host_os: "Mac OS"
description: "Ansible tesing purpose"
state: "present"
- name: Add Initiators to Host
dellemc.unity.host:
unispherehost: "{{unispherehost}}"
username: "{{username}}"
password: "{{password}}"
validate_certs: "{{validate_certs}}"
host_name: "ansible-test-host-2"
initiators:
- "20:00:00:90:FA:13:81:8C:10:00:00:90:FA:13:81:8C"
initiator_state: "present-in-host"
state: "present"
- name: Get Host details using host_name
dellemc.unity.host:
unispherehost: "{{unispherehost}}"
username: "{{username}}"
password: "{{password}}"
validate_certs: "{{validate_certs}}"
host_name: "ansible-test-host-2"
state: "present"
- name: Get Host details using host_id
dellemc.unity.host:
unispherehost: "{{unispherehost}}"
username: "{{username}}"
password: "{{password}}"
validate_certs: "{{validate_certs}}"
host_id: "Host_253"
state: "present"
- name: Delete Host
dellemc.unity.host:
unispherehost: "{{unispherehost}}"
username: "{{username}}"
password: "{{password}}"
validate_certs: "{{validate_certs}}"
host_name: "ansible-test-host-2"
state: "absent"
- name: Add network address to Host
dellemc.unity.host:
unispherehost: "{{unispherehost}}"
username: "{{username}}"
password: "{{password}}"
validate_certs: "{{validate_certs}}"
host_name: "{{host_name}}"
network_address: "192.168.1.2"
network_address_state: "present-in-host"
state: "present"
- name: Delete network address from Host
dellemc.unity.host:
unispherehost: "{{unispherehost}}"
username: "{{username}}"
password: "{{password}}"
validate_certs: "{{validate_certs}}"
host_name: "{{host_name}}"
network_address: "192.168.1.2"
network_address_state: "absent-in-host"
state: "present"
'''
RETURN = r'''
changed:
description: Whether or not the resource has changed.
returned: always
type: bool
sample: true
host_details:
description: Details of the host.
returned: When host exists.
type: dict
contains:
id:
description: The system ID given to the host.
type: str
name:
description: The name of the host.
type: str
description:
description: Description about the host.
type: str
fc_host_initiators:
description: Details of the FC initiators associated with
the host.
type: list
contains:
id:
description: Unique identifier of the FC initiator path.
type: str
name:
description: FC Qualified Name (WWN) of the initiator.
type: str
paths:
description: Details of the paths associated with the FC initiator.
type: list
contains:
id:
description: Unique identifier of the path.
type: str
is_logged_in:
description: Indicates whether the host initiator is logged into the storage system.
type: bool
iscsi_host_initiators:
description: Details of the ISCSI initiators associated
with the host.
type: list
contains:
id:
description: Unique identifier of the ISCSI initiator path.
type: str
name:
description: ISCSI Qualified Name (IQN) of the initiator.
type: str
paths:
description: Details of the paths associated with the ISCSI initiator.
type: list
contains:
id:
description: Unique identifier of the path.
type: str
is_logged_in:
description: Indicates whether the host initiator is logged into the storage system.
type: bool
network_addresses:
description: List of network addresses mapped to the host.
type: list
os_type:
description: Operating system running on the host.
type: str
type:
description: HostTypeEnum of the host.
type: str
host_luns:
description: Details of luns attached to host.
type: list
sample: {
"auto_manage_type": "HostManageEnum.UNKNOWN",
"datastores": null,
"description": "ansible-test-host-1",
"existed": true,
"fc_host_initiators": [
{
"id": "HostInitiator_1",
"name": "HostName_1",
"paths": [
{
"id": "HostInitiator_1_Id1",
"is_logged_in": true
},
{
"id": "HostInitiator_1_Id2",
"is_logged_in": true
}
]
}
],
"hash": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
"health": {
"UnityHealth": {
"hash": 8764429420954
}
},
"host_container": null,
"host_luns": [],
"host_polled_uuid": null,
"host_pushed_uuid": null,
"host_uuid": null,
"host_v_vol_datastore": null,
"id": "Host_2198",
"iscsi_host_initiators": [
{
"id": "HostInitiator_2",
"name": "HostName_2",
"paths": [
{
"id": "HostInitiator_2_Id1",
"is_logged_in": true
},
{
"id": "HostInitiator_2_Id2",
"is_logged_in": true
}
]
}
],
"last_poll_time": null,
"name": "ansible-test-host-1",
"network_addresses": [],
"os_type": "Linux",
"registration_type": null,
"storage_resources": null,
"tenant": null,
"type": "HostTypeEnum.HOST_MANUAL",
"vms": null
}
'''
from ansible.module_utils.basic import AnsibleModule
from ansible_collections.dellemc.unity.plugins.module_utils.storage.dell \
import utils
import ipaddress
LOG = utils.get_logger('host')
application_type = "Ansible/1.6.0"
class Host(object):
"""Class with Host operations"""
def __init__(self):
""" Define all parameters required by this module"""
self.module_params = utils.get_unity_management_host_parameters()
self.module_params.update(get_host_parameters())
mutually_exclusive = [['host_name', 'host_id']]
required_one_of = [['host_name', 'host_id']]
required_together = [['network_address', 'network_address_state']]
""" initialize the ansible module """
self.module = AnsibleModule(argument_spec=self.module_params,
supports_check_mode=False,
mutually_exclusive=mutually_exclusive,
required_together=required_together,
required_one_of=required_one_of)
utils.ensure_required_libs(self.module)
self.unity = utils.get_unity_unisphere_connection(self.module.params, application_type)
LOG.info('Got the unity instance for provisioning on Unity')
def get_host_count(self, host_name):
""" To get the count of hosts with same host_name """
hosts = []
host_count = 0
hosts = utils.host.UnityHostList.get(cli=self.unity._cli,
name=host_name)
host_count = len(hosts)
return host_count
def get_host_details(self, host_id=None, host_name=None):
""" Get details of a given host """
host_id_or_name = host_id if host_id else host_name
try:
LOG.info("Getting host %s details", host_id_or_name)
if host_id:
host_details = self.unity.get_host(_id=host_id)
if host_details.name is None:
return None
if host_name:
''' get the count of hosts with same host_name '''
host_count = self.get_host_count(host_name)
if host_count < 1:
return None
elif host_count > 1:
error_message = "Duplicate hosts found: There are "\
+ host_count + " hosts(s) with the same" \
" host_name: " + host_name
LOG.error(error_message)
self.module.fail_json(msg=error_message)
else:
host_details = self.unity.get_host(name=host_name)
return host_details
except utils.HttpError as e:
if e.http_status == 401:
msg = 'Incorrect username or password provided.'
LOG.error(msg)
self.module.fail_json(msg=msg)
else:
msg = "Got HTTP Connection Error while getting host " \
"details %s : Error %s " % (host_id_or_name, str(e))
LOG.error(msg)
self.module.fail_json(msg=msg)
except utils.UnityResourceNotFoundError as e:
error_message = "Failed to get details of host " \
"{0} with error {1}".format(host_id_or_name,
str(e))
LOG.error(error_message)
return None
except Exception as e:
error_message = "Got error %s while getting details of host %s" \
% (str(e), host_id_or_name)
LOG.error(error_message)
self.module.fail_json(msg=error_message)
def create_host(self, host_name):
""" Create a new host """
try:
description = self.module.params['description']
host_os = self.module.params['host_os']
host_type = utils.HostTypeEnum.HOST_MANUAL
initiators = self.module.params['initiators']
initiator_state = self.module.params['initiator_state']
empty_initiators_flag = False
if (initiators and initiator_state == 'absent-in-host'):
error_message = "Incorrect 'initiator_state' given."
LOG.error(error_message)
self.module.fail_json(msg=error_message)
if (initiators is None or len(initiators) == 0
or not initiator_state
or initiator_state == 'absent-in-host'):
empty_initiators_flag = True
""" if any of the Initiators is invalid or already mapped """
if (initiators and initiator_state == 'present-in-host'):
unmapped_initiators \
= self.get_list_unmapped_initiators(initiators)
if unmapped_initiators is None \
or len(unmapped_initiators) < len(initiators):
error_message = "Provide valid initiators."
LOG.error(error_message)
self.module.fail_json(msg=error_message)
if not empty_initiators_flag:
self.validate_initiators(initiators)
LOG.info("Creating empty host %s ", host_name)
new_host = utils.host.UnityHost.create(self.unity._cli, name=host_name, desc=description,
os=host_os, host_type=host_type)
if not empty_initiators_flag:
host_details = self.unity.get_host(name=host_name)
LOG.info("Adding initiators to %s host", host_name)
result, new_host \
= self.add_initiator_to_host(host_details, initiators)
return True, new_host
except Exception as e:
error_message = "Got error %s while creation of host %s" \
% (str(e), host_name)
LOG.error(error_message)
self.module.fail_json(msg=error_message)
def validate_initiators(self, initiators):
results = []
for item in initiators:
results.append(utils.is_initiator_valid(item))
if False in results:
error_message = "One or more initiator provided is not valid, please provide valid initiators"
LOG.error(error_message)
self.module.fail_json(msg=error_message)
def get_host_initiators_list(self, host_details):
""" Get the list of existing initiators in host"""
existing_initiators = []
if host_details.fc_host_initiators is not None:
fc_len = len(host_details.fc_host_initiators)
if fc_len > 0:
for count in range(fc_len):
""" get initiator 'wwn' id"""
ini_id \
= host_details.fc_host_initiators.initiator_id[count]
""" update existing_initiators list with 'wwn' """
existing_initiators.append(ini_id)
if host_details.iscsi_host_initiators is not None:
iscsi_len = len(host_details.iscsi_host_initiators)
if iscsi_len > 0:
for count in range(iscsi_len):
""" get initiator 'iqn' id"""
ini_id \
= host_details.iscsi_host_initiators.\
initiator_id[count]
""" update existing_initiators list with 'iqn' """
existing_initiators.append(ini_id)
return existing_initiators
def is_host_modified(self, host_details):
""" Determines whether the Host details are to be updated or not """
LOG.info("Checking host attribute values.")
modified_flag = False
if (self.module.params['description'] is not None
and self.module.params['description']
!= host_details.description) \
or (self.module.params['host_os'] is not None
and self.module.params['host_os'] != host_details.os_type) \
or (self.module.params['new_host_name'] is not None
and self.module.params[
'new_host_name'] != host_details.name) \
or (self.module.params['initiators'] is not None
and self.module.params['initiators']
!= self.get_host_initiators_list(host_details)):
LOG.info("Modification required.")
modified_flag = True
return modified_flag
def modify_host(self, host_details, new_host_name=None, description=None,
host_os=None):
""" Modify a host """
try:
hosts = utils.host.UnityHostList.get(cli=self.unity._cli)
host_names_list = hosts.name
for name in host_names_list:
if new_host_name == name:
error_message = "Cannot modify name, new_host_name: " \
+ new_host_name + " already in use."
LOG.error(error_message)
self.module.fail_json(msg=error_message)
host_details.modify(name=new_host_name, desc=description,
os=host_os)
return True
except Exception as e:
error_message = "Got error %s while modifying host %s" \
% (str(e), host_details.name)
LOG.error(error_message)
self.module.fail_json(msg=error_message)
def get_list_unmapped_initiators(self, initiators, host_id=None):
""" Get the list of those initiators which are
not mapped to any host"""
unmapped_initiators = []
for id in initiators:
initiator_details = utils.host.UnityHostInitiatorList \
.get(cli=self.unity._cli, initiator_id=id) \
._get_properties()
""" if an already existing initiator is passed along with an
unmapped initiator"""
if None in initiator_details["parent_host"]:
unmapped_initiators.append(initiator_details
["initiator_id"][0])
elif not initiator_details["parent_host"]:
unmapped_initiators.append(id)
else:
error_message = "Initiator " + id + " mapped to another Host."
LOG.error(error_message)
self.module.fail_json(msg=error_message)
return unmapped_initiators
def add_initiator_to_host(self, host_details, initiators):
""" Add initiator to host """
try:
existing_initiators = self.get_host_initiators_list(host_details)
""" if current and exisitng initiators are same"""
if initiators \
and (set(initiators).issubset(set(existing_initiators))):
LOG.info("Initiators are already present in host: %s",
host_details.name)
return False, host_details
""" get the list of non-mapped initiators out of the
given initiators"""
host_id = host_details.id
unmapped_initiators \
= self.get_list_unmapped_initiators(initiators, host_id)
""" if any of the Initiators is invalid or already mapped """
if unmapped_initiators is None \
or len(unmapped_initiators) < len(initiators):
error_message = "Provide valid initiators."
LOG.error(error_message)
self.module.fail_json(msg=error_message)
LOG.info("Adding initiators to host %s", host_details.name)
for id in unmapped_initiators:
host_details.add_initiator(uid=id)
updated_host \
= self.unity.get_host(name=host_details.name)
return True, updated_host
except Exception as e:
error_message = "Got error %s while adding initiator to host %s" \
% (str(e), host_details.name)
LOG.error(error_message)
self.module.fail_json(msg=error_message)
def remove_initiator_from_host(self, host_details, initiators):
""" Remove initiator from host """
try:
existing_initiators = self.get_host_initiators_list(host_details)
if existing_initiators is None:
LOG.info("No exisiting initiators in host: %s",
host_details.name)
return False, host_details
if not (set(initiators).issubset(set(existing_initiators))):
LOG.info("Initiators already absent in host: %s",
host_details.name)
return False, host_details
LOG.info("Removing initiators from host %s", host_details.name)
if len(initiators) > 1:
self.check_if_initiators_logged_in(initiators)
for id in initiators:
initiator_details = utils.host.UnityHostInitiatorList \
.get(cli=self.unity._cli, initiator_id=id) \
._get_properties()
""" if initiator has no active paths, then remove it """
if initiator_details["paths"][0] is None:
LOG.info("Initiator Path does not exist.")
host_details.delete_initiator(uid=id)
updated_host \
= self.unity.get_host(name=host_details.name)
else:
""" Checking for initiator logged_in state """
for path in initiator_details["paths"][0]["UnityHostInitiatorPathList"]:
path_id = path["UnityHostInitiatorPath"]["id"]
path_id_obj = utils.host.UnityHostInitiatorPathList \
.get(cli=self.unity._cli, _id=path_id)
path_id_details = path_id_obj._get_properties()
""" if is_logged_in is True, can't remove initiator"""
if (path_id_details["is_logged_in"]):
error_message = "Cannot remove initiator "\
+ id + ", as it is logged in " \
"the with host."
LOG.error(error_message)
self.module.fail_json(msg=error_message)
elif (not path_id_details["is_logged_in"]):
""" if is_logged_in is False, remove initiator """
path_id_obj.delete()
else:
""" if logged_in state does not exist """
error_message = " logged_in state does not " \
"exist for initiator " + id + "."
LOG.error(error_message)
self.module.fail_json(msg=error_message)
host_details.delete_initiator(uid=id)
updated_host \
= self.unity.get_host(name=host_details.name)
return True, updated_host
except Exception as e:
error_message = "Got error %s while removing initiator from " \
"host %s" \
% (str(e), host_details.name)
LOG.error(error_message)
self.module.fail_json(msg=error_message)
def check_if_initiators_logged_in(self, initiators):
""" Checks if any of the initiators is of type logged-in"""
for item in initiators:
initiator_details = (utils.host.UnityHostInitiatorList
.get(cli=self.unity._cli, initiator_id=item)
._get_properties())
if initiator_details["paths"][0] is not None and "UnityHostInitiatorPathList" in initiator_details["paths"][0]:
error_message = "Removal operation cannot be done since host has logged in initiator(s)"
LOG.error(error_message)
self.module.fail_json(msg=error_message)
def delete_host(self, host_details):
""" Delete an existing host """
try:
host_details.delete()
return True
except Exception as e:
error_message = "Got error %s while deletion of host %s" \
% (str(e), host_details.name)
LOG.error(error_message)
self.module.fail_json(msg=error_message)
def get_iscsi_host_initiators_details(self, iscsi_host_initiators):
""" Get the details of existing ISCSI initiators in host"""
iscsi_initiator_list = []
for iscsi in iscsi_host_initiators:
iscsi_initiator_details = self.unity.get_initiator(_id=iscsi.id)
iscsi_path_list = []
if iscsi_initiator_details.paths is not None:
for path in iscsi_initiator_details.paths:
iscsi_path_list.append({
'id': path.id,
'is_logged_in': path.is_logged_in
})
iscsi_initiator_list.append({
'id': iscsi_initiator_details.id,
'name': iscsi_initiator_details.initiator_id,
'paths': iscsi_path_list
})
return iscsi_initiator_list
def get_host_network_address_list(self, host_details):
network_address_list = []
if host_details and host_details.host_ip_ports is not None:
for port in host_details.host_ip_ports:
network_address_list.append(port.address)
return network_address_list
def manage_network_address(self, host_details, network_address_list,
network_address, network_address_state):
try:
is_mapped = False
changed = False
for addr in network_address_list:
if addr.lower() == network_address.lower():
is_mapped = True
break
if not is_mapped and network_address_state == 'present-in-host':
LOG.info("Adding network address %s to Host %s", network_address,
host_details.name)
host_details.add_ip_port(network_address)
changed = True
elif is_mapped and network_address_state == 'absent-in-host':
LOG.info("Deleting network address %s from Host %s", network_address,
host_details.name)
host_details.delete_ip_port(network_address)
changed = True
if changed:
updated_host = self.unity.get_host(name=host_details.name)
network_address_list = self.get_host_network_address_list(updated_host)
return network_address_list, changed
except Exception as e:
error_message = "Got error %s while modifying network address %s of host %s" \
% (str(e), network_address, host_details.name)
LOG.error(error_message)
self.module.fail_json(msg=error_message)
def get_host_lun_list(self, host_details):
""" Get luns attached to host"""
host_luns_list = []
if host_details and host_details.host_luns is not None:
for lun in host_details.host_luns.lun:
host_lun = {"name": lun.name, "id": lun.id}
host_luns_list.append(host_lun)
return host_luns_list
def get_fc_host_initiators_details(self, fc_host_initiators):
""" Get the details of existing FC initiators in host"""
fc_initiator_list = []
for fc in fc_host_initiators:
fc_initiator_details = self.unity.get_initiator(_id=fc.id)
fc_path_list = []
if fc_initiator_details.paths is not None:
for path in fc_initiator_details.paths:
fc_path_list.append({
'id': path.id,
'is_logged_in': path.is_logged_in
})
fc_initiator_list.append({
'id': fc_initiator_details.id,
'name': fc_initiator_details.initiator_id,
'paths': fc_path_list
})
return fc_initiator_list
def perform_module_operation(self):
""" Perform different actions on host based on user parameter
chosen in playbook """
host_name = self.module.params['host_name']
host_id = self.module.params['host_id']
description = self.module.params['description']
host_os = self.module.params['host_os']
new_host_name = self.module.params['new_host_name']
initiator_state = self.module.params['initiator_state']
initiators = self.module.params['initiators']
network_address = self.module.params['network_address']
network_address_state = self.module.params['network_address_state']
state = self.module.params['state']
if host_name and len(host_name) > 255:
err_msg = "'host_name' is greater than 255 characters."
LOG.error(err_msg)
self.module.fail_json(msg=err_msg)
if new_host_name and len(new_host_name) > 255:
err_msg = "'new_host_name' is greater than 255 characters."
LOG.error(err_msg)
self.module.fail_json(msg=err_msg)
if description and len(description) > 255:
err_msg = "'description' is greater than 255 characters."
LOG.error(err_msg)
self.module.fail_json(msg=err_msg)
if not initiators and initiator_state:
err_msg = "'initiator_state' is given, " \
"'initiators' are not specified"
LOG.error(err_msg)
self.module.fail_json(msg=err_msg)
if not initiator_state and initiators:
err_msg = "'initiators' are given, " \
"'initiator_state' is not specified"
LOG.error(err_msg)
self.module.fail_json(msg=err_msg)
# result is a dictionary that contains changed status and
# host details
result = dict(
changed=False,
host_details={}
)
''' Get host details based on host_name/host_id'''
host_details = self.get_host_details(host_id, host_name)
if not host_details and state == 'present':
if host_id:
err_msg = "Invalid argument 'host_id' while " \
"creating a host"
LOG.error(err_msg)
self.module.fail_json(msg=err_msg)
if not host_name:
err_msg = "host_name is required to create a host"
LOG.error(err_msg)
self.module.fail_json(msg=err_msg)
if new_host_name:
err_msg = "Invalid argument 'new_host_name' while " \
"creating a host"
LOG.error(err_msg)
self.module.fail_json(msg=err_msg)
if (initiators and initiator_state == 'absent-in-host'):
error_message = "Incorrect 'initiator_state' given."
LOG.error(error_message)
self.module.fail_json(msg=error_message)
# Create new host
LOG.info("Creating host: %s", host_name)
result['changed'], host_details \
= self.create_host(host_name)
result['host_details'] = host_details._get_properties()
# Modify host (Attributes and ADD/REMOVE Initiators)
elif (state == 'present' and host_details):
modified_flag = self.is_host_modified(host_details)
if modified_flag:
# Modify host
result['changed'] = self.modify_host(host_details,
new_host_name,
description,
host_os)
if new_host_name:
host_details = self.get_host_details(host_id,
new_host_name)
else:
host_details = self.get_host_details(host_id, host_name)
result['host_details'] = host_details._get_properties()
# Add Initiators to host
if (initiator_state == 'present-in-host' and initiators
and len(initiators) > 0):
LOG.info("Adding Initiators to Host %s",
host_details.name)
result['changed'], host_details \
= self.add_initiator_to_host(host_details, initiators)
result['host_details'] = host_details._get_properties()
else:
LOG.info('Host modification is not applicable, '
'as none of the attributes has changed.')
result['changed'] = False
result['host_details'] = host_details._get_properties()
# Remove initiators from host
if (host_details and initiator_state == 'absent-in-host'
and initiators and len(initiators) > 0):
LOG.info("Removing Initiators from Host %s",
host_details.name)
result['changed'], host_details \
= self.remove_initiator_from_host(host_details,
initiators)
result['host_details'] = host_details._get_properties()
""" display WWN/IQN w.r.t. initiators mapped to host,
if host exists """
if host_details and host_details.fc_host_initiators is not None:
host_details.fc_host_initiators = self.get_fc_host_initiators_details(host_details.fc_host_initiators)
result['host_details'] = host_details._get_properties()
if host_details and host_details.iscsi_host_initiators is not None:
host_details.iscsi_host_initiators = self.get_iscsi_host_initiators_details(host_details.iscsi_host_initiators)
result['host_details'] = host_details._get_properties()
''' Get host luns details and network addresses'''
if result['host_details']:
result['host_details']['host_luns'] = self.get_host_lun_list(host_details)
result['host_details']['network_addresses'] = self.get_host_network_address_list(host_details)
if 'host_ip_ports' in result['host_details']:
del result['host_details']['host_ip_ports']
# manage network address
if host_details is not None and network_address_state is not None:
self.validate_network_address_params(network_address)
network_address_list, changed = self.manage_network_address(
host_details,
result['host_details']['network_addresses'],
network_address,
network_address_state)
result['host_details']['network_addresses'] = network_address_list
result['changed'] = changed
# Delete a host
if state == 'absent':
if host_details:
LOG.info("Deleting host %s", host_details.name)
result['changed'] = self.delete_host(host_details)
else:
result['changed'] = False
result['host_details'] = []
self.module.exit_json(**result)
def validate_network_address_params(self, network_address):
if '.' in network_address and not is_valid_ip(network_address):
err_msg = 'Please enter valid IPV4 address for network address'
LOG.error(err_msg)
self.module.fail_json(msg=err_msg)
if len(network_address) < 1 or len(network_address) > 63:
err_msg = "'network_address' should be in range of 1 to 63 characters."
LOG.error(err_msg)
self.module.fail_json(msg=err_msg)
if utils.has_special_char(network_address) or ' ' in network_address:
err_msg = 'Please enter valid IPV4 address or host name for network address'
LOG.error(err_msg)
self.module.fail_json(msg=err_msg)
def is_valid_ip(address):
try:
ipaddress.ip_address(address)
return True
except ValueError:
return False
def get_host_parameters():
"""This method provides parameters required for the ansible host
module on Unity"""
return dict(
host_name=dict(required=False, type='str'),
host_id=dict(required=False, type='str'),
description=dict(required=False, type='str'),
host_os=dict(required=False, type='str',
choices=['AIX', 'Citrix XenServer', 'HP-UX',
'IBM VIOS', 'Linux', 'Mac OS', 'Solaris',
'VMware ESXi', 'Windows Client',
'Windows Server']),
new_host_name=dict(required=False, type='str'),
initiators=dict(required=False, type='list', elements='str'),
initiator_state=dict(required=False, type='str',
choices=['present-in-host',
'absent-in-host']),
network_address=dict(required=False, type='str'),
network_address_state=dict(required=False, type='str',
choices=['present-in-host',
'absent-in-host']),
state=dict(required=True, type='str',
choices=['present', 'absent'])
)
def main():
""" Create Unity host object and perform action on it
based on user input from playbook"""
obj = Host()
obj.perform_module_operation()
if __name__ == '__main__':
main()
|