2015-04-17 20:48:57 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# (c) 2015, Joseph Callen <jcallen () csc.com>
|
|
|
|
#
|
|
|
|
# This file is part of Ansible
|
|
|
|
#
|
|
|
|
# Ansible is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# Ansible is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
import atexit
|
|
|
|
import time
|
2016-01-27 20:41:28 +01:00
|
|
|
import ssl
|
2015-04-17 20:48:57 +02:00
|
|
|
# requests is required for exception handling of the ConnectionError
|
|
|
|
import requests
|
|
|
|
from pyVim import connect
|
|
|
|
from pyVmomi import vim, vmodl
|
|
|
|
HAS_PYVMOMI = True
|
|
|
|
except ImportError:
|
|
|
|
HAS_PYVMOMI = False
|
|
|
|
|
|
|
|
|
|
|
|
class TaskError(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
def wait_for_task(task):
|
|
|
|
|
|
|
|
while True:
|
2015-04-21 14:33:32 +02:00
|
|
|
if task.info.state == vim.TaskInfo.State.success:
|
|
|
|
return True, task.info.result
|
2015-04-22 20:54:05 +02:00
|
|
|
if task.info.state == vim.TaskInfo.State.error:
|
2015-04-21 14:33:32 +02:00
|
|
|
try:
|
|
|
|
raise TaskError(task.info.error)
|
|
|
|
except AttributeError:
|
|
|
|
raise TaskError("An unknown error has occurred")
|
|
|
|
if task.info.state == vim.TaskInfo.State.running:
|
|
|
|
time.sleep(15)
|
2015-04-22 20:54:05 +02:00
|
|
|
if task.info.state == vim.TaskInfo.State.queued:
|
2015-04-21 14:33:32 +02:00
|
|
|
time.sleep(15)
|
2015-04-17 20:48:57 +02:00
|
|
|
|
|
|
|
|
|
|
|
def find_dvspg_by_name(dv_switch, portgroup_name):
|
2015-04-21 14:33:32 +02:00
|
|
|
|
2015-04-17 20:48:57 +02:00
|
|
|
portgroups = dv_switch.portgroup
|
|
|
|
|
|
|
|
for pg in portgroups:
|
|
|
|
if pg.name == portgroup_name:
|
|
|
|
return pg
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
def find_cluster_by_name_datacenter(datacenter, cluster_name):
|
2015-04-21 14:33:32 +02:00
|
|
|
|
|
|
|
host_folder = datacenter.hostFolder
|
|
|
|
for folder in host_folder.childEntity:
|
|
|
|
if folder.name == cluster_name:
|
|
|
|
return folder
|
|
|
|
return None
|
2015-04-17 20:48:57 +02:00
|
|
|
|
|
|
|
|
2015-04-22 20:54:05 +02:00
|
|
|
def find_datacenter_by_name(content, datacenter_name):
|
2015-04-17 20:48:57 +02:00
|
|
|
|
2015-04-21 14:33:32 +02:00
|
|
|
datacenters = get_all_objs(content, [vim.Datacenter])
|
|
|
|
for dc in datacenters:
|
|
|
|
if dc.name == datacenter_name:
|
|
|
|
return dc
|
|
|
|
|
|
|
|
return None
|
2015-04-17 20:48:57 +02:00
|
|
|
|
|
|
|
|
|
|
|
def find_dvs_by_name(content, switch_name):
|
2015-04-21 14:33:32 +02:00
|
|
|
|
|
|
|
vmware_distributed_switches = get_all_objs(content, [vim.dvs.VmwareDistributedVirtualSwitch])
|
|
|
|
for dvs in vmware_distributed_switches:
|
|
|
|
if dvs.name == switch_name:
|
|
|
|
return dvs
|
|
|
|
return None
|
2015-04-17 20:48:57 +02:00
|
|
|
|
|
|
|
|
|
|
|
def find_hostsystem_by_name(content, hostname):
|
2015-04-21 14:33:32 +02:00
|
|
|
|
|
|
|
host_system = get_all_objs(content, [vim.HostSystem])
|
|
|
|
for host in host_system:
|
|
|
|
if host.name == hostname:
|
|
|
|
return host
|
|
|
|
return None
|
2015-04-17 20:48:57 +02:00
|
|
|
|
|
|
|
|
2016-03-05 20:50:51 +01:00
|
|
|
def find_vm_by_name(content, vm_name):
|
|
|
|
|
|
|
|
vms = get_all_objs(content, [vim.VirtualMachine])
|
|
|
|
for vm in vms:
|
|
|
|
if vm.name == vm_name:
|
|
|
|
return vm
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
2016-03-16 19:24:20 +01:00
|
|
|
def find_host_portgroup_by_name(host, portgroup_name):
|
|
|
|
|
|
|
|
for portgroup in host.config.network.portgroup:
|
|
|
|
if portgroup.spec.name == portgroup_name:
|
|
|
|
return portgroup
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
2015-04-17 20:48:57 +02:00
|
|
|
def vmware_argument_spec():
|
2015-04-21 14:33:32 +02:00
|
|
|
|
2015-04-17 20:48:57 +02:00
|
|
|
return dict(
|
|
|
|
hostname=dict(type='str', required=True),
|
|
|
|
username=dict(type='str', aliases=['user', 'admin'], required=True),
|
|
|
|
password=dict(type='str', aliases=['pass', 'pwd'], required=True, no_log=True),
|
2016-02-02 15:47:56 +01:00
|
|
|
validate_certs=dict(type='bool', required=False, default=True),
|
2015-04-17 20:48:57 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def connect_to_api(module, disconnect_atexit=True):
|
2015-04-21 14:33:32 +02:00
|
|
|
|
2015-04-17 20:48:57 +02:00
|
|
|
hostname = module.params['hostname']
|
|
|
|
username = module.params['username']
|
|
|
|
password = module.params['password']
|
2016-02-02 15:47:56 +01:00
|
|
|
validate_certs = module.params['validate_certs']
|
2016-01-27 20:41:28 +01:00
|
|
|
|
2016-02-09 08:49:26 +01:00
|
|
|
if validate_certs and not hasattr(ssl, 'SSLContext'):
|
|
|
|
module.fail_json(msg='pyVim does not support changing verification mode with python < 2.7.9. Either update python or or use validate_certs=false')
|
2015-04-17 20:48:57 +02:00
|
|
|
|
2016-02-09 08:49:26 +01:00
|
|
|
try:
|
|
|
|
service_instance = connect.SmartConnect(host=hostname, user=username, pwd=password)
|
2015-08-01 05:01:43 +02:00
|
|
|
except vim.fault.InvalidLogin, invalid_login:
|
2015-04-21 14:33:32 +02:00
|
|
|
module.fail_json(msg=invalid_login.msg, apierror=str(invalid_login))
|
2015-08-01 05:01:43 +02:00
|
|
|
except requests.ConnectionError, connection_error:
|
2016-02-09 08:49:26 +01:00
|
|
|
if '[SSL: CERTIFICATE_VERIFY_FAILED]' in str(connection_error) and not validate_certs:
|
|
|
|
context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
|
|
|
|
context.verify_mode = ssl.CERT_NONE
|
|
|
|
service_instance = connect.SmartConnect(host=hostname, user=username, pwd=password, sslContext=context)
|
|
|
|
else:
|
|
|
|
module.fail_json(msg="Unable to connect to vCenter or ESXi API on TCP/443.", apierror=str(connection_error))
|
|
|
|
|
|
|
|
# Disabling atexit should be used in special cases only.
|
|
|
|
# Such as IP change of the ESXi host which removes the connection anyway.
|
|
|
|
# Also removal significantly speeds up the return of the module
|
|
|
|
if disconnect_atexit:
|
|
|
|
atexit.register(connect.Disconnect, service_instance)
|
|
|
|
return service_instance.RetrieveContent()
|
2015-04-17 20:48:57 +02:00
|
|
|
|
|
|
|
def get_all_objs(content, vimtype):
|
2015-04-21 14:33:32 +02:00
|
|
|
|
|
|
|
obj = {}
|
|
|
|
container = content.viewManager.CreateContainerView(content.rootFolder, vimtype, True)
|
|
|
|
for managed_object_ref in container.view:
|
|
|
|
obj.update({managed_object_ref: managed_object_ref.name})
|
|
|
|
return obj
|