| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2020, sky-joker |
| 5 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 7 |
|
| 8 |
from __future__ import absolute_import, division, print_function |
| 9 |
__metaclass__ = type |
| 10 |
|
| 11 |
DOCUMENTATION = r''' |
| 12 |
module: vmware_host_iscsi |
| 13 |
short_description: Manage the iSCSI configuration of ESXi host |
| 14 |
author: |
| 15 |
- sky-joker (@sky-joker) |
| 16 |
description: |
| 17 |
- In this module, can manage the iSCSI configuration of ESXi host |
| 18 |
options: |
| 19 |
esxi_hostname: |
| 20 |
description: |
| 21 |
- The ESXi hostname on which to change iSCSI settings. |
| 22 |
type: str |
| 23 |
required: true |
| 24 |
iscsi_config: |
| 25 |
description: |
| 26 |
- The iSCSI configs. |
| 27 |
- This parameter is required if O(state=present) or O(state=absent). |
| 28 |
type: dict |
| 29 |
suboptions: |
| 30 |
iscsi_name: |
| 31 |
description: |
| 32 |
- The name for the iSCSI HBA adapter. |
| 33 |
- This is iSCSI qualified name. |
| 34 |
type: str |
| 35 |
aliases: |
| 36 |
- initiator_iqn |
| 37 |
alias: |
| 38 |
description: |
| 39 |
- The new value for the alias of the adapter. |
| 40 |
type: str |
| 41 |
default: '' |
| 42 |
authentication: |
| 43 |
description: |
| 44 |
- CHAP authentication parent settings for iSCSI. |
| 45 |
type: dict |
| 46 |
suboptions: |
| 47 |
chap_auth_enabled: |
| 48 |
description: |
| 49 |
- Whether to enable CHAP authentication. |
| 50 |
type: bool |
| 51 |
default: false |
| 52 |
chap_authentication_type: |
| 53 |
description: |
| 54 |
- The preference for CHAP or non-CHAP protocol of CHAP if CHAP is enabled. |
| 55 |
type: str |
| 56 |
default: chapProhibited |
| 57 |
choices: |
| 58 |
- chapDiscouraged |
| 59 |
- chapPreferred |
| 60 |
- chapRequired |
| 61 |
- chapProhibited |
| 62 |
chap_name: |
| 63 |
description: |
| 64 |
- CHAP user name if CHAP is enabled. |
| 65 |
type: str |
| 66 |
default: '' |
| 67 |
chap_secret: |
| 68 |
description: |
| 69 |
- The secret password of CHAP if CHAP is enabled. |
| 70 |
type: str |
| 71 |
mutual_chap_authentication_type: |
| 72 |
description: |
| 73 |
- The preference for CHAP or non-CHAP protocol of Mutual-CHAP if CHAP is enabled. |
| 74 |
type: str |
| 75 |
default: chapProhibited |
| 76 |
choices: |
| 77 |
- chapProhibited |
| 78 |
- chapRequired |
| 79 |
mutual_chap_name: |
| 80 |
description: |
| 81 |
- The user name that the target needs to use to authenticate with the initiator if Mutual-CHAP is enabled. |
| 82 |
type: str |
| 83 |
default: '' |
| 84 |
mutual_chap_secret: |
| 85 |
description: |
| 86 |
- The secret password of mutual CHAP if Mutual-CHAP is enabled. |
| 87 |
type: str |
| 88 |
port_bind: |
| 89 |
description: |
| 90 |
- The list of the VMkernels if use port bindings. |
| 91 |
type: list |
| 92 |
elements: str |
| 93 |
default: [] |
| 94 |
force: |
| 95 |
description: |
| 96 |
- Force port bind VMkernels to be removed. |
| 97 |
type: bool |
| 98 |
default: false |
| 99 |
vmhba_name: |
| 100 |
description: |
| 101 |
- The iSCSI adapter name. |
| 102 |
type: str |
| 103 |
required: true |
| 104 |
send_target: |
| 105 |
description: |
| 106 |
- The iSCSI dynamic target settings. |
| 107 |
type: dict |
| 108 |
suboptions: |
| 109 |
address: |
| 110 |
description: |
| 111 |
- The IP address or hostname of the storage device. |
| 112 |
type: str |
| 113 |
required: true |
| 114 |
port: |
| 115 |
description: |
| 116 |
- The TCP port of the storage device. |
| 117 |
- If not specified, the standard default of 3260 is used. |
| 118 |
type: int |
| 119 |
default: 3260 |
| 120 |
authentication: |
| 121 |
description: |
| 122 |
- CHAP authentication settings of a dynamic target for iSCSI. |
| 123 |
type: dict |
| 124 |
suboptions: |
| 125 |
chap_auth_enabled: |
| 126 |
description: |
| 127 |
- Whether to enable CHAP authentication. |
| 128 |
type: bool |
| 129 |
default: false |
| 130 |
chap_authentication_type: |
| 131 |
description: |
| 132 |
- The preference for CHAP or non-CHAP protocol of CHAP if CHAP is enabled. |
| 133 |
type: str |
| 134 |
default: chapProhibited |
| 135 |
choices: |
| 136 |
- chapDiscouraged |
| 137 |
- chapPreferred |
| 138 |
- chapRequired |
| 139 |
- chapProhibited |
| 140 |
chap_inherited: |
| 141 |
description: |
| 142 |
- Whether or not to inherit CHAP settings from the parent settings. |
| 143 |
type: bool |
| 144 |
default: true |
| 145 |
chap_name: |
| 146 |
description: |
| 147 |
- CHAP user name if CHAP is enabled. |
| 148 |
type: str |
| 149 |
default: '' |
| 150 |
chap_secret: |
| 151 |
description: |
| 152 |
- The secret password of CHAP if CHAP is enabled. |
| 153 |
type: str |
| 154 |
mutual_chap_authentication_type: |
| 155 |
description: |
| 156 |
- The preference for CHAP or non-CHAP protocol of Mutual-CHAP if CHAP is enabled. |
| 157 |
type: str |
| 158 |
default: chapProhibited |
| 159 |
choices: |
| 160 |
- chapProhibited |
| 161 |
- chapRequired |
| 162 |
mutual_chap_inherited: |
| 163 |
description: |
| 164 |
- Whether or not to inherit Mutual-CHAP settings from the parent settings. |
| 165 |
type: bool |
| 166 |
default: true |
| 167 |
mutual_chap_name: |
| 168 |
description: |
| 169 |
- The user name that the target needs to use to authenticate with the initiator if Mutual-CHAP is enabled. |
| 170 |
type: str |
| 171 |
default: '' |
| 172 |
mutual_chap_secret: |
| 173 |
description: |
| 174 |
- The secret password of mutual CHAP if Mutual-CHAP is enabled. |
| 175 |
type: str |
| 176 |
static_target: |
| 177 |
description: |
| 178 |
- The iSCSI static target settings. |
| 179 |
type: dict |
| 180 |
suboptions: |
| 181 |
iscsi_name: |
| 182 |
description: |
| 183 |
- The name of the iSCSI target to connect to. |
| 184 |
type: str |
| 185 |
required: true |
| 186 |
address: |
| 187 |
description: |
| 188 |
- The IP address or hostname of the storage device. |
| 189 |
type: str |
| 190 |
required: true |
| 191 |
port: |
| 192 |
description: |
| 193 |
- The TCP port of the storage device. |
| 194 |
- If not specified, the standard default of 3260 is used. |
| 195 |
type: int |
| 196 |
default: 3260 |
| 197 |
authentication: |
| 198 |
description: |
| 199 |
- CHAP authentication settings of a static target for iSCSI. |
| 200 |
type: dict |
| 201 |
suboptions: |
| 202 |
chap_auth_enabled: |
| 203 |
description: |
| 204 |
- Whether to enable CHAP authentication. |
| 205 |
type: bool |
| 206 |
default: false |
| 207 |
chap_authentication_type: |
| 208 |
description: |
| 209 |
- The preference for CHAP or non-CHAP protocol of CHAP if CHAP is enabled. |
| 210 |
type: str |
| 211 |
default: chapProhibited |
| 212 |
choices: |
| 213 |
- chapDiscouraged |
| 214 |
- chapPreferred |
| 215 |
- chapRequired |
| 216 |
- chapProhibited |
| 217 |
chap_inherited: |
| 218 |
description: |
| 219 |
- Whether or not to inherit CHAP settings from the parent settings. |
| 220 |
type: bool |
| 221 |
default: true |
| 222 |
chap_name: |
| 223 |
description: |
| 224 |
- CHAP user name if CHAP is enabled. |
| 225 |
type: str |
| 226 |
default: '' |
| 227 |
chap_secret: |
| 228 |
description: |
| 229 |
- The secret password of CHAP if CHAP is enabled. |
| 230 |
type: str |
| 231 |
mutual_chap_authentication_type: |
| 232 |
description: |
| 233 |
- The preference for CHAP or non-CHAP protocol of Mutual-CHAP if CHAP is enabled. |
| 234 |
type: str |
| 235 |
default: chapProhibited |
| 236 |
choices: |
| 237 |
- chapProhibited |
| 238 |
- chapRequired |
| 239 |
mutual_chap_inherited: |
| 240 |
description: |
| 241 |
- Whether or not to inherit Mutual-CHAP settings from the parent settings. |
| 242 |
type: bool |
| 243 |
default: true |
| 244 |
mutual_chap_name: |
| 245 |
description: |
| 246 |
- The user name that the target needs to use to authenticate with the initiator if Mutual-CHAP is enabled. |
| 247 |
type: str |
| 248 |
default: '' |
| 249 |
mutual_chap_secret: |
| 250 |
description: |
| 251 |
- The secret password of mutual CHAP if Mutual-CHAP is enabled. |
| 252 |
type: str |
| 253 |
state: |
| 254 |
description: |
| 255 |
- If set to V(present), add the iSCSI target or the bind ports if they are not existing. |
| 256 |
- If set to V(present), update the iSCSI settings if they already exist and occur change. |
| 257 |
- If set to V(absent), remove the iSCSI target or the bind ports if they are existing. |
| 258 |
- If set to V(enabled), enable the iSCSI of ESXi if the iSCSI is disabled. |
| 259 |
- If set to V(disabled), disable the iSCSI of ESXi if the iSCSI is enabled. |
| 260 |
type: str |
| 261 |
default: present |
| 262 |
choices: |
| 263 |
- present |
| 264 |
- absent |
| 265 |
- enabled |
| 266 |
- disabled |
| 267 |
extends_documentation_fragment: |
| 268 |
- vmware.vmware.base_options |
| 269 |
''' |
| 270 |
|
| 271 |
EXAMPLES = r''' |
| 272 |
- name: Enable iSCSI of ESXi |
| 273 |
community.vmware.vmware_host_iscsi: |
| 274 |
hostname: "{{ vcenter_hostname }}" |
| 275 |
username: "{{ vcenter_username }}" |
| 276 |
password: "{{ vcenter_password }}" |
| 277 |
esxi_hostname: "{{ esxi_hostname }}" |
| 278 |
state: enabled |
| 279 |
|
| 280 |
- name: Add a dynamic target to iSCSI config of ESXi |
| 281 |
community.vmware.vmware_host_iscsi: |
| 282 |
hostname: "{{ vcenter_hostname }}" |
| 283 |
username: "{{ vcenter_username }}" |
| 284 |
password: "{{ vcenter_password }}" |
| 285 |
esxi_hostname: "{{ esxi_hostname }}" |
| 286 |
iscsi_config: |
| 287 |
vmhba_name: vmhba65 |
| 288 |
send_target: |
| 289 |
address: "{{ send_target_address }}" |
| 290 |
state: present |
| 291 |
|
| 292 |
- name: Add a static target to iSCSI config of ESXi |
| 293 |
community.vmware.vmware_host_iscsi: |
| 294 |
hostname: "{{ vcenter_hostname }}" |
| 295 |
username: "{{ vcenter_username }}" |
| 296 |
password: "{{ vcenter_password }}" |
| 297 |
esxi_hostname: "{{ esxi_hostname }}" |
| 298 |
iscsi_config: |
| 299 |
vmhba_name: vmhba65 |
| 300 |
static_target: |
| 301 |
iscsi_name: iqn.2011-08.com.xxxxxxx:as6104t-8c3e9d.target001 |
| 302 |
address: "{{ send_target_address }}" |
| 303 |
state: present |
| 304 |
|
| 305 |
- name: Add VMKernels to iSCSI config of ESXi |
| 306 |
community.vmware.vmware_host_iscsi: |
| 307 |
hostname: "{{ vcenter_hostname }}" |
| 308 |
username: "{{ vcenter_username }}" |
| 309 |
password: "{{ vcenter_password }}" |
| 310 |
esxi_hostname: "{{ esxi_hostname }}" |
| 311 |
iscsi_config: |
| 312 |
vmhba_name: vmhba65 |
| 313 |
port_bind: |
| 314 |
- vmk0 |
| 315 |
- vmk1 |
| 316 |
state: present |
| 317 |
|
| 318 |
- name: Use CHAP authentication |
| 319 |
community.vmware.vmware_host_iscsi: |
| 320 |
hostname: "{{ vcenter_hostname }}" |
| 321 |
username: "{{ vcenter_username }}" |
| 322 |
password: "{{ vcenter_password }}" |
| 323 |
esxi_hostname: "{{ esxi_hostname }}" |
| 324 |
iscsi_config: |
| 325 |
vmhba_name: vmhba65 |
| 326 |
authentication: |
| 327 |
chap_auth_enabled: true |
| 328 |
chap_authentication_type: chapPreferred |
| 329 |
chap_name: chap_user_name |
| 330 |
chap_secret: secret |
| 331 |
state: present |
| 332 |
|
| 333 |
- name: Remove a dynamic target from iSCSI config of ESXi |
| 334 |
community.vmware.vmware_host_iscsi: |
| 335 |
hostname: "{{ vcenter_hostname }}" |
| 336 |
username: "{{ vcenter_username }}" |
| 337 |
password: "{{ vcenter_password }}" |
| 338 |
esxi_hostname: "{{ esxi_hostname }}" |
| 339 |
iscsi_config: |
| 340 |
vmhba_name: vmhba65 |
| 341 |
send_target: |
| 342 |
address: "{{ send_target_address }}" |
| 343 |
state: absent |
| 344 |
''' |
| 345 |
|
| 346 |
RETURN = r''' |
| 347 |
iscsi_properties: |
| 348 |
description: Parameter return when system defaults config is changed. |
| 349 |
returned: changed |
| 350 |
type: dict |
| 351 |
sample: >- |
| 352 |
{ |
| 353 |
"iscsi_alias": "", |
| 354 |
"iscsi_authentication_properties": { |
| 355 |
"_vimtype": "vim.host.InternetScsiHba.AuthenticationProperties", |
| 356 |
"chapAuthEnabled": false, |
| 357 |
"chapAuthenticationType": "chapProhibited", |
| 358 |
"chapInherited": null, |
| 359 |
"chapName": "", |
| 360 |
"chapSecret": "XXXXXXXXXXXXXXXXXXXXX", |
| 361 |
"mutualChapAuthenticationType": "chapProhibited", |
| 362 |
"mutualChapInherited": null, |
| 363 |
"mutualChapName": "XXXXXXXXXXXXXXXXXXXXX", |
| 364 |
"mutualChapSecret": "" |
| 365 |
}, |
| 366 |
"iscsi_enabled": true, |
| 367 |
"iscsi_name": "", |
| 368 |
"iscsi_send_targets": [], |
| 369 |
"iscsi_static_targets": [], |
| 370 |
"port_bind": [], |
| 371 |
"vmhba_name": "vmhba65" |
| 372 |
} |
| 373 |
''' |
| 374 |
|
| 375 |
try: |
| 376 |
from pyVmomi import vim |
| 377 |
except ImportError: |
| 378 |
pass |
| 379 |
|
| 380 |
from ansible.module_utils.common.text.converters import to_native |
| 381 |
from ansible_collections.community.vmware.plugins.module_utils.vmware import PyVmomi |
| 382 |
from ansible_collections.vmware.vmware.plugins.module_utils.argument_spec import base_argument_spec |
| 383 |
from ansible.module_utils.basic import AnsibleModule |
| 384 |
from copy import deepcopy |
| 385 |
|
| 386 |
|
| 387 |
class VMwareHostiScsiManager(PyVmomi): |
| 388 |
def __init__(self, module): |
| 389 |
super(VMwareHostiScsiManager, self).__init__(module) |
| 390 |
self.esxi_hostname = self.params['esxi_hostname'] |
| 391 |
self.iscsi_config = self.params['iscsi_config'] |
| 392 |
self.state = self.params['state'] |
| 393 |
|
| 394 |
if self.iscsi_config: |
| 395 |
self.iscsi_name = self.iscsi_config['iscsi_name'] |
| 396 |
self.alias = self.iscsi_config['alias'] |
| 397 |
self.authentication = self.iscsi_config['authentication'] |
| 398 |
self.port_bind = self.iscsi_config['port_bind'] |
| 399 |
self.force = self.iscsi_config['force'] |
| 400 |
self.vmhba_name = self.iscsi_config['vmhba_name'] |
| 401 |
self.send_target = self.iscsi_config['send_target'] |
| 402 |
self.static_target = self.iscsi_config['static_target'] |
| 403 |
|
| 404 |
def get_iscsi_config(self): |
| 405 |
self.existing_system_iscsi_config = {} |
| 406 |
for hba in self.host_obj.config.storageDevice.hostBusAdapter: |
| 407 |
if isinstance(hba, vim.host.InternetScsiHba): |
| 408 |
self.existing_system_iscsi_config.update( |
| 409 |
{ |
| 410 |
'vmhba_name': hba.device, |
| 411 |
'iscsi_name': hba.iScsiName, |
| 412 |
'iscsi_alias': hba.iScsiAlias, |
| 413 |
'iscsi_authentication_properties': self.to_json(hba.authenticationProperties) |
| 414 |
} |
| 415 |
) |
| 416 |
|
| 417 |
iscsi_send_targets = [] |
| 418 |
for iscsi_send_target in self.to_json(hba.configuredSendTarget): |
| 419 |
iscsi_send_targets.append({ |
| 420 |
'address': iscsi_send_target['address'], |
| 421 |
'authenticationProperties': iscsi_send_target['authenticationProperties'], |
| 422 |
'port': iscsi_send_target['port'] |
| 423 |
}) |
| 424 |
self.existing_system_iscsi_config['iscsi_send_targets'] = iscsi_send_targets |
| 425 |
|
| 426 |
iscsi_static_targets = [] |
| 427 |
for iscsi_static_target in self.to_json(hba.configuredStaticTarget): |
| 428 |
iscsi_static_targets.append({ |
| 429 |
'iscsi_name': iscsi_static_target['iScsiName'], |
| 430 |
'address': iscsi_static_target['address'], |
| 431 |
'authenticationProperties': iscsi_static_target['authenticationProperties'], |
| 432 |
'port': iscsi_static_target['port'] |
| 433 |
}) |
| 434 |
self.existing_system_iscsi_config['iscsi_static_targets'] = iscsi_static_targets |
| 435 |
|
| 436 |
self.existing_system_iscsi_config['iscsi_enabled'] = self.to_json(self.host_obj.config.storageDevice.softwareInternetScsiEnabled) |
| 437 |
|
| 438 |
vnic_devices = [] |
| 439 |
if self.iscsi_config: |
| 440 |
for vnic in self.host_obj.configManager.iscsiManager.QueryBoundVnics(iScsiHbaName=self.vmhba_name): |
| 441 |
vnic_devices.append(vnic.vnicDevice) |
| 442 |
self.existing_system_iscsi_config['port_bind'] = vnic_devices |
| 443 |
|
| 444 |
def check_hba_name(self): |
| 445 |
if self.existing_system_iscsi_config['vmhba_name'] != self.vmhba_name: |
| 446 |
self.module.fail_json(msg="%s is not an iSCSI device." % self.vmhba_name) |
| 447 |
|
| 448 |
def diff_iscsi_config(self): |
| 449 |
if self.state == 'enabled': |
| 450 |
self.change_flag = True |
| 451 |
if self.existing_system_iscsi_config['iscsi_enabled'] == 'true': |
| 452 |
self.change_flag = False |
| 453 |
|
| 454 |
if self.state == 'disabled': |
| 455 |
self.change_flag = True |
| 456 |
if self.existing_system_iscsi_config['iscsi_enabled'] == 'false': |
| 457 |
self.change_flag = False |
| 458 |
|
| 459 |
if self.state == 'present': |
| 460 |
self.change_flag = True |
| 461 |
|
| 462 |
self.add_send_interface_flag = True |
| 463 |
if self.send_target: |
| 464 |
for config in self.existing_system_iscsi_config['iscsi_send_targets']: |
| 465 |
if config['address'] == self.send_target['address'] and config['port'] == self.send_target['port']: |
| 466 |
self.change_flag = False |
| 467 |
self.add_send_interface_flag = False |
| 468 |
|
| 469 |
self.add_static_interface_flag = True |
| 470 |
if self.static_target: |
| 471 |
for config in self.existing_system_iscsi_config['iscsi_static_targets']: |
| 472 |
if config['address'] == self.static_target['address'] and config['port'] == self.static_target['port'] \ |
| 473 |
and config['iscsi_name'] == self.static_target['iscsi_name']: |
| 474 |
self.change_flag = False |
| 475 |
self.add_static_interface_flag = False |
| 476 |
|
| 477 |
self.update_iscsi_name_flag = False |
| 478 |
if self.existing_system_iscsi_config['iscsi_name'] != self.iscsi_name and self.iscsi_name: |
| 479 |
self.change_flag = True |
| 480 |
self.update_iscsi_name_flag = True |
| 481 |
|
| 482 |
self.update_alias_flag = False |
| 483 |
if self.existing_system_iscsi_config['iscsi_alias'] != self.alias: |
| 484 |
self.change_flag = True |
| 485 |
self.update_alias_flag = True |
| 486 |
|
| 487 |
self.update_auth_flag = False |
| 488 |
if self.authentication: |
| 489 |
auth_properties = self.existing_system_iscsi_config['iscsi_authentication_properties'] |
| 490 |
if auth_properties['chapAuthEnabled'] != self.authentication['chap_auth_enabled']: |
| 491 |
self.change_flag = True |
| 492 |
self.update_auth_flag = True |
| 493 |
if auth_properties['chapAuthenticationType'] != self.authentication['chap_authentication_type']: |
| 494 |
self.change_flag = True |
| 495 |
self.update_auth_flag = True |
| 496 |
if auth_properties['chapName'] != self.authentication['chap_name']: |
| 497 |
self.change_flag = True |
| 498 |
self.update_auth_flag = True |
| 499 |
if auth_properties['mutualChapAuthenticationType'] != self.authentication['mutual_chap_authentication_type']: |
| 500 |
self.change_flag = True |
| 501 |
self.update_auth_flag = True |
| 502 |
if auth_properties['mutualChapName'] != self.authentication['mutual_chap_name']: |
| 503 |
self.change_flag = True |
| 504 |
self.update_auth_flag = True |
| 505 |
|
| 506 |
self.update_port_bind_flag = False |
| 507 |
if sorted(self.existing_system_iscsi_config['port_bind']) != sorted(self.port_bind): |
| 508 |
self.change_flag = True |
| 509 |
self.update_port_bind_flag = True |
| 510 |
|
| 511 |
self.update_send_target_authentication = False |
| 512 |
if self.add_send_interface_flag is False: |
| 513 |
for config in self.existing_system_iscsi_config['iscsi_send_targets']: |
| 514 |
if config['address'] == self.send_target['address'] and \ |
| 515 |
config['port'] == self.send_target['port']: |
| 516 |
auth_properties = config['authenticationProperties'] |
| 517 |
if auth_properties['chapAuthEnabled'] != self.send_target['authentication']['chap_auth_enabled']: |
| 518 |
self.change_flag = True |
| 519 |
self.update_send_target_authentication = True |
| 520 |
if auth_properties['chapAuthenticationType'] != self.send_target['authentication']['chap_authentication_type']: |
| 521 |
self.change_flag = True |
| 522 |
self.update_send_target_authentication = True |
| 523 |
if auth_properties['chapInherited'] != self.send_target['authentication']['chap_inherited']: |
| 524 |
self.change_flag = True |
| 525 |
self.update_send_target_authentication = True |
| 526 |
if auth_properties['chapName'] != self.send_target['authentication']['chap_name']: |
| 527 |
self.change_flag = True |
| 528 |
self.update_send_target_authentication = True |
| 529 |
if auth_properties['mutualChapAuthenticationType'] != self.send_target['authentication']['mutual_chap_authentication_type']: |
| 530 |
self.change_flag = True |
| 531 |
self.update_send_target_authentication = True |
| 532 |
if auth_properties['mutualChapInherited'] != self.send_target['authentication']['mutual_chap_inherited']: |
| 533 |
self.change_flag = True |
| 534 |
self.update_send_target_authentication = True |
| 535 |
if auth_properties['mutualChapName'] != self.send_target['authentication']['mutual_chap_name']: |
| 536 |
self.change_flag = True |
| 537 |
self.update_send_target_authentication = True |
| 538 |
break |
| 539 |
|
| 540 |
self.update_static_target_authentication = False |
| 541 |
if self.add_static_interface_flag is False: |
| 542 |
for config in self.existing_system_iscsi_config['iscsi_static_targets']: |
| 543 |
if config['address'] == self.static_target['address'] and \ |
| 544 |
config['port'] == self.static_target['port']: |
| 545 |
auth_properties = config['authenticationProperties'] |
| 546 |
if auth_properties['chapAuthEnabled'] != self.static_target['authentication']['chap_auth_enabled']: |
| 547 |
self.change_flag = True |
| 548 |
self.update_static_target_authentication = True |
| 549 |
if auth_properties['chapAuthenticationType'] != self.static_target['authentication']['chap_authentication_type']: |
| 550 |
self.change_flag = True |
| 551 |
self.update_static_target_authentication = True |
| 552 |
if auth_properties['chapInherited'] != self.static_target['authentication']['chap_inherited']: |
| 553 |
self.change_flag = True |
| 554 |
self.update_static_target_authentication = True |
| 555 |
if auth_properties['chapName'] != self.static_target['authentication']['chap_name']: |
| 556 |
self.change_flag = True |
| 557 |
self.update_static_target_authentication = True |
| 558 |
if auth_properties['mutualChapAuthenticationType'] != self.static_target['authentication']['mutual_chap_authentication_type']: |
| 559 |
self.change_flag = True |
| 560 |
self.update_static_target_authentication = True |
| 561 |
if auth_properties['mutualChapInherited'] != self.static_target['authentication']['mutual_chap_inherited']: |
| 562 |
self.change_flag = True |
| 563 |
self.update_static_target_authentication = True |
| 564 |
if auth_properties['mutualChapName'] != self.static_target['authentication']['mutual_chap_name']: |
| 565 |
self.change_flag = True |
| 566 |
self.update_static_target_authentication = True |
| 567 |
break |
| 568 |
|
| 569 |
if self.state == 'absent': |
| 570 |
self.change_flag = False |
| 571 |
|
| 572 |
self.remove_send_interface_flag = False |
| 573 |
if self.existing_system_iscsi_config['iscsi_send_targets'] and self.send_target: |
| 574 |
for config in self.existing_system_iscsi_config['iscsi_send_targets']: |
| 575 |
if config['address'] == self.send_target['address'] and \ |
| 576 |
config['port'] == self.send_target['port']: |
| 577 |
self.change_flag = True |
| 578 |
self.remove_send_interface_flag = True |
| 579 |
|
| 580 |
self.remove_static_interface_flag = False |
| 581 |
if self.existing_system_iscsi_config['iscsi_static_targets'] and self.static_target: |
| 582 |
for config in self.existing_system_iscsi_config['iscsi_static_targets']: |
| 583 |
if config['address'] == self.static_target['address'] and \ |
| 584 |
config['port'] == self.static_target['port'] and \ |
| 585 |
config['iscsi_name'] == self.static_target['iscsi_name']: |
| 586 |
self.change_flag = True |
| 587 |
self.remove_static_interface_flag = True |
| 588 |
|
| 589 |
self.remove_port_bind_flag = False |
| 590 |
if self.iscsi_config: |
| 591 |
for vnic in self.port_bind: |
| 592 |
for existing_vnic in self.existing_system_iscsi_config['port_bind']: |
| 593 |
if vnic == existing_vnic: |
| 594 |
self.change_flag = True |
| 595 |
self.remove_port_bind_flag = True |
| 596 |
|
| 597 |
def generate_iscsi_config(self): |
| 598 |
self.authentication_config = '' |
| 599 |
self.authentication_send_target_config = '' |
| 600 |
self.send_target_configs = [] |
| 601 |
self.static_target_configs = [] |
| 602 |
|
| 603 |
if self.authentication: |
| 604 |
self.authentication_config = vim.host.InternetScsiHba.AuthenticationProperties() |
| 605 |
self.authentication_config.chapAuthEnabled = self.authentication['chap_auth_enabled'] |
| 606 |
self.authentication_config.chapAuthenticationType = self.authentication['chap_authentication_type'] |
| 607 |
self.authentication_config.chapName = self.authentication['chap_name'] |
| 608 |
self.authentication_config.chapSecret = self.authentication['chap_secret'] |
| 609 |
self.authentication_config.mutualChapAuthenticationType = self.authentication['mutual_chap_authentication_type'] |
| 610 |
self.authentication_config.mutualChapName = self.authentication['mutual_chap_name'] |
| 611 |
self.authentication_config.mutualChapSecret = self.authentication['mutual_chap_secret'] |
| 612 |
|
| 613 |
if self.send_target: |
| 614 |
send_target_config = vim.host.InternetScsiHba.SendTarget() |
| 615 |
send_target_config.address = self.send_target['address'] |
| 616 |
send_target_config.port = self.send_target['port'] |
| 617 |
|
| 618 |
if self.send_target['authentication']: |
| 619 |
self.send_target_authentication_config = vim.host.InternetScsiHba.AuthenticationProperties() |
| 620 |
self.send_target_authentication_config.chapAuthEnabled = self.send_target['authentication']['chap_auth_enabled'] |
| 621 |
self.send_target_authentication_config.chapAuthenticationType = self.send_target['authentication']['chap_authentication_type'] |
| 622 |
self.send_target_authentication_config.chapInherited = self.send_target['authentication']['chap_inherited'] |
| 623 |
self.send_target_authentication_config.chapName = self.send_target['authentication']['chap_name'] |
| 624 |
self.send_target_authentication_config.chapSecret = self.send_target['authentication']['chap_secret'] |
| 625 |
self.send_target_authentication_config.mutualChapAuthenticationType = self.send_target['authentication']['mutual_chap_authentication_type'] |
| 626 |
self.send_target_authentication_config.mutualChapInherited = self.send_target['authentication']['mutual_chap_inherited'] |
| 627 |
self.send_target_authentication_config.mutualChapName = self.send_target['authentication']['mutual_chap_name'] |
| 628 |
self.send_target_authentication_config.mutualChapSecret = self.send_target['authentication']['mutual_chap_secret'] |
| 629 |
|
| 630 |
self.send_target_configs.append(send_target_config) |
| 631 |
|
| 632 |
if self.static_target: |
| 633 |
static_target_config = vim.host.InternetScsiHba.StaticTarget() |
| 634 |
static_target_config.iScsiName = self.static_target['iscsi_name'] |
| 635 |
static_target_config.address = self.static_target['address'] |
| 636 |
static_target_config.port = self.static_target['port'] |
| 637 |
|
| 638 |
if self.static_target['authentication']: |
| 639 |
self.static_target_authentication_config = vim.host.InternetScsiHba.AuthenticationProperties() |
| 640 |
self.static_target_authentication_config.chapAuthEnabled = self.static_target['authentication']['chap_auth_enabled'] |
| 641 |
self.static_target_authentication_config.chapAuthenticationType = self.static_target['authentication']['chap_authentication_type'] |
| 642 |
self.static_target_authentication_config.chapInherited = self.static_target['authentication']['chap_inherited'] |
| 643 |
self.static_target_authentication_config.chapName = self.static_target['authentication']['chap_name'] |
| 644 |
self.static_target_authentication_config.chapSecret = self.static_target['authentication']['chap_secret'] |
| 645 |
self.static_target_authentication_config.mutualChapAuthenticationType = self.static_target['authentication']['mutual_chap_authentication_type'] |
| 646 |
self.static_target_authentication_config.mutualChapInherited = self.static_target['authentication']['mutual_chap_inherited'] |
| 647 |
self.static_target_authentication_config.mutualChapName = self.static_target['authentication']['mutual_chap_name'] |
| 648 |
self.static_target_authentication_config.mutualChapSecret = self.static_target['authentication']['mutual_chap_secret'] |
| 649 |
|
| 650 |
self.static_target_configs.append(static_target_config) |
| 651 |
|
| 652 |
def execute(self): |
| 653 |
self.host_obj = self.find_hostsystem_by_name(self.esxi_hostname) |
| 654 |
if not self.host_obj: |
| 655 |
self.module.fail_json(msg="Cannot find the specified ESXi host: %s" % self.esxi_hostname) |
| 656 |
|
| 657 |
if not self.iscsi_config and (self.state == 'present' or self.state == 'absent'): |
| 658 |
self.module.fail_json(msg="If state is present or absent must specify the iscsi_config parameter.") |
| 659 |
else: |
| 660 |
self.get_iscsi_config() |
| 661 |
|
| 662 |
result = dict(changed=False) |
| 663 |
|
| 664 |
if self.state == 'enabled': |
| 665 |
self.diff_iscsi_config() |
| 666 |
if self.module.check_mode: |
| 667 |
self.module.exit_json(changed=self.change_flag) |
| 668 |
|
| 669 |
if self.existing_system_iscsi_config['iscsi_enabled'] is False: |
| 670 |
try: |
| 671 |
self.host_obj.configManager.storageSystem.UpdateSoftwareInternetScsiEnabled(enabled=True) |
| 672 |
result['changed'] = True |
| 673 |
except Exception as e: |
| 674 |
self.module.fail_json(msg="Failed to enable iSCSI: %s" % to_native(e)) |
| 675 |
|
| 676 |
if self.state == 'disabled': |
| 677 |
self.diff_iscsi_config() |
| 678 |
if self.module.check_mode: |
| 679 |
self.module.exit_json(changed=self.change_flag) |
| 680 |
|
| 681 |
if self.existing_system_iscsi_config['iscsi_enabled']: |
| 682 |
try: |
| 683 |
self.host_obj.configManager.storageSystem.UpdateSoftwareInternetScsiEnabled(enabled=False) |
| 684 |
result['changed'] = True |
| 685 |
except Exception as e: |
| 686 |
self.module.fail_json(msg="Failed to disable iSCSI: %s" % to_native(e)) |
| 687 |
|
| 688 |
if self.state == 'present': |
| 689 |
self.check_hba_name() |
| 690 |
self.diff_iscsi_config() |
| 691 |
|
| 692 |
if self.module.check_mode: |
| 693 |
self.module.exit_json(changed=self.change_flag) |
| 694 |
|
| 695 |
if self.change_flag: |
| 696 |
self.generate_iscsi_config() |
| 697 |
|
| 698 |
# add a dynamic target to an iSCSI configuration |
| 699 |
if self.add_send_interface_flag: |
| 700 |
if self.send_target_configs: |
| 701 |
try: |
| 702 |
self.host_obj.configManager.storageSystem.AddInternetScsiSendTargets( |
| 703 |
iScsiHbaDevice=self.vmhba_name, |
| 704 |
targets=self.send_target_configs) |
| 705 |
result['changed'] = True |
| 706 |
except Exception as e: |
| 707 |
self.module.fail_json(msg="Failed to add a dynamic target: %s" % to_native(e)) |
| 708 |
|
| 709 |
# add a static target to an iSCSI configuration |
| 710 |
if self.add_static_interface_flag: |
| 711 |
if self.static_target_configs: |
| 712 |
try: |
| 713 |
self.host_obj.configManager.storageSystem.AddInternetScsiStaticTargets( |
| 714 |
iScsiHbaDevice=self.vmhba_name, |
| 715 |
targets=self.static_target_configs) |
| 716 |
result['changed'] = True |
| 717 |
except Exception as e: |
| 718 |
self.module.fail_json(msg="Failed to add a static target: %s" % to_native(e)) |
| 719 |
|
| 720 |
# update a CHAP authentication of a dynamic target in an iSCSI configuration |
| 721 |
if self.update_send_target_authentication: |
| 722 |
target_set = vim.host.InternetScsiHba.TargetSet() |
| 723 |
target_set.sendTargets = self.send_target_configs |
| 724 |
try: |
| 725 |
self.host_obj.configManager.storageSystem.UpdateInternetScsiAuthenticationProperties( |
| 726 |
iScsiHbaDevice=self.vmhba_name, |
| 727 |
authenticationProperties=self.send_target_authentication_config, |
| 728 |
targetSet=target_set) |
| 729 |
result['changed'] = True |
| 730 |
except Exception as e: |
| 731 |
self.module.fail_json(msg="Failed to update a CHAP authentication of a dynamic target: %s" % to_native(e)) |
| 732 |
|
| 733 |
# update a CHAP authentication of a static target in an iSCSI configuration |
| 734 |
if self.update_static_target_authentication: |
| 735 |
target_set = vim.host.InternetScsiHba.TargetSet() |
| 736 |
target_set.staticTargets = self.static_target_configs |
| 737 |
try: |
| 738 |
self.host_obj.configManager.storageSystem.UpdateInternetScsiAuthenticationProperties( |
| 739 |
iScsiHbaDevice=self.vmhba_name, |
| 740 |
authenticationProperties=self.static_target_authentication_config, |
| 741 |
targetSet=target_set) |
| 742 |
result['changed'] = True |
| 743 |
except Exception as e: |
| 744 |
self.module.fail_json(msg="Failed to update a CHAP authentication of a static target: %s" % to_native(e)) |
| 745 |
|
| 746 |
# update an iqn in an iSCSI configuration |
| 747 |
if self.update_iscsi_name_flag: |
| 748 |
try: |
| 749 |
self.host_obj.configManager.storageSystem.UpdateInternetScsiName( |
| 750 |
iScsiHbaDevice=self.vmhba_name, iScsiName=self.iscsi_name) |
| 751 |
result['changed'] = True |
| 752 |
except Exception as e: |
| 753 |
self.module.fail_json(msg="Failed to update an iqn: %s" % to_native(e)) |
| 754 |
|
| 755 |
# update an alias in an iSCSI configuration |
| 756 |
if self.update_alias_flag: |
| 757 |
try: |
| 758 |
self.host_obj.configManager.storageSystem.UpdateInternetScsiAlias( |
| 759 |
iScsiHbaDevice=self.vmhba_name, iScsiAlias=self.alias) |
| 760 |
result['changed'] = True |
| 761 |
except Exception as e: |
| 762 |
self.module.fail_json(msg="Failed to update an alias: %s" % to_native(e)) |
| 763 |
|
| 764 |
# update a CHAP authentication an iSCSI configuration |
| 765 |
if self.update_auth_flag: |
| 766 |
try: |
| 767 |
self.host_obj.configManager.storageSystem.UpdateInternetScsiAuthenticationProperties( |
| 768 |
iScsiHbaDevice=self.vmhba_name, |
| 769 |
authenticationProperties=self.authentication_config, |
| 770 |
targetSet=None) |
| 771 |
result['changed'] = True |
| 772 |
except Exception as e: |
| 773 |
self.module.fail_json(msg="Failed to update a CHAP authentication: %s" % to_native(e)) |
| 774 |
|
| 775 |
# add port binds in an iSCSI configuration |
| 776 |
if self.update_port_bind_flag: |
| 777 |
for vnic in list(filter(lambda x: x not in self.existing_system_iscsi_config['port_bind'], |
| 778 |
self.port_bind)): |
| 779 |
try: |
| 780 |
self.host_obj.configManager.iscsiManager.BindVnic(iScsiHbaName=self.vmhba_name, |
| 781 |
vnicDevice=vnic) |
| 782 |
result['changed'] = True |
| 783 |
except Exception as e: |
| 784 |
self.module.fail_json(msg="Failed to add a port bind: %s" % to_native(e)) |
| 785 |
|
| 786 |
if self.state == 'absent': |
| 787 |
self.check_hba_name() |
| 788 |
self.diff_iscsi_config() |
| 789 |
|
| 790 |
if self.module.check_mode: |
| 791 |
self.module.exit_json(changed=self.change_flag) |
| 792 |
|
| 793 |
if self.change_flag: |
| 794 |
self.generate_iscsi_config() |
| 795 |
|
| 796 |
# remove a dynamic target to an iSCSI configuration |
| 797 |
if self.remove_send_interface_flag: |
| 798 |
try: |
| 799 |
self.host_obj.configManager.storageSystem.RemoveInternetScsiSendTargets( |
| 800 |
iScsiHbaDevice=self.vmhba_name, |
| 801 |
targets=self.send_target_configs) |
| 802 |
result['changed'] = True |
| 803 |
except Exception as e: |
| 804 |
self.module.fail_json(msg="Failed to remove a dynamic target: %s" % to_native(e)) |
| 805 |
|
| 806 |
# remove a static target to an iSCSI configuration |
| 807 |
if self.remove_static_interface_flag: |
| 808 |
try: |
| 809 |
self.host_obj.configManager.storageSystem.RemoveInternetScsiStaticTargets( |
| 810 |
iScsiHbaDevice=self.vmhba_name, |
| 811 |
targets=self.static_target_configs) |
| 812 |
result['changed'] = True |
| 813 |
except Exception as e: |
| 814 |
self.module.fail_json(msg="Failed to remove a static target: %s" % to_native(e)) |
| 815 |
|
| 816 |
# remove port binds from an iSCSI configuration |
| 817 |
if self.remove_port_bind_flag: |
| 818 |
for vnic in list(set(self.existing_system_iscsi_config['port_bind']) & set(self.port_bind)): |
| 819 |
try: |
| 820 |
self.host_obj.configManager.iscsiManager.UnbindVnic(iScsiHbaName=self.vmhba_name, |
| 821 |
vnicDevice=vnic, |
| 822 |
force=self.force) |
| 823 |
result['changed'] = True |
| 824 |
except Exception as e: |
| 825 |
self.module.fail_json(msg="Failed to remove a port bind: %s" % to_native(e)) |
| 826 |
|
| 827 |
if result['changed'] is True: |
| 828 |
self.get_iscsi_config() |
| 829 |
result['iscsi_properties'] = self.existing_system_iscsi_config |
| 830 |
|
| 831 |
self.module.exit_json(**result) |
| 832 |
|
| 833 |
|
| 834 |
def main(): |
| 835 |
# base authentication parameters. |
| 836 |
authentication = dict(type='dict', apply_defaults=True, |
| 837 |
options=dict( |
| 838 |
chap_auth_enabled=dict(type='bool', default=False), |
| 839 |
chap_authentication_type=dict(type='str', default='chapProhibited', |
| 840 |
choices=['chapDiscouraged', 'chapPreferred', 'chapRequired', |
| 841 |
'chapProhibited']), |
| 842 |
chap_name=dict(type='str', default=''), |
| 843 |
chap_secret=dict(type='str', no_log=True), |
| 844 |
mutual_chap_authentication_type=dict(type='str', default='chapProhibited', |
| 845 |
choices=['chapProhibited', 'chapRequired']), |
| 846 |
mutual_chap_name=dict(type='str', default=''), |
| 847 |
mutual_chap_secret=dict(type='str', no_log=True))) |
| 848 |
|
| 849 |
authentication_target = deepcopy(authentication) |
| 850 |
authentication_target['options'].update( |
| 851 |
chap_inherited=dict(type='bool', default=True), |
| 852 |
mutual_chap_inherited=dict(type='bool', default=True) |
| 853 |
) |
| 854 |
|
| 855 |
argument_spec = base_argument_spec() |
| 856 |
argument_spec.update( |
| 857 |
esxi_hostname=dict(type='str', required=True), |
| 858 |
iscsi_config=dict(type='dict', |
| 859 |
options=dict( |
| 860 |
iscsi_name=dict(type='str', default=None, aliases=['initiator_iqn']), |
| 861 |
alias=dict(type='str', default=''), |
| 862 |
authentication=authentication, |
| 863 |
port_bind=dict(type='list', elements='str', default=[]), |
| 864 |
force=dict(type='bool', default=False), |
| 865 |
vmhba_name=dict(type='str', required=True), |
| 866 |
send_target=dict(type='dict', |
| 867 |
options=dict( |
| 868 |
address=dict(type='str', required=True), |
| 869 |
port=dict(type='int', default=3260), |
| 870 |
authentication=authentication_target |
| 871 |
)), |
| 872 |
static_target=dict(type='dict', |
| 873 |
options=dict( |
| 874 |
iscsi_name=dict(type='str', required=True), |
| 875 |
address=dict(type='str', required=True), |
| 876 |
port=dict(type='int', default=3260), |
| 877 |
authentication=authentication_target |
| 878 |
)) |
| 879 |
)), |
| 880 |
state=dict(type='str', choices=['present', 'absent', 'enabled', 'disabled'], default='present') |
| 881 |
) |
| 882 |
|
| 883 |
module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) |
| 884 |
|
| 885 |
vmware_host_iscsi_manager = VMwareHostiScsiManager(module) |
| 886 |
vmware_host_iscsi_manager.execute() |
| 887 |
|
| 888 |
|
| 889 |
if __name__ == "__main__": |
| 890 |
main() |