| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2015, Joseph Callen <jcallen () csc.com> |
| 5 |
# Copyright: (c) 2017, Ansible Project |
| 6 |
# Copyright: (c) 2018, Christian Kotte <[email protected]> |
| 7 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 8 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 9 |
|
| 10 |
from __future__ import absolute_import, division, print_function |
| 11 |
__metaclass__ = type |
| 12 |
|
| 13 |
|
| 14 |
DOCUMENTATION = r''' |
| 15 |
--- |
| 16 |
module: vmware_host |
| 17 |
deprecated: |
| 18 |
removed_in: 7.0.0 |
| 19 |
why: This module has been moved to the L(new vmware.vmware collection,https://forum.ansible.com/t/5880) |
| 20 |
alternative: Use M(vmware.vmware.esxi_host) and M(vmware.vmware.esxi_connection) instead. |
| 21 |
short_description: Add, remove, or move an ESXi host to, from, or within vCenter |
| 22 |
description: |
| 23 |
- This module can be used to add, reconnect, or remove an ESXi host to or from vCenter. |
| 24 |
- This module can also be used to move an ESXi host to a cluster or folder, or vice versa, within the same datacenter. |
| 25 |
author: |
| 26 |
- Joseph Callen (@jcpowermac) |
| 27 |
- Russell Teague (@mtnbikenc) |
| 28 |
- Maxime de Roucy (@tchernomax) |
| 29 |
- Christian Kotte (@ckotte) |
| 30 |
options: |
| 31 |
datacenter_name: |
| 32 |
description: |
| 33 |
- Name of the datacenter to add the host. |
| 34 |
required: true |
| 35 |
aliases: ['datacenter'] |
| 36 |
type: str |
| 37 |
cluster_name: |
| 38 |
description: |
| 39 |
- Name of the cluster to add the host. |
| 40 |
- If O(folder) is not set, then this parameter is required. |
| 41 |
aliases: ['cluster'] |
| 42 |
type: str |
| 43 |
folder: |
| 44 |
description: |
| 45 |
- Name of the folder under which host to add. |
| 46 |
- If O(cluster_name) is not set, then this parameter is required. |
| 47 |
- "For example, if there is a datacenter 'dc1' under folder called 'Site1' then, this value will be '/Site1/dc1/host'." |
| 48 |
- "Here 'host' is an invisible folder under VMware Web Client." |
| 49 |
- "Another example, if there is a nested folder structure like '/myhosts/india/pune' under |
| 50 |
datacenter 'dc2', then O(folder) value will be '/dc2/host/myhosts/india/pune'." |
| 51 |
- "Other Examples: '/Site2/dc2/Asia-Cluster/host' or '/dc3/Asia-Cluster/host'" |
| 52 |
aliases: ['folder_name'] |
| 53 |
type: str |
| 54 |
add_connected: |
| 55 |
description: |
| 56 |
- If set to V(true), then the host should be connected as soon as it is added. |
| 57 |
- This parameter is ignored if not O(state=present). |
| 58 |
default: true |
| 59 |
type: bool |
| 60 |
esxi_hostname: |
| 61 |
description: |
| 62 |
- ESXi hostname to manage. |
| 63 |
required: true |
| 64 |
type: str |
| 65 |
esxi_username: |
| 66 |
description: |
| 67 |
- ESXi username. |
| 68 |
- Required for adding a host. |
| 69 |
- Optional for reconnect. If both O(esxi_username) and O(esxi_password) are used |
| 70 |
- Unused for removing. |
| 71 |
- No longer a required parameter from version 2.5. |
| 72 |
type: str |
| 73 |
esxi_password: |
| 74 |
description: |
| 75 |
- ESXi password. |
| 76 |
- Required for adding a host. |
| 77 |
- Optional for reconnect. |
| 78 |
- Unused for removing. |
| 79 |
- No longer a required parameter from version 2.5. |
| 80 |
type: str |
| 81 |
state: |
| 82 |
description: |
| 83 |
- If set to V(present), add the host if host is absent. |
| 84 |
- If set to V(present), update the location of the host if host already exists. |
| 85 |
- If set to V(absent), remove the host if host is present. |
| 86 |
- If set to V(absent), do nothing if host already does not exists. |
| 87 |
- If set to V(add_or_reconnect), add the host if it's absent else reconnect it and update the location. |
| 88 |
- If set to V(reconnect), then reconnect the host if it's present and update the location. |
| 89 |
- If set to V(disconnected), disconnect the host if the host already exists. |
| 90 |
default: present |
| 91 |
choices: ['present', 'absent', 'add_or_reconnect', 'reconnect', 'disconnected'] |
| 92 |
type: str |
| 93 |
esxi_ssl_thumbprint: |
| 94 |
description: |
| 95 |
- "Specifying the hostsystem certificate's thumbprint." |
| 96 |
- "Use following command to get hostsystem certificate's thumbprint - " |
| 97 |
- "# openssl x509 -in /etc/vmware/ssl/rui.crt -fingerprint -sha1 -noout" |
| 98 |
default: '' |
| 99 |
type: str |
| 100 |
aliases: ['ssl_thumbprint'] |
| 101 |
fetch_ssl_thumbprint: |
| 102 |
description: |
| 103 |
- Fetch the thumbprint of the host's SSL certificate. |
| 104 |
- This basically disables the host certificate verification (check if it was signed by a recognized CA). |
| 105 |
- Disable this option if you want to allow only hosts with valid certificates to be added to vCenter. |
| 106 |
- If this option is set to V(false) and the certificate can't be verified, an add or reconnect will fail. |
| 107 |
- Unused when O(esxi_ssl_thumbprint) is set. |
| 108 |
- Optional for reconnect, but only used if O(esxi_username) and O(esxi_password) are used. |
| 109 |
- Unused for removing. |
| 110 |
type: bool |
| 111 |
default: true |
| 112 |
force_connection: |
| 113 |
description: |
| 114 |
- Force the connection if the host is already being managed by another vCenter server. |
| 115 |
type: bool |
| 116 |
default: true |
| 117 |
reconnect_disconnected: |
| 118 |
description: |
| 119 |
- Reconnect disconnected hosts. |
| 120 |
- This is only used if O(state=present) and if the host already exists. |
| 121 |
type: bool |
| 122 |
default: true |
| 123 |
extends_documentation_fragment: |
| 124 |
- vmware.vmware.base_options |
| 125 |
|
| 126 |
''' |
| 127 |
|
| 128 |
EXAMPLES = r''' |
| 129 |
- name: Add ESXi Host to vCenter |
| 130 |
community.vmware.vmware_host: |
| 131 |
hostname: '{{ vcenter_hostname }}' |
| 132 |
username: '{{ vcenter_username }}' |
| 133 |
password: '{{ vcenter_password }}' |
| 134 |
datacenter: datacenter_name |
| 135 |
cluster: cluster_name |
| 136 |
esxi_hostname: '{{ esxi_hostname }}' |
| 137 |
esxi_username: '{{ esxi_username }}' |
| 138 |
esxi_password: '{{ esxi_password }}' |
| 139 |
state: present |
| 140 |
delegate_to: localhost |
| 141 |
|
| 142 |
- name: Add ESXi Host to vCenter under a specific folder |
| 143 |
community.vmware.vmware_host: |
| 144 |
hostname: '{{ vcenter_hostname }}' |
| 145 |
username: '{{ vcenter_username }}' |
| 146 |
password: '{{ vcenter_password }}' |
| 147 |
datacenter: datacenter_name |
| 148 |
folder: '/Site2/Asia-Cluster/host' |
| 149 |
esxi_hostname: '{{ esxi_hostname }}' |
| 150 |
esxi_username: '{{ esxi_username }}' |
| 151 |
esxi_password: '{{ esxi_password }}' |
| 152 |
state: present |
| 153 |
add_connected: true |
| 154 |
delegate_to: localhost |
| 155 |
|
| 156 |
- name: Reconnect ESXi Host (with username/password set) |
| 157 |
community.vmware.vmware_host: |
| 158 |
hostname: '{{ vcenter_hostname }}' |
| 159 |
username: '{{ vcenter_username }}' |
| 160 |
password: '{{ vcenter_password }}' |
| 161 |
datacenter: datacenter_name |
| 162 |
cluster: cluster_name |
| 163 |
esxi_hostname: '{{ esxi_hostname }}' |
| 164 |
esxi_username: '{{ esxi_username }}' |
| 165 |
esxi_password: '{{ esxi_password }}' |
| 166 |
state: reconnect |
| 167 |
delegate_to: localhost |
| 168 |
|
| 169 |
- name: Reconnect ESXi Host (with default username/password) |
| 170 |
community.vmware.vmware_host: |
| 171 |
hostname: '{{ vcenter_hostname }}' |
| 172 |
username: '{{ vcenter_username }}' |
| 173 |
password: '{{ vcenter_password }}' |
| 174 |
datacenter: datacenter_name |
| 175 |
cluster: cluster_name |
| 176 |
esxi_hostname: '{{ esxi_hostname }}' |
| 177 |
state: reconnect |
| 178 |
delegate_to: localhost |
| 179 |
|
| 180 |
- name: Add ESXi Host with SSL Thumbprint to vCenter |
| 181 |
community.vmware.vmware_host: |
| 182 |
hostname: '{{ vcenter_hostname }}' |
| 183 |
username: '{{ vcenter_username }}' |
| 184 |
password: '{{ vcenter_password }}' |
| 185 |
datacenter: datacenter_name |
| 186 |
cluster: cluster_name |
| 187 |
esxi_hostname: '{{ esxi_hostname }}' |
| 188 |
esxi_username: '{{ esxi_username }}' |
| 189 |
esxi_password: '{{ esxi_password }}' |
| 190 |
esxi_ssl_thumbprint: "3C:A5:60:6F:7A:B7:C4:6C:48:28:3D:2F:A5:EC:A3:58:13:88:F6:DD" |
| 191 |
state: present |
| 192 |
delegate_to: localhost |
| 193 |
''' |
| 194 |
|
| 195 |
RETURN = r''' |
| 196 |
result: |
| 197 |
description: metadata about the new host system added |
| 198 |
returned: on successful addition |
| 199 |
type: str |
| 200 |
sample: "Host already connected to vCenter 'vcenter01' in cluster 'cluster01'" |
| 201 |
''' |
| 202 |
|
| 203 |
try: |
| 204 |
from pyVmomi import vim, vmodl |
| 205 |
except ImportError: |
| 206 |
pass |
| 207 |
|
| 208 |
from ansible.module_utils.basic import AnsibleModule |
| 209 |
from ansible.module_utils.common.text.converters import to_native |
| 210 |
from ansible_collections.community.vmware.plugins.module_utils.vmware import ( |
| 211 |
PyVmomi, TaskError, wait_for_task, |
| 212 |
find_host_by_cluster_datacenter, find_hostsystem_by_name |
| 213 |
) |
| 214 |
from ansible_collections.vmware.vmware.plugins.module_utils.argument_spec import base_argument_spec |
| 215 |
|
| 216 |
|
| 217 |
class VMwareHost(PyVmomi): |
| 218 |
"""Class to manage vCenter connection""" |
| 219 |
|
| 220 |
def __init__(self, module): |
| 221 |
super(VMwareHost, self).__init__(module) |
| 222 |
self.vcenter = module.params['hostname'] |
| 223 |
self.datacenter_name = module.params['datacenter_name'] |
| 224 |
self.cluster_name = module.params['cluster_name'] |
| 225 |
self.folder_name = module.params['folder'] |
| 226 |
self.esxi_hostname = module.params['esxi_hostname'] |
| 227 |
self.esxi_username = module.params['esxi_username'] |
| 228 |
self.esxi_password = module.params['esxi_password'] |
| 229 |
self.state = module.params['state'] |
| 230 |
self.esxi_ssl_thumbprint = module.params.get('esxi_ssl_thumbprint', '') |
| 231 |
self.force_connection = module.params.get('force_connection') |
| 232 |
self.fetch_ssl_thumbprint = module.params.get('fetch_ssl_thumbprint') |
| 233 |
self.reconnect_disconnected = module.params.get('reconnect_disconnected') |
| 234 |
self.host_update = self.host = self.cluster = self.folder = self.host_parent_compute_resource = None |
| 235 |
|
| 236 |
def process_state(self): |
| 237 |
"""Check the current state""" |
| 238 |
host_states = { |
| 239 |
'absent': { |
| 240 |
'present': self.state_remove_host, |
| 241 |
'update': self.state_remove_host, |
| 242 |
'absent': self.state_exit_unchanged, |
| 243 |
}, |
| 244 |
'present': { |
| 245 |
'present': self.state_exit_unchanged, |
| 246 |
'update': self.state_update_host, |
| 247 |
'absent': self.state_add_host, |
| 248 |
}, |
| 249 |
'add_or_reconnect': { |
| 250 |
'present': self.state_reconnect_host, |
| 251 |
'update': self.state_update_host, |
| 252 |
'absent': self.state_add_host, |
| 253 |
}, |
| 254 |
'reconnect': { |
| 255 |
'present': self.state_reconnect_host, |
| 256 |
'update': self.state_update_host, |
| 257 |
}, |
| 258 |
'disconnected': { |
| 259 |
'present': self.state_disconnected_host, |
| 260 |
'absent': self.state_exit_unchanged, |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
try: |
| 265 |
host_states[self.state][self.check_host_state()]() |
| 266 |
except vmodl.RuntimeFault as runtime_fault: |
| 267 |
self.module.fail_json(msg=to_native(runtime_fault.msg)) |
| 268 |
except vmodl.MethodFault as method_fault: |
| 269 |
self.module.fail_json(msg=to_native(method_fault.msg)) |
| 270 |
except Exception as e: |
| 271 |
self.module.fail_json(msg=to_native(e)) |
| 272 |
|
| 273 |
def check_host_state(self): |
| 274 |
"""Check current state""" |
| 275 |
# Check if the host is already connected to vCenter |
| 276 |
self.host_update = find_hostsystem_by_name(self.content, self.esxi_hostname) |
| 277 |
if self.host_update: |
| 278 |
# The host name is unique in vCenter; A host with the same name cannot exist in another datacenter |
| 279 |
# However, the module will fail later if the target folder/cluster is in another datacenter as the host |
| 280 |
# Check if the host is connected under the target cluster |
| 281 |
if self.cluster_name: |
| 282 |
self.host, self.cluster = self.search_cluster(self.datacenter_name, self.cluster_name, self.esxi_hostname) |
| 283 |
if self.host: |
| 284 |
state = 'present' |
| 285 |
else: |
| 286 |
state = 'update' |
| 287 |
# Check if the host is connected under the target folder |
| 288 |
elif self.folder_name: |
| 289 |
self.folder = self.search_folder(self.folder_name) |
| 290 |
for child in self.folder.childEntity: |
| 291 |
if not child or not isinstance(child, vim.ComputeResource): |
| 292 |
continue |
| 293 |
try: |
| 294 |
if isinstance(child.host[0], vim.HostSystem) and child.name == self.esxi_hostname: |
| 295 |
self.host_parent_compute_resource = child |
| 296 |
self.host = child.host[0] |
| 297 |
break |
| 298 |
except IndexError: |
| 299 |
continue |
| 300 |
if self.host: |
| 301 |
state = 'present' |
| 302 |
else: |
| 303 |
state = 'update' |
| 304 |
else: |
| 305 |
state = 'absent' |
| 306 |
return state |
| 307 |
|
| 308 |
def search_folder(self, folder_name): |
| 309 |
""" |
| 310 |
Search folder in vCenter |
| 311 |
Returns: folder object |
| 312 |
""" |
| 313 |
search_index = self.content.searchIndex |
| 314 |
folder_obj = search_index.FindByInventoryPath(folder_name) |
| 315 |
if not (folder_obj and isinstance(folder_obj, vim.Folder)): |
| 316 |
self.module.fail_json(msg="Folder '%s' not found" % folder_name) |
| 317 |
return folder_obj |
| 318 |
|
| 319 |
def search_cluster(self, datacenter_name, cluster_name, esxi_hostname): |
| 320 |
""" |
| 321 |
Search cluster in vCenter |
| 322 |
Returns: host and cluster object |
| 323 |
""" |
| 324 |
return find_host_by_cluster_datacenter( |
| 325 |
self.module, self.content, datacenter_name, cluster_name, esxi_hostname |
| 326 |
) |
| 327 |
|
| 328 |
def state_exit_unchanged(self): |
| 329 |
"""Exit with status message""" |
| 330 |
if not self.host_update: |
| 331 |
result = "Host already disconnected" |
| 332 |
elif self.reconnect_disconnected and self.host_update.runtime.connectionState == 'disconnected': |
| 333 |
self.state_reconnect_host() |
| 334 |
else: |
| 335 |
if self.folder_name: |
| 336 |
result = "Host already connected to vCenter '%s' in folder '%s'" % (self.vcenter, self.folder_name) |
| 337 |
elif self.cluster_name: |
| 338 |
result = "Host already connected to vCenter '%s' in cluster '%s'" % (self.vcenter, self.cluster_name) |
| 339 |
self.module.exit_json(changed=False, result=str(result)) |
| 340 |
|
| 341 |
def state_add_host(self): |
| 342 |
"""Add ESXi host to a cluster of folder in vCenter""" |
| 343 |
changed = True |
| 344 |
result = None |
| 345 |
|
| 346 |
if self.module.check_mode: |
| 347 |
result = "Host would be connected to vCenter '%s'" % self.vcenter |
| 348 |
else: |
| 349 |
host_connect_spec = self.get_host_connect_spec() |
| 350 |
as_connected = self.params.get('add_connected') |
| 351 |
esxi_license = None |
| 352 |
resource_pool = None |
| 353 |
task = None |
| 354 |
if self.folder_name: |
| 355 |
self.folder = self.search_folder(self.folder_name) |
| 356 |
try: |
| 357 |
task = self.folder.AddStandaloneHost( |
| 358 |
spec=host_connect_spec, compResSpec=resource_pool, |
| 359 |
addConnected=as_connected, license=esxi_license |
| 360 |
) |
| 361 |
except vim.fault.InvalidLogin as invalid_login: |
| 362 |
self.module.fail_json( |
| 363 |
msg="Cannot authenticate with the host : %s" % to_native(invalid_login) |
| 364 |
) |
| 365 |
except vim.fault.HostConnectFault as connect_fault: |
| 366 |
self.module.fail_json( |
| 367 |
msg="An error occurred during connect : %s" % to_native(connect_fault) |
| 368 |
) |
| 369 |
except vim.fault.DuplicateName as duplicate_name: |
| 370 |
self.module.fail_json( |
| 371 |
msg="The folder already contains a host with the same name : %s" % |
| 372 |
to_native(duplicate_name) |
| 373 |
) |
| 374 |
except vmodl.fault.InvalidArgument as invalid_argument: |
| 375 |
self.module.fail_json( |
| 376 |
msg="An argument was specified incorrectly : %s" % to_native(invalid_argument) |
| 377 |
) |
| 378 |
except vim.fault.AlreadyBeingManaged as already_managed: |
| 379 |
self.module.fail_json( |
| 380 |
msg="The host is already being managed by another vCenter server : %s" % |
| 381 |
to_native(already_managed) |
| 382 |
) |
| 383 |
except vmodl.fault.NotEnoughLicenses as not_enough_licenses: |
| 384 |
self.module.fail_json( |
| 385 |
msg="There are not enough licenses to add this host : %s" % to_native(not_enough_licenses) |
| 386 |
) |
| 387 |
except vim.fault.NoHost as no_host: |
| 388 |
self.module.fail_json( |
| 389 |
msg="Unable to contact the host : %s" % to_native(no_host) |
| 390 |
) |
| 391 |
except vmodl.fault.NotSupported as not_supported: |
| 392 |
self.module.fail_json( |
| 393 |
msg="The folder is not a host folder : %s" % to_native(not_supported) |
| 394 |
) |
| 395 |
except vim.fault.NotSupportedHost as host_not_supported: |
| 396 |
self.module.fail_json( |
| 397 |
msg="The host is running a software version that is not supported : %s" % |
| 398 |
to_native(host_not_supported) |
| 399 |
) |
| 400 |
except vim.fault.AgentInstallFailed as agent_install: |
| 401 |
self.module.fail_json( |
| 402 |
msg="Error during vCenter agent installation : %s" % to_native(agent_install) |
| 403 |
) |
| 404 |
except vim.fault.AlreadyConnected as already_connected: |
| 405 |
self.module.fail_json( |
| 406 |
msg="The host is already connected to the vCenter server : %s" % to_native(already_connected) |
| 407 |
) |
| 408 |
except vim.fault.SSLVerifyFault as ssl_fault: |
| 409 |
self.module.fail_json( |
| 410 |
msg="The host certificate could not be authenticated : %s" % to_native(ssl_fault) |
| 411 |
) |
| 412 |
elif self.cluster_name: |
| 413 |
self.host, self.cluster = self.search_cluster( |
| 414 |
self.datacenter_name, |
| 415 |
self.cluster_name, |
| 416 |
self.esxi_hostname |
| 417 |
) |
| 418 |
try: |
| 419 |
task = self.cluster.AddHost_Task( |
| 420 |
spec=host_connect_spec, asConnected=as_connected, |
| 421 |
resourcePool=resource_pool, license=esxi_license |
| 422 |
) |
| 423 |
except vim.fault.InvalidLogin as invalid_login: |
| 424 |
self.module.fail_json( |
| 425 |
msg="Cannot authenticate with the host : %s" % to_native(invalid_login) |
| 426 |
) |
| 427 |
except vim.fault.HostConnectFault as connect_fault: |
| 428 |
self.module.fail_json( |
| 429 |
msg="An error occurred during connect : %s" % to_native(connect_fault) |
| 430 |
) |
| 431 |
except vim.fault.DuplicateName as duplicate_name: |
| 432 |
self.module.fail_json( |
| 433 |
msg="The cluster already contains a host with the same name : %s" % |
| 434 |
to_native(duplicate_name) |
| 435 |
) |
| 436 |
except vim.fault.AlreadyBeingManaged as already_managed: |
| 437 |
self.module.fail_json( |
| 438 |
msg="The host is already being managed by another vCenter server : %s" % |
| 439 |
to_native(already_managed) |
| 440 |
) |
| 441 |
except vmodl.fault.NotEnoughLicenses as not_enough_licenses: |
| 442 |
self.module.fail_json( |
| 443 |
msg="There are not enough licenses to add this host : %s" % to_native(not_enough_licenses) |
| 444 |
) |
| 445 |
except vim.fault.NoHost as no_host: |
| 446 |
self.module.fail_json( |
| 447 |
msg="Unable to contact the host : %s" % to_native(no_host) |
| 448 |
) |
| 449 |
except vim.fault.NotSupportedHost as host_not_supported: |
| 450 |
self.module.fail_json( |
| 451 |
msg="The host is running a software version that is not supported; " |
| 452 |
"It may still be possible to add the host as a stand-alone host : %s" % |
| 453 |
to_native(host_not_supported) |
| 454 |
) |
| 455 |
except vim.fault.TooManyHosts as too_many_hosts: |
| 456 |
self.module.fail_json( |
| 457 |
msg="No additional hosts can be added to the cluster : %s" % to_native(too_many_hosts) |
| 458 |
) |
| 459 |
except vim.fault.AgentInstallFailed as agent_install: |
| 460 |
self.module.fail_json( |
| 461 |
msg="Error during vCenter agent installation : %s" % to_native(agent_install) |
| 462 |
) |
| 463 |
except vim.fault.AlreadyConnected as already_connected: |
| 464 |
self.module.fail_json( |
| 465 |
msg="The host is already connected to the vCenter server : %s" % to_native(already_connected) |
| 466 |
) |
| 467 |
except vim.fault.SSLVerifyFault as ssl_fault: |
| 468 |
self.module.fail_json( |
| 469 |
msg="The host certificate could not be authenticated : %s" % to_native(ssl_fault) |
| 470 |
) |
| 471 |
try: |
| 472 |
changed, result = wait_for_task(task) |
| 473 |
result = "Host connected to vCenter '%s'" % self.vcenter |
| 474 |
except TaskError as task_error: |
| 475 |
self.module.fail_json( |
| 476 |
msg="Failed to add host to vCenter '%s' : %s" % (self.vcenter, to_native(task_error)) |
| 477 |
) |
| 478 |
|
| 479 |
self.module.exit_json(changed=changed, result=result) |
| 480 |
|
| 481 |
def get_host_connect_spec(self): |
| 482 |
""" |
| 483 |
Function to return Host connection specification |
| 484 |
Returns: host connection specification |
| 485 |
""" |
| 486 |
# Get the thumbprint of the SSL certificate |
| 487 |
if self.fetch_ssl_thumbprint and self.esxi_ssl_thumbprint == '': |
| 488 |
sslThumbprint = self.get_cert_fingerprint(self.esxi_hostname, self.module.params['port'], |
| 489 |
self.module.params['proxy_host'], self.module.params['proxy_port']) |
| 490 |
else: |
| 491 |
sslThumbprint = self.esxi_ssl_thumbprint |
| 492 |
|
| 493 |
host_connect_spec = vim.host.ConnectSpec() |
| 494 |
host_connect_spec.sslThumbprint = sslThumbprint |
| 495 |
host_connect_spec.hostName = self.esxi_hostname |
| 496 |
host_connect_spec.userName = self.esxi_username |
| 497 |
host_connect_spec.password = self.esxi_password |
| 498 |
host_connect_spec.force = self.force_connection |
| 499 |
return host_connect_spec |
| 500 |
|
| 501 |
def state_reconnect_host(self): |
| 502 |
"""Reconnect host to vCenter""" |
| 503 |
changed = True |
| 504 |
result = None |
| 505 |
|
| 506 |
if self.module.check_mode: |
| 507 |
result = "Host would be reconnected to vCenter '%s'" % self.vcenter |
| 508 |
else: |
| 509 |
self.reconnect_host(self.host) |
| 510 |
result = "Host reconnected to vCenter '%s'" % self.vcenter |
| 511 |
self.module.exit_json(changed=changed, result=str(result)) |
| 512 |
|
| 513 |
def reconnect_host(self, host_object): |
| 514 |
"""Reconnect host to vCenter""" |
| 515 |
reconnecthost_args = {} |
| 516 |
reconnecthost_args['reconnectSpec'] = vim.HostSystem.ReconnectSpec() |
| 517 |
reconnecthost_args['reconnectSpec'].syncState = True |
| 518 |
|
| 519 |
if self.esxi_username and self.esxi_password: |
| 520 |
# Build the connection spec as well and fetch thumbprint if enabled |
| 521 |
# Useful if you reinstalled a host and it uses a new self-signed certificate |
| 522 |
reconnecthost_args['cnxSpec'] = self.get_host_connect_spec() |
| 523 |
try: |
| 524 |
task = host_object.ReconnectHost_Task(**reconnecthost_args) |
| 525 |
except vim.fault.InvalidLogin as invalid_login: |
| 526 |
self.module.fail_json( |
| 527 |
msg="Cannot authenticate with the host : %s" % to_native(invalid_login) |
| 528 |
) |
| 529 |
except vim.fault.InvalidState as invalid_state: |
| 530 |
self.module.fail_json( |
| 531 |
msg="The host is not disconnected : %s" % to_native(invalid_state) |
| 532 |
) |
| 533 |
except vim.fault.InvalidName as invalid_name: |
| 534 |
self.module.fail_json( |
| 535 |
msg="The host name is invalid : %s" % to_native(invalid_name) |
| 536 |
) |
| 537 |
except vim.fault.HostConnectFault as connect_fault: |
| 538 |
self.module.fail_json( |
| 539 |
msg="An error occurred during reconnect : %s" % to_native(connect_fault) |
| 540 |
) |
| 541 |
except vmodl.fault.NotSupported as not_supported: |
| 542 |
self.module.fail_json( |
| 543 |
msg="No host can be added to this group : %s" % to_native(not_supported) |
| 544 |
) |
| 545 |
except vim.fault.AlreadyBeingManaged as already_managed: |
| 546 |
self.module.fail_json( |
| 547 |
msg="The host is already being managed by another vCenter server : %s" % to_native(already_managed) |
| 548 |
) |
| 549 |
except vmodl.fault.NotEnoughLicenses as not_enough_licenses: |
| 550 |
self.module.fail_json( |
| 551 |
msg="There are not enough licenses to add this host : %s" % to_native(not_enough_licenses) |
| 552 |
) |
| 553 |
except vim.fault.NoHost as no_host: |
| 554 |
self.module.fail_json( |
| 555 |
msg="Unable to contact the host : %s" % to_native(no_host) |
| 556 |
) |
| 557 |
except vim.fault.NotSupportedHost as host_not_supported: |
| 558 |
self.module.fail_json( |
| 559 |
msg="The host is running a software version that is not supported : %s" % |
| 560 |
to_native(host_not_supported) |
| 561 |
) |
| 562 |
except vim.fault.SSLVerifyFault as ssl_fault: |
| 563 |
self.module.fail_json( |
| 564 |
msg="The host certificate could not be authenticated : %s" % to_native(ssl_fault) |
| 565 |
) |
| 566 |
try: |
| 567 |
changed, result = wait_for_task(task) |
| 568 |
except TaskError as task_error: |
| 569 |
self.module.fail_json( |
| 570 |
msg="Failed to reconnect host to vCenter '%s' due to %s" % |
| 571 |
(self.vcenter, to_native(task_error)) |
| 572 |
) |
| 573 |
|
| 574 |
def state_remove_host(self): |
| 575 |
"""Remove host from vCenter""" |
| 576 |
changed = True |
| 577 |
result = None |
| 578 |
if self.module.check_mode: |
| 579 |
result = "Host would be removed from vCenter '%s'" % self.vcenter |
| 580 |
else: |
| 581 |
# Check parent type |
| 582 |
parent_type = self.get_parent_type(self.host_update) |
| 583 |
if parent_type == 'cluster': |
| 584 |
self.put_host_in_maintenance_mode(self.host_update) |
| 585 |
try: |
| 586 |
if self.folder_name: |
| 587 |
task = self.host_parent_compute_resource.Destroy_Task() |
| 588 |
elif self.cluster_name: |
| 589 |
task = self.host.Destroy_Task() |
| 590 |
except vim.fault.VimFault as vim_fault: |
| 591 |
self.module.fail_json(msg=vim_fault) |
| 592 |
try: |
| 593 |
changed, result = wait_for_task(task) |
| 594 |
result = "Host removed from vCenter '%s'" % self.vcenter |
| 595 |
except TaskError as task_error: |
| 596 |
self.module.fail_json( |
| 597 |
msg="Failed to remove the host from vCenter '%s' : %s" % (self.vcenter, to_native(task_error)) |
| 598 |
) |
| 599 |
self.module.exit_json(changed=changed, result=str(result)) |
| 600 |
|
| 601 |
def put_host_in_maintenance_mode(self, host_object): |
| 602 |
"""Put host in maintenance mode, if not already""" |
| 603 |
if not host_object.runtime.inMaintenanceMode: |
| 604 |
try: |
| 605 |
try: |
| 606 |
maintenance_mode_task = host_object.EnterMaintenanceMode_Task(300, True, None) |
| 607 |
except vim.fault.InvalidState as invalid_state: |
| 608 |
self.module.fail_json( |
| 609 |
msg="The host is already in maintenance mode : %s" % to_native(invalid_state) |
| 610 |
) |
| 611 |
except vim.fault.Timedout as timed_out: |
| 612 |
self.module.fail_json( |
| 613 |
msg="The maintenance mode operation timed out : %s" % to_native(timed_out) |
| 614 |
) |
| 615 |
except vim.fault.Timedout as timed_out: |
| 616 |
self.module.fail_json( |
| 617 |
msg="The maintenance mode operation was canceled : %s" % to_native(timed_out) |
| 618 |
) |
| 619 |
wait_for_task(maintenance_mode_task) |
| 620 |
except TaskError as task_err: |
| 621 |
self.module.fail_json( |
| 622 |
msg="Failed to put the host in maintenance mode : %s" % to_native(task_err) |
| 623 |
) |
| 624 |
|
| 625 |
def get_parent_type(self, host_object): |
| 626 |
""" |
| 627 |
Get the type of the parent object |
| 628 |
Returns: string with 'folder' or 'cluster' |
| 629 |
""" |
| 630 |
object_type = None |
| 631 |
# check 'vim.ClusterComputeResource' first because it's also an |
| 632 |
# instance of 'vim.ComputeResource' |
| 633 |
if isinstance(host_object.parent, vim.ClusterComputeResource): |
| 634 |
object_type = 'cluster' |
| 635 |
elif isinstance(host_object.parent, vim.ComputeResource): |
| 636 |
object_type = 'folder' |
| 637 |
return object_type |
| 638 |
|
| 639 |
def state_update_host(self): |
| 640 |
"""Move host to a cluster or a folder, or vice versa""" |
| 641 |
changed = True |
| 642 |
result = None |
| 643 |
reconnect = False |
| 644 |
|
| 645 |
# Check if the host is disconnected if reconnect disconnected hosts is true |
| 646 |
if self.reconnect_disconnected and self.host_update.runtime.connectionState == 'disconnected': |
| 647 |
reconnect = True |
| 648 |
|
| 649 |
# Check parent type |
| 650 |
parent_type = self.get_parent_type(self.host_update) |
| 651 |
|
| 652 |
if self.folder_name: |
| 653 |
if self.module.check_mode: |
| 654 |
if reconnect or self.state == 'add_or_reconnect' or self.state == 'reconnect': |
| 655 |
result = "Host would be reconnected and moved to folder '%s'" % self.folder_name |
| 656 |
else: |
| 657 |
result = "Host would be moved to folder '%s'" % self.folder_name |
| 658 |
else: |
| 659 |
# Reconnect the host if disconnected or if specified by state |
| 660 |
if reconnect or self.state == 'add_or_reconnect' or self.state == 'reconnect': |
| 661 |
self.reconnect_host(self.host_update) |
| 662 |
try: |
| 663 |
task = None |
| 664 |
try: |
| 665 |
if parent_type == 'folder': |
| 666 |
# Move ESXi host from folder to folder |
| 667 |
task = self.folder.MoveIntoFolder_Task([self.host_update.parent]) |
| 668 |
elif parent_type == 'cluster': |
| 669 |
self.put_host_in_maintenance_mode(self.host_update) |
| 670 |
# Move ESXi host from cluster to folder |
| 671 |
task = self.folder.MoveIntoFolder_Task([self.host_update]) |
| 672 |
except vim.fault.DuplicateName as duplicate_name: |
| 673 |
self.module.fail_json( |
| 674 |
msg="The folder already contains an object with the specified name : %s" % |
| 675 |
to_native(duplicate_name) |
| 676 |
) |
| 677 |
except vim.fault.InvalidFolder as invalid_folder: |
| 678 |
self.module.fail_json( |
| 679 |
msg="The parent of this folder is in the list of objects : %s" % |
| 680 |
to_native(invalid_folder) |
| 681 |
) |
| 682 |
except vim.fault.InvalidState as invalid_state: |
| 683 |
self.module.fail_json( |
| 684 |
msg="Failed to move host, this can be due to either of following :" |
| 685 |
" 1. The host is not part of the same datacenter, 2. The host is not in maintenance mode : %s" % |
| 686 |
to_native(invalid_state) |
| 687 |
) |
| 688 |
except vmodl.fault.NotSupported as not_supported: |
| 689 |
self.module.fail_json( |
| 690 |
msg="The target folder is not a host folder : %s" % |
| 691 |
to_native(not_supported) |
| 692 |
) |
| 693 |
except vim.fault.DisallowedOperationOnFailoverHost as failover_host: |
| 694 |
self.module.fail_json( |
| 695 |
msg="The host is configured as a failover host : %s" % |
| 696 |
to_native(failover_host) |
| 697 |
) |
| 698 |
except vim.fault.VmAlreadyExistsInDatacenter as already_exists: |
| 699 |
self.module.fail_json( |
| 700 |
msg="The host's virtual machines are already registered to a host in " |
| 701 |
"the destination datacenter : %s" % to_native(already_exists) |
| 702 |
) |
| 703 |
changed, result = wait_for_task(task) |
| 704 |
except TaskError as task_error_exception: |
| 705 |
task_error = task_error_exception.args[0] |
| 706 |
self.module.fail_json( |
| 707 |
msg="Failed to move host %s to folder %s due to %s" % |
| 708 |
(self.esxi_hostname, self.folder_name, to_native(task_error)) |
| 709 |
) |
| 710 |
if reconnect or self.state == 'add_or_reconnect' or self.state == 'reconnect': |
| 711 |
result = "Host reconnected and moved to folder '%s'" % self.folder_name |
| 712 |
else: |
| 713 |
result = "Host moved to folder '%s'" % self.folder_name |
| 714 |
elif self.cluster_name: |
| 715 |
if self.module.check_mode: |
| 716 |
result = "Host would be moved to cluster '%s'" % self.cluster_name |
| 717 |
else: |
| 718 |
if parent_type == 'cluster': |
| 719 |
# Put host in maintenance mode if moved from another cluster |
| 720 |
self.put_host_in_maintenance_mode(self.host_update) |
| 721 |
resource_pool = None |
| 722 |
try: |
| 723 |
try: |
| 724 |
task = self.cluster.MoveHostInto_Task( |
| 725 |
host=self.host_update, resourcePool=resource_pool |
| 726 |
) |
| 727 |
except vim.fault.TooManyHosts as too_many_hosts: |
| 728 |
self.module.fail_json( |
| 729 |
msg="No additional hosts can be added to the cluster : %s" % to_native(too_many_hosts) |
| 730 |
) |
| 731 |
except vim.fault.InvalidState as invalid_state: |
| 732 |
self.module.fail_json( |
| 733 |
msg="The host is already part of a cluster and is not in maintenance mode : %s" % |
| 734 |
to_native(invalid_state) |
| 735 |
) |
| 736 |
except vmodl.fault.InvalidArgument as invalid_argument: |
| 737 |
self.module.fail_json( |
| 738 |
msg="Failed to move host, this can be due to either of following :" |
| 739 |
" 1. The host is is not a part of the same datacenter as the cluster," |
| 740 |
" 2. The source and destination clusters are the same : %s" % |
| 741 |
to_native(invalid_argument) |
| 742 |
) |
| 743 |
changed, result = wait_for_task(task) |
| 744 |
except TaskError as task_error_exception: |
| 745 |
task_error = task_error_exception.args[0] |
| 746 |
self.module.fail_json( |
| 747 |
msg="Failed to move host to cluster '%s' due to : %s" % |
| 748 |
(self.cluster_name, to_native(task_error)) |
| 749 |
) |
| 750 |
if reconnect or self.state == 'add_or_reconnect' or self.state == 'reconnect': |
| 751 |
result = "Host reconnected and moved to cluster '%s'" % self.cluster_name |
| 752 |
else: |
| 753 |
result = "Host moved to cluster '%s'" % self.cluster_name |
| 754 |
|
| 755 |
self.module.exit_json(changed=changed, msg=str(result)) |
| 756 |
|
| 757 |
def state_disconnected_host(self): |
| 758 |
"""Disconnect host to vCenter""" |
| 759 |
changed = True |
| 760 |
result = None |
| 761 |
|
| 762 |
if self.module.check_mode: |
| 763 |
if self.host.runtime.connectionState == 'disconnected': |
| 764 |
result = "Host already disconnected" |
| 765 |
changed = False |
| 766 |
else: |
| 767 |
result = "Host would be disconnected host from vCenter '%s'" % self.vcenter |
| 768 |
else: |
| 769 |
if self.host.runtime.connectionState == 'disconnected': |
| 770 |
changed = False |
| 771 |
result = "Host already disconnected" |
| 772 |
else: |
| 773 |
self.disconnect_host(self.host) |
| 774 |
result = "Host disconnected from vCenter '%s'" % self.vcenter |
| 775 |
self.module.exit_json(changed=changed, result=to_native(result)) |
| 776 |
|
| 777 |
def disconnect_host(self, host_object): |
| 778 |
"""Disconnect host to vCenter""" |
| 779 |
try: |
| 780 |
task = host_object.DisconnectHost_Task() |
| 781 |
except Exception as e: |
| 782 |
self.module.fail_json(msg="Failed to disconnect host from vCenter: %s" % to_native(e)) |
| 783 |
|
| 784 |
try: |
| 785 |
changed, result = wait_for_task(task) |
| 786 |
except TaskError as task_error: |
| 787 |
self.module.fail_json( |
| 788 |
msg="Failed to disconnect host from vCenter '%s' due to %s" % |
| 789 |
(self.vcenter, to_native(task_error)) |
| 790 |
) |
| 791 |
|
| 792 |
|
| 793 |
def main(): |
| 794 |
"""Main""" |
| 795 |
argument_spec = base_argument_spec() |
| 796 |
argument_spec.update( |
| 797 |
datacenter_name=dict(type='str', required=True, aliases=['datacenter']), |
| 798 |
cluster_name=dict(type='str', aliases=['cluster']), |
| 799 |
esxi_hostname=dict(type='str', required=True), |
| 800 |
esxi_username=dict(type='str'), |
| 801 |
esxi_password=dict(type='str', no_log=True), |
| 802 |
esxi_ssl_thumbprint=dict(type='str', default='', aliases=['ssl_thumbprint']), |
| 803 |
fetch_ssl_thumbprint=dict(type='bool', default=True), |
| 804 |
state=dict(default='present', |
| 805 |
choices=['present', 'absent', 'add_or_reconnect', 'reconnect', 'disconnected'], |
| 806 |
type='str'), |
| 807 |
folder=dict(type='str', aliases=['folder_name']), |
| 808 |
add_connected=dict(type='bool', default=True), |
| 809 |
force_connection=dict(type='bool', default=True), |
| 810 |
reconnect_disconnected=dict(type='bool', default=True), |
| 811 |
) |
| 812 |
|
| 813 |
module = AnsibleModule( |
| 814 |
argument_spec=argument_spec, |
| 815 |
supports_check_mode=True, |
| 816 |
required_if=[ |
| 817 |
['state', 'present', ['esxi_username', 'esxi_password']], |
| 818 |
['state', 'add_or_reconnect', ['esxi_username', 'esxi_password']] |
| 819 |
], |
| 820 |
required_one_of=[ |
| 821 |
['cluster_name', 'folder'], |
| 822 |
], |
| 823 |
mutually_exclusive=[ |
| 824 |
['cluster_name', 'folder'], |
| 825 |
] |
| 826 |
) |
| 827 |
|
| 828 |
vmware_host = VMwareHost(module) |
| 829 |
vmware_host.process_state() |
| 830 |
|
| 831 |
|
| 832 |
if __name__ == '__main__': |
| 833 |
main() |