| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2020, Anusha Hegde <[email protected]> |
| 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 |
|
| 12 |
DOCUMENTATION = r''' |
| 13 |
module: vmware_guest_cross_vc_clone |
| 14 |
|
| 15 |
short_description: Cross-vCenter VM/template clone |
| 16 |
|
| 17 |
|
| 18 |
description: |
| 19 |
- 'This module can be used for Cross-vCenter vm/template clone' |
| 20 |
|
| 21 |
options: |
| 22 |
name: |
| 23 |
description: |
| 24 |
- Name of the virtual machine or template. |
| 25 |
- This is a required parameter, if parameter O(uuid) or O(moid) is not supplied. |
| 26 |
type: str |
| 27 |
uuid: |
| 28 |
description: |
| 29 |
- UUID of the vm/template instance to clone from, this is VMware's unique identifier. |
| 30 |
- This is a required parameter, if parameter O(name) or O(moid) is not supplied. |
| 31 |
type: str |
| 32 |
moid: |
| 33 |
description: |
| 34 |
- Managed Object ID of the vm/template instance to manage if known, this is a unique identifier only within a single vCenter instance. |
| 35 |
- This is required if O(name) or O(uuid) is not supplied. |
| 36 |
type: str |
| 37 |
use_instance_uuid: |
| 38 |
description: |
| 39 |
- Whether to use the VMware instance UUID rather than the BIOS UUID. |
| 40 |
default: false |
| 41 |
type: bool |
| 42 |
destination_vm_name: |
| 43 |
description: |
| 44 |
- The name of the cloned VM. |
| 45 |
type: str |
| 46 |
required: true |
| 47 |
destination_vcenter: |
| 48 |
description: |
| 49 |
- The hostname or IP address of the destination VCenter. |
| 50 |
type: str |
| 51 |
required: true |
| 52 |
destination_vcenter_username: |
| 53 |
description: |
| 54 |
- The username of the destination VCenter. |
| 55 |
type: str |
| 56 |
required: true |
| 57 |
destination_vcenter_password: |
| 58 |
description: |
| 59 |
- The password of the destination VCenter. |
| 60 |
type: str |
| 61 |
required: true |
| 62 |
destination_vcenter_port: |
| 63 |
description: |
| 64 |
- The port to establish connection in the destination VCenter. |
| 65 |
type: int |
| 66 |
default: 443 |
| 67 |
destination_vcenter_validate_certs: |
| 68 |
description: |
| 69 |
- Parameter to indicate if certification validation needs to be done on destination VCenter. |
| 70 |
type: bool |
| 71 |
default: false |
| 72 |
destination_host: |
| 73 |
description: |
| 74 |
- The name of the destination host. |
| 75 |
type: str |
| 76 |
required: true |
| 77 |
destination_datastore: |
| 78 |
description: |
| 79 |
- The name of the destination datastore or the datastore cluster. |
| 80 |
- If datastore cluster name is specified, we will find the Storage DRS recommended datastore in that cluster. |
| 81 |
type: str |
| 82 |
required: true |
| 83 |
destination_vm_folder: |
| 84 |
description: |
| 85 |
- Destination folder, absolute path to deploy the cloned vm. |
| 86 |
- 'Examples:' |
| 87 |
- ' folder: vm' |
| 88 |
- ' folder: ha-datacenter/vm' |
| 89 |
- ' folder: /datacenter1/vm' |
| 90 |
type: str |
| 91 |
required: true |
| 92 |
destination_resource_pool: |
| 93 |
description: |
| 94 |
- Destination resource pool. |
| 95 |
- If not provided, the destination host's parent's resource pool will be used. |
| 96 |
type: str |
| 97 |
is_template: |
| 98 |
description: |
| 99 |
- Specifies whether or not the new virtual machine should be marked as a template. |
| 100 |
type: bool |
| 101 |
default: false |
| 102 |
state: |
| 103 |
description: |
| 104 |
- The state of Virtual Machine deployed. |
| 105 |
- If set to V(present) and VM does not exists, then VM is created. |
| 106 |
- If set to V(present) and VM exists, no action is taken. |
| 107 |
- If set to V(poweredon) and VM does not exists, then VM is created with powered on state. |
| 108 |
- If set to V(poweredon) and VM exists, no action is taken. |
| 109 |
type: str |
| 110 |
required: false |
| 111 |
default: 'present' |
| 112 |
choices: [ 'present', 'poweredon' ] |
| 113 |
timeout: |
| 114 |
description: |
| 115 |
- The timeout in seconds. When the timeout is reached, the module will fail. |
| 116 |
type: int |
| 117 |
default: 3600 |
| 118 |
version_added: '3.5.0' |
| 119 |
extends_documentation_fragment: |
| 120 |
- vmware.vmware.base_options |
| 121 |
|
| 122 |
|
| 123 |
author: |
| 124 |
- Anusha Hegde (@anusha94) |
| 125 |
''' |
| 126 |
|
| 127 |
EXAMPLES = r''' |
| 128 |
# Clone template |
| 129 |
- name: clone a template across VC |
| 130 |
community.vmware.vmware_guest_cross_vc_clone: |
| 131 |
hostname: '{{ vcenter_hostname }}' |
| 132 |
username: '{{ vcenter_username }}' |
| 133 |
password: '{{ vcenter_password }}' |
| 134 |
name: "test_vm1" |
| 135 |
destination_vm_name: "cloned_vm_from_template" |
| 136 |
destination_vcenter: '{{ destination_vcenter_hostname }}' |
| 137 |
destination_vcenter_username: '{{ destination_vcenter_username }}' |
| 138 |
destination_vcenter_password: '{{ destination_vcenter_password }}' |
| 139 |
destination_vcenter_port: '{{ destination_vcenter_port }}' |
| 140 |
destination_vcenter_validate_certs: '{{ destination_vcenter_validate_certs }}' |
| 141 |
destination_host: '{{ destination_esxi }}' |
| 142 |
destination_datastore: '{{ destination_datastore }}' |
| 143 |
destination_vm_folder: '{{ destination_vm_folder }}' |
| 144 |
state: present |
| 145 |
register: cross_vc_clone_from_template |
| 146 |
|
| 147 |
- name: clone a VM across VC |
| 148 |
community.vmware.vmware_guest_cross_vc_clone: |
| 149 |
hostname: '{{ vcenter_hostname }}' |
| 150 |
username: '{{ vcenter_username }}' |
| 151 |
password: "{{ vcenter_password }}" |
| 152 |
name: "test_vm1" |
| 153 |
destination_vm_name: "cloned_vm_from_vm" |
| 154 |
destination_vcenter: '{{ destination_vcenter_hostname }}' |
| 155 |
destination_vcenter_username: '{{ destination_vcenter_username }}' |
| 156 |
destination_vcenter_password: '{{ destination_vcenter_password }}' |
| 157 |
destination_host: '{{ destination_esxi }}' |
| 158 |
destination_datastore: '{{ destination_datastore }}' |
| 159 |
destination_vm_folder: '{{ destination_vm_folder }}' |
| 160 |
state: poweredon |
| 161 |
register: cross_vc_clone_from_vm |
| 162 |
|
| 163 |
- name: check_mode support |
| 164 |
community.vmware.vmware_guest_cross_vc_clone: |
| 165 |
hostname: '{{ vcenter_hostname }}' |
| 166 |
username: '{{ vcenter_username }}' |
| 167 |
password: "{{ vcenter_password }}" |
| 168 |
name: "test_vm1" |
| 169 |
destination_vm_name: "cloned_vm_from_vm" |
| 170 |
destination_vcenter: '{{ destination_vcenter_hostname }}' |
| 171 |
destination_vcenter_username: '{{ destination_vcenter_username }}' |
| 172 |
destination_vcenter_password: '{{ destination_vcenter_password }}' |
| 173 |
destination_host: '{{ destination_esxi }}' |
| 174 |
destination_datastore: '{{ destination_datastore }}' |
| 175 |
destination_vm_folder: '{{ destination_vm_folder }}' |
| 176 |
check_mode: true |
| 177 |
''' |
| 178 |
|
| 179 |
RETURN = r''' |
| 180 |
vm_info: |
| 181 |
description: metadata about the virtual machine |
| 182 |
returned: always |
| 183 |
type: dict |
| 184 |
sample: { |
| 185 |
"vm_name": "", |
| 186 |
"vcenter": "", |
| 187 |
"host": "", |
| 188 |
"datastore": "", |
| 189 |
"vm_folder": "", |
| 190 |
"power_on": "" |
| 191 |
} |
| 192 |
''' |
| 193 |
|
| 194 |
from ansible.module_utils.basic import AnsibleModule |
| 195 |
from ansible_collections.community.vmware.plugins.module_utils.vmware import ( |
| 196 |
PyVmomi, |
| 197 |
find_hostsystem_by_name, |
| 198 |
find_datastore_by_name, |
| 199 |
find_folder_by_name, |
| 200 |
find_vm_by_name, |
| 201 |
gather_vm_facts, |
| 202 |
find_obj, |
| 203 |
find_resource_pool_by_name, |
| 204 |
wait_for_task, |
| 205 |
) |
| 206 |
from ansible_collections.vmware.vmware.plugins.module_utils.argument_spec import base_argument_spec |
| 207 |
from ansible_collections.vmware.vmware.plugins.module_utils.clients.pyvmomi import PyvmomiClient |
| 208 |
try: |
| 209 |
from pyVmomi import vim |
| 210 |
except ImportError: |
| 211 |
pass |
| 212 |
|
| 213 |
|
| 214 |
class CrossVCCloneManager(PyVmomi): |
| 215 |
def __init__(self, module): |
| 216 |
super(CrossVCCloneManager, self).__init__(module) |
| 217 |
self.config_spec = vim.vm.ConfigSpec() |
| 218 |
self.clone_spec = vim.vm.CloneSpec() |
| 219 |
self.relocate_spec = vim.vm.RelocateSpec() |
| 220 |
self.service_locator = vim.ServiceLocator() |
| 221 |
self.destination_vcenter = self.params['destination_vcenter'] |
| 222 |
self.destination_vcenter_username = self.params['destination_vcenter_username'] |
| 223 |
self.destination_vcenter_password = self.params['destination_vcenter_password'] |
| 224 |
self.destination_vcenter_port = self.params.get('port', 443) |
| 225 |
self.destination_vcenter_validate_certs = self.params.get('destination_vcenter_validate_certs', None) |
| 226 |
self.timeout = self.params.get('timeout') |
| 227 |
|
| 228 |
def get_new_vm_info(self, vm): |
| 229 |
# to check if vm has been cloned in the destination vc |
| 230 |
# connect to destination VC |
| 231 |
# query for the vm in destination vc |
| 232 |
# get the host and datastore info |
| 233 |
# get the power status of the newly cloned vm |
| 234 |
pyvmomi_client = PyvmomiClient( |
| 235 |
hostname=self.destination_vcenter, |
| 236 |
username=self.destination_vcenter_username, |
| 237 |
password=self.destination_vcenter_password, |
| 238 |
port=self.destination_vcenter_port, |
| 239 |
validate_certs=self.destination_vcenter_validate_certs) |
| 240 |
self.destination_content = pyvmomi_client.content |
| 241 |
info = {} |
| 242 |
vm_obj = find_vm_by_name(content=self.destination_content, vm_name=vm) |
| 243 |
if vm_obj is None: |
| 244 |
self.module.fail_json(msg="Newly cloned VM is not found in the destination VCenter") |
| 245 |
else: |
| 246 |
vm_facts = gather_vm_facts(self.destination_content, vm_obj) |
| 247 |
info['vm_name'] = vm |
| 248 |
info['vcenter'] = self.destination_vcenter |
| 249 |
info['host'] = vm_facts['hw_esxi_host'] |
| 250 |
info['datastore'] = vm_facts['hw_datastores'] |
| 251 |
info['vm_folder'] = vm_facts['hw_folder'] |
| 252 |
info['power_on'] = vm_facts['hw_power_status'] |
| 253 |
return info |
| 254 |
|
| 255 |
def clone(self): |
| 256 |
# clone the vm/template on destination VC |
| 257 |
vm_folder = find_folder_by_name(content=self.destination_content, folder_name=self.params['destination_vm_folder']) |
| 258 |
if not vm_folder: |
| 259 |
self.module.fail_json(msg="Destination folder does not exist. Please refer to the documentation to correctly specify the folder.") |
| 260 |
vm_name = self.params['destination_vm_name'] |
| 261 |
task = self.vm_obj.Clone(folder=vm_folder, name=vm_name, spec=self.clone_spec) |
| 262 |
wait_for_task(task, timeout=self.timeout) |
| 263 |
if task.info.state == 'error': |
| 264 |
result = {'changed': False, 'failed': True, 'msg': task.info.error.msg} |
| 265 |
else: |
| 266 |
vm_info = self.get_new_vm_info(vm_name) |
| 267 |
result = {'changed': True, 'failed': False, 'vm_info': vm_info} |
| 268 |
return result |
| 269 |
|
| 270 |
def sanitize_params(self): |
| 271 |
''' |
| 272 |
this method is used to verify user provided parameters |
| 273 |
''' |
| 274 |
self.vm_obj = self.get_vm() |
| 275 |
if self.vm_obj is None: |
| 276 |
vm_id = self.vm_uuid or self.vm_name or self.moid |
| 277 |
self.module.fail_json(msg="Failed to find the VM/template with %s" % vm_id) |
| 278 |
|
| 279 |
# connect to destination VC |
| 280 |
pyvmomi_client = PyvmomiClient( |
| 281 |
hostname=self.destination_vcenter, |
| 282 |
username=self.destination_vcenter_username, |
| 283 |
password=self.destination_vcenter_password, |
| 284 |
port=self.destination_vcenter_port, |
| 285 |
validate_certs=self.destination_vcenter_validate_certs) |
| 286 |
self.destination_content = pyvmomi_client.content |
| 287 |
|
| 288 |
# Check if vm name already exists in the destination VC |
| 289 |
vm = find_vm_by_name(content=self.destination_content, vm_name=self.params['destination_vm_name']) |
| 290 |
if vm: |
| 291 |
self.module.exit_json(changed=False, msg="A VM with the given name already exists") |
| 292 |
|
| 293 |
datastore_name = self.params['destination_datastore'] |
| 294 |
datastore_cluster = find_obj(self.destination_content, [vim.StoragePod], datastore_name) |
| 295 |
if datastore_cluster: |
| 296 |
# If user specified datastore cluster so get recommended datastore |
| 297 |
datastore_name = self.get_recommended_datastore(datastore_cluster_obj=datastore_cluster) |
| 298 |
# Check if get_recommended_datastore or user specified datastore exists or not |
| 299 |
self.destination_datastore = find_datastore_by_name(content=self.destination_content, datastore_name=datastore_name) |
| 300 |
if self.destination_datastore is None: |
| 301 |
self.module.fail_json(msg="Destination datastore not found.") |
| 302 |
|
| 303 |
self.destination_host = find_hostsystem_by_name(content=self.destination_content, hostname=self.params['destination_host']) |
| 304 |
if self.destination_host is None: |
| 305 |
self.module.fail_json(msg="Destination host not found.") |
| 306 |
|
| 307 |
if self.params['destination_resource_pool']: |
| 308 |
self.destination_resource_pool = find_resource_pool_by_name( |
| 309 |
content=self.destination_content, |
| 310 |
resource_pool_name=self.params['destination_resource_pool']) |
| 311 |
else: |
| 312 |
self.destination_resource_pool = self.destination_host.parent.resourcePool |
| 313 |
|
| 314 |
def populate_specs(self): |
| 315 |
# populate service locator |
| 316 |
self.service_locator.instanceUuid = self.destination_content.about.instanceUuid |
| 317 |
self.service_locator.url = "https://" + self.destination_vcenter + ":" + str(self.params['port']) + "/sdk" |
| 318 |
# If ssl verify is false, we ignore it also in the clone task by fetching thumbprint automatically |
| 319 |
if not self.destination_vcenter_validate_certs: |
| 320 |
self.service_locator.sslThumbprint = self.get_cert_fingerprint( |
| 321 |
self.destination_vcenter, |
| 322 |
self.destination_vcenter_port, |
| 323 |
self.module.params['proxy_host'], |
| 324 |
self.module.params['proxy_port']) |
| 325 |
creds = vim.ServiceLocatorNamePassword() |
| 326 |
creds.username = self.destination_vcenter_username |
| 327 |
creds.password = self.destination_vcenter_password |
| 328 |
self.service_locator.credential = creds |
| 329 |
|
| 330 |
# populate relocate spec |
| 331 |
self.relocate_spec.datastore = self.destination_datastore |
| 332 |
self.relocate_spec.pool = self.destination_resource_pool |
| 333 |
self.relocate_spec.service = self.service_locator |
| 334 |
self.relocate_spec.host = self.destination_host |
| 335 |
|
| 336 |
# populate clone spec |
| 337 |
self.clone_spec.config = self.config_spec |
| 338 |
self.clone_spec.powerOn = True if self.params['state'].lower() == 'poweredon' else False |
| 339 |
self.clone_spec.location = self.relocate_spec |
| 340 |
self.clone_spec.template = self.params['is_template'] |
| 341 |
|
| 342 |
|
| 343 |
def main(): |
| 344 |
""" |
| 345 |
Main method |
| 346 |
""" |
| 347 |
argument_spec = base_argument_spec() |
| 348 |
argument_spec.update( |
| 349 |
name=dict(type='str'), |
| 350 |
uuid=dict(type='str'), |
| 351 |
moid=dict(type='str'), |
| 352 |
use_instance_uuid=dict(type='bool', default=False), |
| 353 |
destination_vm_name=dict(type='str', required=True), |
| 354 |
destination_datastore=dict(type='str', required=True), |
| 355 |
destination_host=dict(type='str', required=True), |
| 356 |
destination_vcenter=dict(type='str', required=True), |
| 357 |
destination_vcenter_username=dict(type='str', required=True), |
| 358 |
destination_vcenter_password=dict(type='str', required=True, no_log=True), |
| 359 |
destination_vcenter_port=dict(type='int', default=443), |
| 360 |
destination_vcenter_validate_certs=dict(type='bool', default=False), |
| 361 |
destination_vm_folder=dict(type='str', required=True), |
| 362 |
destination_resource_pool=dict(type='str', default=None), |
| 363 |
is_template=dict(type='bool', default=False), |
| 364 |
state=dict(type='str', default='present', |
| 365 |
choices=['present', 'poweredon']), |
| 366 |
timeout=dict(type='int', default=3600) |
| 367 |
) |
| 368 |
|
| 369 |
module = AnsibleModule( |
| 370 |
argument_spec=argument_spec, |
| 371 |
supports_check_mode=True, |
| 372 |
required_one_of=[ |
| 373 |
['uuid', 'name', 'moid'], |
| 374 |
], |
| 375 |
mutually_exclusive=[ |
| 376 |
['uuid', 'name', 'moid'], |
| 377 |
], |
| 378 |
) |
| 379 |
result = {'failed': False, 'changed': False} |
| 380 |
if module.check_mode: |
| 381 |
if module.params['state'] in ['present']: |
| 382 |
result.update( |
| 383 |
vm_name=module.params['destination_vm_name'], |
| 384 |
vcenter=module.params['destination_vcenter'], |
| 385 |
host=module.params['destination_host'], |
| 386 |
datastore=module.params['destination_datastore'], |
| 387 |
vm_folder=module.params['destination_vm_folder'], |
| 388 |
state=module.params['state'], |
| 389 |
changed=True, |
| 390 |
desired_operation='Create VM with PowerOff State' |
| 391 |
) |
| 392 |
if module.params['state'] == 'poweredon': |
| 393 |
result.update( |
| 394 |
vm_name=module.params['destination_vm_name'], |
| 395 |
vcenter=module.params['destination_vcenter'], |
| 396 |
host=module.params['destination_host'], |
| 397 |
datastore=module.params['destination_datastore'], |
| 398 |
vm_folder=module.params['destination_vm_folder'], |
| 399 |
state=module.params['state'], |
| 400 |
changed=True, |
| 401 |
desired_operation='Create VM with PowerON State' |
| 402 |
) |
| 403 |
module.exit_json(**result) |
| 404 |
|
| 405 |
clone_manager = CrossVCCloneManager(module) |
| 406 |
clone_manager.sanitize_params() |
| 407 |
clone_manager.populate_specs() |
| 408 |
result = clone_manager.clone() |
| 409 |
|
| 410 |
if result['failed']: |
| 411 |
module.fail_json(**result) |
| 412 |
else: |
| 413 |
module.exit_json(**result) |
| 414 |
|
| 415 |
|
| 416 |
if __name__ == '__main__': |
| 417 |
main() |