| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
"""Ansible module for managing host on Unity""" |
| 6 |
|
| 7 |
from __future__ import absolute_import, division, print_function |
| 8 |
|
| 9 |
__metaclass__ = type |
| 10 |
|
| 11 |
DOCUMENTATION = r''' |
| 12 |
--- |
| 13 |
module: host |
| 14 |
|
| 15 |
version_added: '1.1.0' |
| 16 |
|
| 17 |
short_description: Manage Host operations on Unity |
| 18 |
|
| 19 |
description: |
| 20 |
- The Host module contains the operations |
| 21 |
Creation of a Host, |
| 22 |
Addition of initiators to Host, |
| 23 |
Removal of initiators from Host, |
| 24 |
Modification of host attributes, |
| 25 |
Get details of a Host, |
| 26 |
Deletion of a Host, |
| 27 |
Addition of network address to Host, |
| 28 |
Removal of network address from Host. |
| 29 |
|
| 30 |
extends_documentation_fragment: |
| 31 |
- dellemc.unity.unity |
| 32 |
|
| 33 |
author: |
| 34 |
- Rajshree Khare (@kharer5) <[email protected]> |
| 35 |
|
| 36 |
options: |
| 37 |
host_name: |
| 38 |
description: |
| 39 |
- Name of the host. |
| 40 |
- Mandatory for host creation. |
| 41 |
type: str |
| 42 |
|
| 43 |
host_id: |
| 44 |
description: |
| 45 |
- Unique identifier of the host. |
| 46 |
- Host Id is auto generated during creation. |
| 47 |
- Except create, all other operations require either I(host_id) or Ihost_name). |
| 48 |
type: str |
| 49 |
|
| 50 |
description: |
| 51 |
description: |
| 52 |
- Host description. |
| 53 |
type: str |
| 54 |
|
| 55 |
host_os: |
| 56 |
description: |
| 57 |
- Operating system running on the host. |
| 58 |
choices: ['AIX', 'Citrix XenServer', 'HP-UX', 'IBM VIOS', 'Linux', |
| 59 |
'Mac OS', 'Solaris', 'VMware ESXi', 'Windows Client', 'Windows Server'] |
| 60 |
type: str |
| 61 |
|
| 62 |
new_host_name: |
| 63 |
description: |
| 64 |
- New name for the host. |
| 65 |
- Only required in rename host operation. |
| 66 |
type: str |
| 67 |
|
| 68 |
initiators: |
| 69 |
description: |
| 70 |
- List of initiators to be added/removed to/from host. |
| 71 |
type: list |
| 72 |
elements: str |
| 73 |
|
| 74 |
initiator_state: |
| 75 |
description: |
| 76 |
- State of the initiator. |
| 77 |
choices: [present-in-host , absent-in-host] |
| 78 |
type: str |
| 79 |
|
| 80 |
network_address: |
| 81 |
description: |
| 82 |
- Network address to be added/removed to/from the host. |
| 83 |
- Enter valid IPV4 or host name. |
| 84 |
type: str |
| 85 |
|
| 86 |
network_address_state: |
| 87 |
description: |
| 88 |
- State of the Network address. |
| 89 |
choices: [present-in-host , absent-in-host] |
| 90 |
type: str |
| 91 |
|
| 92 |
state: |
| 93 |
description: |
| 94 |
- State of the host. |
| 95 |
choices: [present , absent] |
| 96 |
type: str |
| 97 |
required: true |
| 98 |
|
| 99 |
notes: |
| 100 |
- The I(check_mode) is not supported. |
| 101 |
''' |
| 102 |
|
| 103 |
EXAMPLES = r''' |
| 104 |
- name: Create empty Host |
| 105 |
host: |
| 106 |
unispherehost: "{{unispherehost}}" |
| 107 |
username: "{{username}}" |
| 108 |
password: "{{password}}" |
| 109 |
validate_certs: "{{validate_certs}}" |
| 110 |
host_name: "ansible-test-host" |
| 111 |
host_os: "Linux" |
| 112 |
description: "ansible-test-host" |
| 113 |
state: "present" |
| 114 |
|
| 115 |
- name: Create Host with Initiators |
| 116 |
host: |
| 117 |
unispherehost: "{{unispherehost}}" |
| 118 |
username: "{{username}}" |
| 119 |
password: "{{password}}" |
| 120 |
validate_certs: "{{validate_certs}}" |
| 121 |
host_name: "ansible-test-host-1" |
| 122 |
host_os: "Linux" |
| 123 |
description: "ansible-test-host-1" |
| 124 |
initiators: |
| 125 |
- "iqn.1994-05.com.redhat:c38e6e8cfd81" |
| 126 |
- "20:00:00:90:FA:13:81:8D:10:00:00:90:FA:13:81:8D" |
| 127 |
initiator_state: "present-in-host" |
| 128 |
state: "present" |
| 129 |
|
| 130 |
- name: Modify Host using host_id |
| 131 |
host: |
| 132 |
unispherehost: "{{unispherehost}}" |
| 133 |
username: "{{username}}" |
| 134 |
password: "{{password}}" |
| 135 |
validate_certs: "{{validate_certs}}" |
| 136 |
host_id: "Host_253" |
| 137 |
new_host_name: "ansible-test-host-2" |
| 138 |
host_os: "Mac OS" |
| 139 |
description: "Ansible tesing purpose" |
| 140 |
state: "present" |
| 141 |
|
| 142 |
- name: Add Initiators to Host |
| 143 |
host: |
| 144 |
unispherehost: "{{unispherehost}}" |
| 145 |
username: "{{username}}" |
| 146 |
password: "{{password}}" |
| 147 |
validate_certs: "{{validate_certs}}" |
| 148 |
host_name: "ansible-test-host-2" |
| 149 |
initiators: |
| 150 |
- "20:00:00:90:FA:13:81:8C:10:00:00:90:FA:13:81:8C" |
| 151 |
initiator_state: "present-in-host" |
| 152 |
state: "present" |
| 153 |
|
| 154 |
- name: Get Host details using host_name |
| 155 |
host: |
| 156 |
unispherehost: "{{unispherehost}}" |
| 157 |
username: "{{username}}" |
| 158 |
password: "{{password}}" |
| 159 |
validate_certs: "{{validate_certs}}" |
| 160 |
host_name: "ansible-test-host-2" |
| 161 |
state: "present" |
| 162 |
|
| 163 |
- name: Get Host details using host_id |
| 164 |
host: |
| 165 |
unispherehost: "{{unispherehost}}" |
| 166 |
username: "{{username}}" |
| 167 |
password: "{{password}}" |
| 168 |
validate_certs: "{{validate_certs}}" |
| 169 |
host_id: "Host_253" |
| 170 |
state: "present" |
| 171 |
|
| 172 |
- name: Delete Host |
| 173 |
host: |
| 174 |
unispherehost: "{{unispherehost}}" |
| 175 |
username: "{{username}}" |
| 176 |
password: "{{password}}" |
| 177 |
validate_certs: "{{validate_certs}}" |
| 178 |
host_name: "ansible-test-host-2" |
| 179 |
state: "absent" |
| 180 |
|
| 181 |
- name: Add network address to Host |
| 182 |
host: |
| 183 |
unispherehost: "{{unispherehost}}" |
| 184 |
username: "{{username}}" |
| 185 |
password: "{{password}}" |
| 186 |
validate_certs: "{{validate_certs}}" |
| 187 |
host_name: "{{host_name}}" |
| 188 |
network_address: "192.168.1.2" |
| 189 |
network_address_state: "present-in-host" |
| 190 |
state: "present" |
| 191 |
|
| 192 |
- name: Delete network address from Host |
| 193 |
host: |
| 194 |
unispherehost: "{{unispherehost}}" |
| 195 |
username: "{{username}}" |
| 196 |
password: "{{password}}" |
| 197 |
validate_certs: "{{validate_certs}}" |
| 198 |
host_name: "{{host_name}}" |
| 199 |
network_address: "192.168.1.2" |
| 200 |
network_address_state: "absent-in-host" |
| 201 |
state: "present" |
| 202 |
''' |
| 203 |
|
| 204 |
RETURN = r''' |
| 205 |
changed: |
| 206 |
description: Whether or not the resource has changed. |
| 207 |
returned: always |
| 208 |
type: bool |
| 209 |
sample: true |
| 210 |
|
| 211 |
host_details: |
| 212 |
description: Details of the host. |
| 213 |
returned: When host exists. |
| 214 |
type: dict |
| 215 |
contains: |
| 216 |
id: |
| 217 |
description: The system ID given to the host. |
| 218 |
type: str |
| 219 |
name: |
| 220 |
description: The name of the host. |
| 221 |
type: str |
| 222 |
description: |
| 223 |
description: Description about the host. |
| 224 |
type: str |
| 225 |
fc_host_initiators: |
| 226 |
description: Details of the FC initiators associated with |
| 227 |
the host. |
| 228 |
type: list |
| 229 |
contains: |
| 230 |
id: |
| 231 |
description: Unique identifier of the FC initiator path. |
| 232 |
type: str |
| 233 |
name: |
| 234 |
description: FC Qualified Name (WWN) of the initiator. |
| 235 |
type: str |
| 236 |
paths: |
| 237 |
description: Details of the paths associated with the FC initiator. |
| 238 |
type: list |
| 239 |
contains: |
| 240 |
id: |
| 241 |
description: Unique identifier of the path. |
| 242 |
type: str |
| 243 |
is_logged_in: |
| 244 |
description: Indicates whether the host initiator is logged into the storage system. |
| 245 |
type: bool |
| 246 |
iscsi_host_initiators: |
| 247 |
description: Details of the ISCSI initiators associated |
| 248 |
with the host. |
| 249 |
type: list |
| 250 |
contains: |
| 251 |
id: |
| 252 |
description: Unique identifier of the ISCSI initiator path. |
| 253 |
type: str |
| 254 |
name: |
| 255 |
description: ISCSI Qualified Name (IQN) of the initiator. |
| 256 |
type: str |
| 257 |
paths: |
| 258 |
description: Details of the paths associated with the ISCSI initiator. |
| 259 |
type: list |
| 260 |
contains: |
| 261 |
id: |
| 262 |
description: Unique identifier of the path. |
| 263 |
type: str |
| 264 |
is_logged_in: |
| 265 |
description: Indicates whether the host initiator is logged into the storage system. |
| 266 |
type: bool |
| 267 |
network_addresses: |
| 268 |
description: List of network addresses mapped to the host. |
| 269 |
type: list |
| 270 |
os_type: |
| 271 |
description: Operating system running on the host. |
| 272 |
type: str |
| 273 |
type: |
| 274 |
description: HostTypeEnum of the host. |
| 275 |
type: str |
| 276 |
host_luns: |
| 277 |
description: Details of luns attached to host. |
| 278 |
type: list |
| 279 |
sample: { |
| 280 |
"auto_manage_type": "HostManageEnum.UNKNOWN", |
| 281 |
"datastores": null, |
| 282 |
"description": "ansible-test-host-1", |
| 283 |
"existed": true, |
| 284 |
"fc_host_initiators": [ |
| 285 |
{ |
| 286 |
"id": "HostInitiator_1", |
| 287 |
"name": "HostName_1", |
| 288 |
"paths": [ |
| 289 |
{ |
| 290 |
"id": "HostInitiator_1_Id1", |
| 291 |
"is_logged_in": true |
| 292 |
}, |
| 293 |
{ |
| 294 |
"id": "HostInitiator_1_Id2", |
| 295 |
"is_logged_in": true |
| 296 |
} |
| 297 |
] |
| 298 |
} |
| 299 |
], |
| 300 |
"hash": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", |
| 301 |
"health": { |
| 302 |
"UnityHealth": { |
| 303 |
"hash": 8764429420954 |
| 304 |
} |
| 305 |
}, |
| 306 |
"host_container": null, |
| 307 |
"host_luns": [], |
| 308 |
"host_polled_uuid": null, |
| 309 |
"host_pushed_uuid": null, |
| 310 |
"host_uuid": null, |
| 311 |
"host_v_vol_datastore": null, |
| 312 |
"id": "Host_2198", |
| 313 |
"iscsi_host_initiators": [ |
| 314 |
{ |
| 315 |
"id": "HostInitiator_2", |
| 316 |
"name": "HostName_2", |
| 317 |
"paths": [ |
| 318 |
{ |
| 319 |
"id": "HostInitiator_2_Id1", |
| 320 |
"is_logged_in": true |
| 321 |
}, |
| 322 |
{ |
| 323 |
"id": "HostInitiator_2_Id2", |
| 324 |
"is_logged_in": true |
| 325 |
} |
| 326 |
] |
| 327 |
} |
| 328 |
], |
| 329 |
"last_poll_time": null, |
| 330 |
"name": "ansible-test-host-1", |
| 331 |
"network_addresses": [], |
| 332 |
"os_type": "Linux", |
| 333 |
"registration_type": null, |
| 334 |
"storage_resources": null, |
| 335 |
"tenant": null, |
| 336 |
"type": "HostTypeEnum.HOST_MANUAL", |
| 337 |
"vms": null |
| 338 |
} |
| 339 |
''' |
| 340 |
|
| 341 |
|
| 342 |
from ansible.module_utils.basic import AnsibleModule |
| 343 |
from ansible_collections.dellemc.unity.plugins.module_utils.storage.dell \ |
| 344 |
import utils |
| 345 |
import ipaddress |
| 346 |
|
| 347 |
LOG = utils.get_logger('host') |
| 348 |
|
| 349 |
application_type = "Ansible/1.7.1" |
| 350 |
|
| 351 |
|
| 352 |
class Host(object): |
| 353 |
"""Class with Host operations""" |
| 354 |
|
| 355 |
def __init__(self): |
| 356 |
""" Define all parameters required by this module""" |
| 357 |
|
| 358 |
self.module_params = utils.get_unity_management_host_parameters() |
| 359 |
self.module_params.update(get_host_parameters()) |
| 360 |
|
| 361 |
mutually_exclusive = [['host_name', 'host_id']] |
| 362 |
required_one_of = [['host_name', 'host_id']] |
| 363 |
required_together = [['network_address', 'network_address_state']] |
| 364 |
|
| 365 |
""" initialize the ansible module """ |
| 366 |
self.module = AnsibleModule(argument_spec=self.module_params, |
| 367 |
supports_check_mode=False, |
| 368 |
mutually_exclusive=mutually_exclusive, |
| 369 |
required_together=required_together, |
| 370 |
required_one_of=required_one_of) |
| 371 |
utils.ensure_required_libs(self.module) |
| 372 |
|
| 373 |
self.unity = utils.get_unity_unisphere_connection(self.module.params, application_type) |
| 374 |
LOG.info('Got the unity instance for provisioning on Unity') |
| 375 |
|
| 376 |
def get_host_count(self, host_name): |
| 377 |
""" To get the count of hosts with same host_name """ |
| 378 |
|
| 379 |
hosts = [] |
| 380 |
host_count = 0 |
| 381 |
hosts = utils.host.UnityHostList.get(cli=self.unity._cli, |
| 382 |
name=host_name) |
| 383 |
host_count = len(hosts) |
| 384 |
return host_count |
| 385 |
|
| 386 |
def get_host_details(self, host_id=None, host_name=None): |
| 387 |
""" Get details of a given host """ |
| 388 |
|
| 389 |
host_id_or_name = host_id if host_id else host_name |
| 390 |
try: |
| 391 |
LOG.info("Getting host %s details", host_id_or_name) |
| 392 |
if host_id: |
| 393 |
host_details = self.unity.get_host(_id=host_id) |
| 394 |
if host_details.name is None: |
| 395 |
return None |
| 396 |
if host_name: |
| 397 |
|
| 398 |
''' get the count of hosts with same host_name ''' |
| 399 |
host_count = self.get_host_count(host_name) |
| 400 |
|
| 401 |
if host_count < 1: |
| 402 |
return None |
| 403 |
elif host_count > 1: |
| 404 |
error_message = "Duplicate hosts found: There are "\ |
| 405 |
+ host_count + " hosts(s) with the same" \ |
| 406 |
" host_name: " + host_name |
| 407 |
LOG.error(error_message) |
| 408 |
self.module.fail_json(msg=error_message) |
| 409 |
else: |
| 410 |
host_details = self.unity.get_host(name=host_name) |
| 411 |
|
| 412 |
return host_details |
| 413 |
except utils.HttpError as e: |
| 414 |
if e.http_status == 401: |
| 415 |
msg = 'Incorrect username or password provided.' |
| 416 |
LOG.error(msg) |
| 417 |
self.module.fail_json(msg=msg) |
| 418 |
else: |
| 419 |
msg = "Got HTTP Connection Error while getting host " \ |
| 420 |
"details %s : Error %s " % (host_id_or_name, str(e)) |
| 421 |
LOG.error(msg) |
| 422 |
self.module.fail_json(msg=msg) |
| 423 |
except utils.UnityResourceNotFoundError as e: |
| 424 |
error_message = "Failed to get details of host " \ |
| 425 |
"{0} with error {1}".format(host_id_or_name, |
| 426 |
str(e)) |
| 427 |
LOG.error(error_message) |
| 428 |
return None |
| 429 |
except Exception as e: |
| 430 |
error_message = "Got error %s while getting details of host %s" \ |
| 431 |
% (str(e), host_id_or_name) |
| 432 |
LOG.error(error_message) |
| 433 |
self.module.fail_json(msg=error_message) |
| 434 |
|
| 435 |
def create_host(self, host_name): |
| 436 |
""" Create a new host """ |
| 437 |
try: |
| 438 |
description = self.module.params['description'] |
| 439 |
host_os = self.module.params['host_os'] |
| 440 |
host_type = utils.HostTypeEnum.HOST_MANUAL |
| 441 |
initiators = self.module.params['initiators'] |
| 442 |
initiator_state = self.module.params['initiator_state'] |
| 443 |
empty_initiators_flag = False |
| 444 |
|
| 445 |
if (initiators and initiator_state == 'absent-in-host'): |
| 446 |
error_message = "Incorrect 'initiator_state' given." |
| 447 |
LOG.error(error_message) |
| 448 |
self.module.fail_json(msg=error_message) |
| 449 |
|
| 450 |
if (initiators is None or len(initiators) == 0 |
| 451 |
or not initiator_state |
| 452 |
or initiator_state == 'absent-in-host'): |
| 453 |
empty_initiators_flag = True |
| 454 |
|
| 455 |
""" if any of the Initiators is invalid or already mapped """ |
| 456 |
if (initiators and initiator_state == 'present-in-host'): |
| 457 |
unmapped_initiators \ |
| 458 |
= self.get_list_unmapped_initiators(initiators) |
| 459 |
if unmapped_initiators is None \ |
| 460 |
or len(unmapped_initiators) < len(initiators): |
| 461 |
error_message = "Provide valid initiators." |
| 462 |
LOG.error(error_message) |
| 463 |
self.module.fail_json(msg=error_message) |
| 464 |
if not empty_initiators_flag: |
| 465 |
self.validate_initiators(initiators) |
| 466 |
LOG.info("Creating empty host %s ", host_name) |
| 467 |
new_host = utils.host.UnityHost.create(self.unity._cli, name=host_name, desc=description, |
| 468 |
os=host_os, host_type=host_type) |
| 469 |
if not empty_initiators_flag: |
| 470 |
host_details = self.unity.get_host(name=host_name) |
| 471 |
LOG.info("Adding initiators to %s host", host_name) |
| 472 |
result, new_host \ |
| 473 |
= self.add_initiator_to_host(host_details, initiators) |
| 474 |
return True, new_host |
| 475 |
except Exception as e: |
| 476 |
error_message = "Got error %s while creation of host %s" \ |
| 477 |
% (str(e), host_name) |
| 478 |
LOG.error(error_message) |
| 479 |
self.module.fail_json(msg=error_message) |
| 480 |
|
| 481 |
def validate_initiators(self, initiators): |
| 482 |
results = [] |
| 483 |
for item in initiators: |
| 484 |
results.append(utils.is_initiator_valid(item)) |
| 485 |
if False in results: |
| 486 |
error_message = "One or more initiator provided is not valid, please provide valid initiators" |
| 487 |
LOG.error(error_message) |
| 488 |
self.module.fail_json(msg=error_message) |
| 489 |
|
| 490 |
def get_host_initiators_list(self, host_details): |
| 491 |
""" Get the list of existing initiators in host""" |
| 492 |
|
| 493 |
existing_initiators = [] |
| 494 |
if host_details.fc_host_initiators is not None: |
| 495 |
fc_len = len(host_details.fc_host_initiators) |
| 496 |
if fc_len > 0: |
| 497 |
for count in range(fc_len): |
| 498 |
""" get initiator 'wwn' id""" |
| 499 |
ini_id \ |
| 500 |
= host_details.fc_host_initiators.initiator_id[count] |
| 501 |
|
| 502 |
""" update existing_initiators list with 'wwn' """ |
| 503 |
existing_initiators.append(ini_id) |
| 504 |
|
| 505 |
if host_details.iscsi_host_initiators is not None: |
| 506 |
iscsi_len = len(host_details.iscsi_host_initiators) |
| 507 |
if iscsi_len > 0: |
| 508 |
for count in range(iscsi_len): |
| 509 |
""" get initiator 'iqn' id""" |
| 510 |
ini_id \ |
| 511 |
= host_details.iscsi_host_initiators.\ |
| 512 |
initiator_id[count] |
| 513 |
|
| 514 |
""" update existing_initiators list with 'iqn' """ |
| 515 |
existing_initiators.append(ini_id) |
| 516 |
return existing_initiators |
| 517 |
|
| 518 |
def is_host_modified(self, host_details): |
| 519 |
""" Determines whether the Host details are to be updated or not """ |
| 520 |
LOG.info("Checking host attribute values.") |
| 521 |
modified_flag = False |
| 522 |
|
| 523 |
if (self.module.params['description'] is not None |
| 524 |
and self.module.params['description'] |
| 525 |
!= host_details.description) \ |
| 526 |
or (self.module.params['host_os'] is not None |
| 527 |
and self.module.params['host_os'] != host_details.os_type) \ |
| 528 |
or (self.module.params['new_host_name'] is not None |
| 529 |
and self.module.params[ |
| 530 |
'new_host_name'] != host_details.name) \ |
| 531 |
or (self.module.params['initiators'] is not None |
| 532 |
and self.module.params['initiators'] |
| 533 |
!= self.get_host_initiators_list(host_details)): |
| 534 |
LOG.info("Modification required.") |
| 535 |
modified_flag = True |
| 536 |
|
| 537 |
return modified_flag |
| 538 |
|
| 539 |
def modify_host(self, host_details, new_host_name=None, description=None, |
| 540 |
host_os=None): |
| 541 |
""" Modify a host """ |
| 542 |
try: |
| 543 |
hosts = utils.host.UnityHostList.get(cli=self.unity._cli) |
| 544 |
host_names_list = hosts.name |
| 545 |
for name in host_names_list: |
| 546 |
if new_host_name == name: |
| 547 |
error_message = "Cannot modify name, new_host_name: " \ |
| 548 |
+ new_host_name + " already in use." |
| 549 |
LOG.error(error_message) |
| 550 |
self.module.fail_json(msg=error_message) |
| 551 |
host_details.modify(name=new_host_name, desc=description, |
| 552 |
os=host_os) |
| 553 |
return True |
| 554 |
|
| 555 |
except Exception as e: |
| 556 |
error_message = "Got error %s while modifying host %s" \ |
| 557 |
% (str(e), host_details.name) |
| 558 |
LOG.error(error_message) |
| 559 |
self.module.fail_json(msg=error_message) |
| 560 |
|
| 561 |
def get_list_unmapped_initiators(self, initiators, host_id=None): |
| 562 |
""" Get the list of those initiators which are |
| 563 |
not mapped to any host""" |
| 564 |
|
| 565 |
unmapped_initiators = [] |
| 566 |
for id in initiators: |
| 567 |
initiator_details = utils.host.UnityHostInitiatorList \ |
| 568 |
.get(cli=self.unity._cli, initiator_id=id) \ |
| 569 |
._get_properties() |
| 570 |
|
| 571 |
""" if an already existing initiator is passed along with an |
| 572 |
unmapped initiator""" |
| 573 |
if None in initiator_details["parent_host"]: |
| 574 |
unmapped_initiators.append(initiator_details |
| 575 |
["initiator_id"][0]) |
| 576 |
elif not initiator_details["parent_host"]: |
| 577 |
unmapped_initiators.append(id) |
| 578 |
else: |
| 579 |
error_message = "Initiator " + id + " mapped to another Host." |
| 580 |
LOG.error(error_message) |
| 581 |
self.module.fail_json(msg=error_message) |
| 582 |
return unmapped_initiators |
| 583 |
|
| 584 |
def add_initiator_to_host(self, host_details, initiators): |
| 585 |
""" Add initiator to host """ |
| 586 |
|
| 587 |
try: |
| 588 |
existing_initiators = self.get_host_initiators_list(host_details) |
| 589 |
|
| 590 |
""" if current and exisitng initiators are same""" |
| 591 |
if initiators \ |
| 592 |
and (set(initiators).issubset(set(existing_initiators))): |
| 593 |
LOG.info("Initiators are already present in host: %s", |
| 594 |
host_details.name) |
| 595 |
return False, host_details |
| 596 |
|
| 597 |
""" get the list of non-mapped initiators out of the |
| 598 |
given initiators""" |
| 599 |
host_id = host_details.id |
| 600 |
unmapped_initiators \ |
| 601 |
= self.get_list_unmapped_initiators(initiators, host_id) |
| 602 |
|
| 603 |
""" if any of the Initiators is invalid or already mapped """ |
| 604 |
if unmapped_initiators is None \ |
| 605 |
or len(unmapped_initiators) < len(initiators): |
| 606 |
error_message = "Provide valid initiators." |
| 607 |
LOG.error(error_message) |
| 608 |
self.module.fail_json(msg=error_message) |
| 609 |
|
| 610 |
LOG.info("Adding initiators to host %s", host_details.name) |
| 611 |
for id in unmapped_initiators: |
| 612 |
host_details.add_initiator(uid=id) |
| 613 |
updated_host \ |
| 614 |
= self.unity.get_host(name=host_details.name) |
| 615 |
return True, updated_host |
| 616 |
|
| 617 |
except Exception as e: |
| 618 |
error_message = "Got error %s while adding initiator to host %s" \ |
| 619 |
% (str(e), host_details.name) |
| 620 |
LOG.error(error_message) |
| 621 |
self.module.fail_json(msg=error_message) |
| 622 |
|
| 623 |
def remove_initiator_from_host(self, host_details, initiators): |
| 624 |
""" Remove initiator from host """ |
| 625 |
|
| 626 |
try: |
| 627 |
existing_initiators = self.get_host_initiators_list(host_details) |
| 628 |
|
| 629 |
if existing_initiators is None: |
| 630 |
LOG.info("No exisiting initiators in host: %s", |
| 631 |
host_details.name) |
| 632 |
return False, host_details |
| 633 |
|
| 634 |
if not (set(initiators).issubset(set(existing_initiators))): |
| 635 |
LOG.info("Initiators already absent in host: %s", |
| 636 |
host_details.name) |
| 637 |
return False, host_details |
| 638 |
|
| 639 |
LOG.info("Removing initiators from host %s", host_details.name) |
| 640 |
|
| 641 |
if len(initiators) > 1: |
| 642 |
self.check_if_initiators_logged_in(initiators) |
| 643 |
|
| 644 |
for id in initiators: |
| 645 |
initiator_details = utils.host.UnityHostInitiatorList \ |
| 646 |
.get(cli=self.unity._cli, initiator_id=id) \ |
| 647 |
._get_properties() |
| 648 |
|
| 649 |
""" if initiator has no active paths, then remove it """ |
| 650 |
if initiator_details["paths"][0] is None: |
| 651 |
LOG.info("Initiator Path does not exist.") |
| 652 |
host_details.delete_initiator(uid=id) |
| 653 |
updated_host \ |
| 654 |
= self.unity.get_host(name=host_details.name) |
| 655 |
|
| 656 |
else: |
| 657 |
""" Checking for initiator logged_in state """ |
| 658 |
for path in initiator_details["paths"][0]["UnityHostInitiatorPathList"]: |
| 659 |
path_id = path["UnityHostInitiatorPath"]["id"] |
| 660 |
|
| 661 |
path_id_obj = utils.host.UnityHostInitiatorPathList \ |
| 662 |
.get(cli=self.unity._cli, _id=path_id) |
| 663 |
|
| 664 |
path_id_details = path_id_obj._get_properties() |
| 665 |
|
| 666 |
""" if is_logged_in is True, can't remove initiator""" |
| 667 |
if (path_id_details["is_logged_in"]): |
| 668 |
error_message = "Cannot remove initiator "\ |
| 669 |
+ id + ", as it is logged in " \ |
| 670 |
"the with host." |
| 671 |
LOG.error(error_message) |
| 672 |
self.module.fail_json(msg=error_message) |
| 673 |
|
| 674 |
elif (not path_id_details["is_logged_in"]): |
| 675 |
""" if is_logged_in is False, remove initiator """ |
| 676 |
path_id_obj.delete() |
| 677 |
|
| 678 |
else: |
| 679 |
""" if logged_in state does not exist """ |
| 680 |
error_message = " logged_in state does not " \ |
| 681 |
"exist for initiator " + id + "." |
| 682 |
LOG.error(error_message) |
| 683 |
self.module.fail_json(msg=error_message) |
| 684 |
|
| 685 |
host_details.delete_initiator(uid=id) |
| 686 |
updated_host \ |
| 687 |
= self.unity.get_host(name=host_details.name) |
| 688 |
|
| 689 |
return True, updated_host |
| 690 |
|
| 691 |
except Exception as e: |
| 692 |
error_message = "Got error %s while removing initiator from " \ |
| 693 |
"host %s" \ |
| 694 |
% (str(e), host_details.name) |
| 695 |
LOG.error(error_message) |
| 696 |
self.module.fail_json(msg=error_message) |
| 697 |
|
| 698 |
def check_if_initiators_logged_in(self, initiators): |
| 699 |
""" Checks if any of the initiators is of type logged-in""" |
| 700 |
|
| 701 |
for item in initiators: |
| 702 |
initiator_details = (utils.host.UnityHostInitiatorList |
| 703 |
.get(cli=self.unity._cli, initiator_id=item) |
| 704 |
._get_properties()) |
| 705 |
if initiator_details["paths"][0] is not None and "UnityHostInitiatorPathList" in initiator_details["paths"][0]: |
| 706 |
error_message = "Removal operation cannot be done since host has logged in initiator(s)" |
| 707 |
LOG.error(error_message) |
| 708 |
self.module.fail_json(msg=error_message) |
| 709 |
|
| 710 |
def delete_host(self, host_details): |
| 711 |
""" Delete an existing host """ |
| 712 |
|
| 713 |
try: |
| 714 |
host_details.delete() |
| 715 |
return True |
| 716 |
except Exception as e: |
| 717 |
error_message = "Got error %s while deletion of host %s" \ |
| 718 |
% (str(e), host_details.name) |
| 719 |
LOG.error(error_message) |
| 720 |
self.module.fail_json(msg=error_message) |
| 721 |
|
| 722 |
def get_iscsi_host_initiators_details(self, iscsi_host_initiators): |
| 723 |
""" Get the details of existing ISCSI initiators in host""" |
| 724 |
|
| 725 |
iscsi_initiator_list = [] |
| 726 |
for iscsi in iscsi_host_initiators: |
| 727 |
iscsi_initiator_details = self.unity.get_initiator(_id=iscsi.id) |
| 728 |
iscsi_path_list = [] |
| 729 |
if iscsi_initiator_details.paths is not None: |
| 730 |
for path in iscsi_initiator_details.paths: |
| 731 |
iscsi_path_list.append({ |
| 732 |
'id': path.id, |
| 733 |
'is_logged_in': path.is_logged_in |
| 734 |
}) |
| 735 |
iscsi_initiator_list.append({ |
| 736 |
'id': iscsi_initiator_details.id, |
| 737 |
'name': iscsi_initiator_details.initiator_id, |
| 738 |
'paths': iscsi_path_list |
| 739 |
}) |
| 740 |
return iscsi_initiator_list |
| 741 |
|
| 742 |
def get_host_network_address_list(self, host_details): |
| 743 |
network_address_list = [] |
| 744 |
if host_details and host_details.host_ip_ports is not None: |
| 745 |
for port in host_details.host_ip_ports: |
| 746 |
network_address_list.append(port.address) |
| 747 |
return network_address_list |
| 748 |
|
| 749 |
def manage_network_address(self, host_details, network_address_list, |
| 750 |
network_address, network_address_state): |
| 751 |
try: |
| 752 |
is_mapped = False |
| 753 |
changed = False |
| 754 |
for addr in network_address_list: |
| 755 |
if addr.lower() == network_address.lower(): |
| 756 |
is_mapped = True |
| 757 |
break |
| 758 |
if not is_mapped and network_address_state == 'present-in-host': |
| 759 |
LOG.info("Adding network address %s to Host %s", network_address, |
| 760 |
host_details.name) |
| 761 |
host_details.add_ip_port(network_address) |
| 762 |
changed = True |
| 763 |
elif is_mapped and network_address_state == 'absent-in-host': |
| 764 |
LOG.info("Deleting network address %s from Host %s", network_address, |
| 765 |
host_details.name) |
| 766 |
host_details.delete_ip_port(network_address) |
| 767 |
changed = True |
| 768 |
|
| 769 |
if changed: |
| 770 |
updated_host = self.unity.get_host(name=host_details.name) |
| 771 |
network_address_list = self.get_host_network_address_list(updated_host) |
| 772 |
return network_address_list, changed |
| 773 |
except Exception as e: |
| 774 |
error_message = "Got error %s while modifying network address %s of host %s" \ |
| 775 |
% (str(e), network_address, host_details.name) |
| 776 |
LOG.error(error_message) |
| 777 |
self.module.fail_json(msg=error_message) |
| 778 |
|
| 779 |
def get_host_lun_list(self, host_details): |
| 780 |
""" Get luns attached to host""" |
| 781 |
host_luns_list = [] |
| 782 |
if host_details and host_details.host_luns is not None: |
| 783 |
for lun in host_details.host_luns.lun: |
| 784 |
host_lun = {"name": lun.name, "id": lun.id} |
| 785 |
host_luns_list.append(host_lun) |
| 786 |
return host_luns_list |
| 787 |
|
| 788 |
def get_fc_host_initiators_details(self, fc_host_initiators): |
| 789 |
""" Get the details of existing FC initiators in host""" |
| 790 |
|
| 791 |
fc_initiator_list = [] |
| 792 |
for fc in fc_host_initiators: |
| 793 |
fc_initiator_details = self.unity.get_initiator(_id=fc.id) |
| 794 |
fc_path_list = [] |
| 795 |
if fc_initiator_details.paths is not None: |
| 796 |
for path in fc_initiator_details.paths: |
| 797 |
fc_path_list.append({ |
| 798 |
'id': path.id, |
| 799 |
'is_logged_in': path.is_logged_in |
| 800 |
}) |
| 801 |
fc_initiator_list.append({ |
| 802 |
'id': fc_initiator_details.id, |
| 803 |
'name': fc_initiator_details.initiator_id, |
| 804 |
'paths': fc_path_list |
| 805 |
}) |
| 806 |
return fc_initiator_list |
| 807 |
|
| 808 |
def perform_module_operation(self): |
| 809 |
""" Perform different actions on host based on user parameter |
| 810 |
chosen in playbook """ |
| 811 |
|
| 812 |
host_name = self.module.params['host_name'] |
| 813 |
host_id = self.module.params['host_id'] |
| 814 |
description = self.module.params['description'] |
| 815 |
host_os = self.module.params['host_os'] |
| 816 |
new_host_name = self.module.params['new_host_name'] |
| 817 |
initiator_state = self.module.params['initiator_state'] |
| 818 |
initiators = self.module.params['initiators'] |
| 819 |
network_address = self.module.params['network_address'] |
| 820 |
network_address_state = self.module.params['network_address_state'] |
| 821 |
state = self.module.params['state'] |
| 822 |
|
| 823 |
if host_name and len(host_name) > 255: |
| 824 |
err_msg = "'host_name' is greater than 255 characters." |
| 825 |
LOG.error(err_msg) |
| 826 |
self.module.fail_json(msg=err_msg) |
| 827 |
|
| 828 |
if new_host_name and len(new_host_name) > 255: |
| 829 |
err_msg = "'new_host_name' is greater than 255 characters." |
| 830 |
LOG.error(err_msg) |
| 831 |
self.module.fail_json(msg=err_msg) |
| 832 |
|
| 833 |
if description and len(description) > 255: |
| 834 |
err_msg = "'description' is greater than 255 characters." |
| 835 |
LOG.error(err_msg) |
| 836 |
self.module.fail_json(msg=err_msg) |
| 837 |
|
| 838 |
if not initiators and initiator_state: |
| 839 |
err_msg = "'initiator_state' is given, " \ |
| 840 |
"'initiators' are not specified" |
| 841 |
LOG.error(err_msg) |
| 842 |
self.module.fail_json(msg=err_msg) |
| 843 |
|
| 844 |
if not initiator_state and initiators: |
| 845 |
err_msg = "'initiators' are given, " \ |
| 846 |
"'initiator_state' is not specified" |
| 847 |
LOG.error(err_msg) |
| 848 |
self.module.fail_json(msg=err_msg) |
| 849 |
|
| 850 |
|
| 851 |
|
| 852 |
result = dict( |
| 853 |
changed=False, |
| 854 |
host_details={} |
| 855 |
) |
| 856 |
|
| 857 |
''' Get host details based on host_name/host_id''' |
| 858 |
host_details = self.get_host_details(host_id, host_name) |
| 859 |
if not host_details and state == 'present': |
| 860 |
if host_id: |
| 861 |
err_msg = "Invalid argument 'host_id' while " \ |
| 862 |
"creating a host" |
| 863 |
LOG.error(err_msg) |
| 864 |
self.module.fail_json(msg=err_msg) |
| 865 |
if not host_name: |
| 866 |
err_msg = "host_name is required to create a host" |
| 867 |
LOG.error(err_msg) |
| 868 |
self.module.fail_json(msg=err_msg) |
| 869 |
if new_host_name: |
| 870 |
err_msg = "Invalid argument 'new_host_name' while " \ |
| 871 |
"creating a host" |
| 872 |
LOG.error(err_msg) |
| 873 |
self.module.fail_json(msg=err_msg) |
| 874 |
|
| 875 |
if (initiators and initiator_state == 'absent-in-host'): |
| 876 |
error_message = "Incorrect 'initiator_state' given." |
| 877 |
LOG.error(error_message) |
| 878 |
self.module.fail_json(msg=error_message) |
| 879 |
|
| 880 |
|
| 881 |
LOG.info("Creating host: %s", host_name) |
| 882 |
result['changed'], host_details \ |
| 883 |
= self.create_host(host_name) |
| 884 |
result['host_details'] = host_details._get_properties() |
| 885 |
|
| 886 |
|
| 887 |
elif (state == 'present' and host_details): |
| 888 |
modified_flag = self.is_host_modified(host_details) |
| 889 |
if modified_flag: |
| 890 |
|
| 891 |
|
| 892 |
result['changed'] = self.modify_host(host_details, |
| 893 |
new_host_name, |
| 894 |
description, |
| 895 |
host_os) |
| 896 |
if new_host_name: |
| 897 |
host_details = self.get_host_details(host_id, |
| 898 |
new_host_name) |
| 899 |
else: |
| 900 |
host_details = self.get_host_details(host_id, host_name) |
| 901 |
result['host_details'] = host_details._get_properties() |
| 902 |
|
| 903 |
|
| 904 |
if (initiator_state == 'present-in-host' and initiators |
| 905 |
and len(initiators) > 0): |
| 906 |
LOG.info("Adding Initiators to Host %s", |
| 907 |
host_details.name) |
| 908 |
result['changed'], host_details \ |
| 909 |
= self.add_initiator_to_host(host_details, initiators) |
| 910 |
result['host_details'] = host_details._get_properties() |
| 911 |
|
| 912 |
else: |
| 913 |
LOG.info('Host modification is not applicable, ' |
| 914 |
'as none of the attributes has changed.') |
| 915 |
result['changed'] = False |
| 916 |
result['host_details'] = host_details._get_properties() |
| 917 |
|
| 918 |
|
| 919 |
if (host_details and initiator_state == 'absent-in-host' |
| 920 |
and initiators and len(initiators) > 0): |
| 921 |
LOG.info("Removing Initiators from Host %s", |
| 922 |
host_details.name) |
| 923 |
result['changed'], host_details \ |
| 924 |
= self.remove_initiator_from_host(host_details, |
| 925 |
initiators) |
| 926 |
result['host_details'] = host_details._get_properties() |
| 927 |
|
| 928 |
""" display WWN/IQN w.r.t. initiators mapped to host, |
| 929 |
if host exists """ |
| 930 |
if host_details and host_details.fc_host_initiators is not None: |
| 931 |
host_details.fc_host_initiators = self.get_fc_host_initiators_details(host_details.fc_host_initiators) |
| 932 |
result['host_details'] = host_details._get_properties() |
| 933 |
if host_details and host_details.iscsi_host_initiators is not None: |
| 934 |
host_details.iscsi_host_initiators = self.get_iscsi_host_initiators_details(host_details.iscsi_host_initiators) |
| 935 |
result['host_details'] = host_details._get_properties() |
| 936 |
|
| 937 |
''' Get host luns details and network addresses''' |
| 938 |
if result['host_details']: |
| 939 |
result['host_details']['host_luns'] = self.get_host_lun_list(host_details) |
| 940 |
result['host_details']['network_addresses'] = self.get_host_network_address_list(host_details) |
| 941 |
if 'host_ip_ports' in result['host_details']: |
| 942 |
del result['host_details']['host_ip_ports'] |
| 943 |
|
| 944 |
|
| 945 |
if host_details is not None and network_address_state is not None: |
| 946 |
self.validate_network_address_params(network_address) |
| 947 |
network_address_list, changed = self.manage_network_address( |
| 948 |
host_details, |
| 949 |
result['host_details']['network_addresses'], |
| 950 |
network_address, |
| 951 |
network_address_state) |
| 952 |
result['host_details']['network_addresses'] = network_address_list |
| 953 |
result['changed'] = changed |
| 954 |
|
| 955 |
|
| 956 |
if state == 'absent': |
| 957 |
if host_details: |
| 958 |
LOG.info("Deleting host %s", host_details.name) |
| 959 |
result['changed'] = self.delete_host(host_details) |
| 960 |
else: |
| 961 |
result['changed'] = False |
| 962 |
result['host_details'] = [] |
| 963 |
|
| 964 |
self.module.exit_json(**result) |
| 965 |
|
| 966 |
def validate_network_address_params(self, network_address): |
| 967 |
if '.' in network_address and not is_valid_ip(network_address): |
| 968 |
err_msg = 'Please enter valid IPV4 address for network address' |
| 969 |
LOG.error(err_msg) |
| 970 |
self.module.fail_json(msg=err_msg) |
| 971 |
|
| 972 |
if len(network_address) < 1 or len(network_address) > 63: |
| 973 |
err_msg = "'network_address' should be in range of 1 to 63 characters." |
| 974 |
LOG.error(err_msg) |
| 975 |
self.module.fail_json(msg=err_msg) |
| 976 |
|
| 977 |
if utils.has_special_char(network_address) or ' ' in network_address: |
| 978 |
err_msg = 'Please enter valid IPV4 address or host name for network address' |
| 979 |
LOG.error(err_msg) |
| 980 |
self.module.fail_json(msg=err_msg) |
| 981 |
|
| 982 |
|
| 983 |
def is_valid_ip(address): |
| 984 |
try: |
| 985 |
ipaddress.ip_address(address) |
| 986 |
return True |
| 987 |
except ValueError: |
| 988 |
return False |
| 989 |
|
| 990 |
|
| 991 |
def get_host_parameters(): |
| 992 |
"""This method provides parameters required for the ansible host |
| 993 |
module on Unity""" |
| 994 |
return dict( |
| 995 |
host_name=dict(required=False, type='str'), |
| 996 |
host_id=dict(required=False, type='str'), |
| 997 |
description=dict(required=False, type='str'), |
| 998 |
host_os=dict(required=False, type='str', |
| 999 |
choices=['AIX', 'Citrix XenServer', 'HP-UX', |
| 1000 |
'IBM VIOS', 'Linux', 'Mac OS', 'Solaris', |
| 1001 |
'VMware ESXi', 'Windows Client', |
| 1002 |
'Windows Server']), |
| 1003 |
new_host_name=dict(required=False, type='str'), |
| 1004 |
initiators=dict(required=False, type='list', elements='str'), |
| 1005 |
initiator_state=dict(required=False, type='str', |
| 1006 |
choices=['present-in-host', |
| 1007 |
'absent-in-host']), |
| 1008 |
network_address=dict(required=False, type='str'), |
| 1009 |
network_address_state=dict(required=False, type='str', |
| 1010 |
choices=['present-in-host', |
| 1011 |
'absent-in-host']), |
| 1012 |
state=dict(required=True, type='str', |
| 1013 |
choices=['present', 'absent']) |
| 1014 |
) |
| 1015 |
|
| 1016 |
|
| 1017 |
def main(): |
| 1018 |
""" Create Unity host object and perform action on it |
| 1019 |
based on user input from playbook""" |
| 1020 |
obj = Host() |
| 1021 |
obj.perform_module_operation() |
| 1022 |
|
| 1023 |
|
| 1024 |
if __name__ == '__main__': |
| 1025 |
main() |