|
|
|
@ -47,7 +47,7 @@ options:
|
|
|
|
|
name:
|
|
|
|
|
description:
|
|
|
|
|
- Name of the VM to work with
|
|
|
|
|
required: True
|
|
|
|
|
- This is required if uuid is not supplied.
|
|
|
|
|
name_match:
|
|
|
|
|
description:
|
|
|
|
|
- If multiple VMs matching the name, use the first or last found
|
|
|
|
@ -59,7 +59,22 @@ options:
|
|
|
|
|
- This is required if name is not supplied.
|
|
|
|
|
folder:
|
|
|
|
|
description:
|
|
|
|
|
- Define instance folder location.
|
|
|
|
|
- Destination folder, absolute or relative path to find an existing guest.
|
|
|
|
|
- This is required if name is supplied.
|
|
|
|
|
- The folder should include the datacenter. ESX's datacenter is ha-datacenter
|
|
|
|
|
- 'Examples:'
|
|
|
|
|
- ' folder: /ha-datacenter/vm'
|
|
|
|
|
- ' folder: ha-datacenter/vm'
|
|
|
|
|
- ' folder: /datacenter1/vm'
|
|
|
|
|
- ' folder: datacenter1/vm'
|
|
|
|
|
- ' folder: /datacenter1/vm/folder1'
|
|
|
|
|
- ' folder: datacenter1/vm/folder1'
|
|
|
|
|
- ' folder: /folder1/datacenter1/vm'
|
|
|
|
|
- ' folder: folder1/datacenter1/vm'
|
|
|
|
|
- ' folder: /folder1/datacenter1/vm/folder2'
|
|
|
|
|
- ' folder: vm/folder2'
|
|
|
|
|
- ' folder: folder2'
|
|
|
|
|
default: /vm
|
|
|
|
|
datacenter:
|
|
|
|
|
description:
|
|
|
|
|
- Destination datacenter for the deploy operation
|
|
|
|
@ -172,11 +187,10 @@ instance:
|
|
|
|
|
sample: None
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import time
|
|
|
|
|
from ansible.module_utils._text import to_native
|
|
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
|
|
|
|
from ansible.module_utils.vmware import connect_to_api, vmware_argument_spec
|
|
|
|
|
from ansible.module_utils.vmware import connect_to_api, vmware_argument_spec, find_vm_by_id
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
import json
|
|
|
|
@ -200,38 +214,17 @@ class PyVmomiHelper(object):
|
|
|
|
|
|
|
|
|
|
self.module = module
|
|
|
|
|
self.params = module.params
|
|
|
|
|
self.si = None
|
|
|
|
|
self.content = connect_to_api(self.module)
|
|
|
|
|
self.change_detected = False
|
|
|
|
|
|
|
|
|
|
def getvm(self, name=None, uuid=None, folder=None):
|
|
|
|
|
|
|
|
|
|
# https://www.vmware.com/support/developer/vc-sdk/visdk2xpubs/ReferenceGuide/vim.SearchIndex.html
|
|
|
|
|
# self.si.content.searchIndex.FindByInventoryPath('DC1/vm/test_folder')
|
|
|
|
|
|
|
|
|
|
vm = None
|
|
|
|
|
|
|
|
|
|
match_first = False
|
|
|
|
|
if uuid:
|
|
|
|
|
vm = self.content.searchIndex.FindByUuid(uuid=uuid, vmSearch=True)
|
|
|
|
|
elif folder:
|
|
|
|
|
# Build the absolute folder path to pass into the search method
|
|
|
|
|
if not self.params['folder'].startswith('/'):
|
|
|
|
|
self.module.fail_json(msg="Folder %(folder)s needs to be an absolute path, starting with '/'." % self.params)
|
|
|
|
|
searchpath = '%(datacenter)s%(folder)s' % self.params
|
|
|
|
|
|
|
|
|
|
# get all objects for this path ...
|
|
|
|
|
f_obj = self.content.searchIndex.FindByInventoryPath(searchpath)
|
|
|
|
|
if f_obj:
|
|
|
|
|
if isinstance(f_obj, vim.Datacenter):
|
|
|
|
|
f_obj = f_obj.vmFolder
|
|
|
|
|
for c_obj in f_obj.childEntity:
|
|
|
|
|
if not isinstance(c_obj, vim.VirtualMachine):
|
|
|
|
|
continue
|
|
|
|
|
if c_obj.name == name:
|
|
|
|
|
vm = c_obj
|
|
|
|
|
if self.params['name_match'] == 'first':
|
|
|
|
|
break
|
|
|
|
|
|
|
|
|
|
vm = find_vm_by_id(self.content, uuid, vm_id_type="uuid")
|
|
|
|
|
elif folder and name:
|
|
|
|
|
if self.params['name_match'] == 'first':
|
|
|
|
|
match_first = True
|
|
|
|
|
vm = find_vm_by_id(self.content, vm_id=name, vm_id_type="inventory_path", folder=folder, match_first=match_first)
|
|
|
|
|
return vm
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
@ -323,7 +316,7 @@ def main():
|
|
|
|
|
argument_spec.update(
|
|
|
|
|
state=dict(default='present', choices=['present', 'absent', 'revert', 'remove_all']),
|
|
|
|
|
name=dict(required=True, type='str'),
|
|
|
|
|
name_match=dict(type='str', default='first'),
|
|
|
|
|
name_match=dict(type='str', choices=['first', 'last'], default='first'),
|
|
|
|
|
uuid=dict(type='str'),
|
|
|
|
|
folder=dict(type='str', default='/vm'),
|
|
|
|
|
datacenter=dict(required=True, type='str'),
|
|
|
|
@ -333,11 +326,10 @@ def main():
|
|
|
|
|
memory_dump=dict(type='bool', default=False),
|
|
|
|
|
remove_children=dict(type='bool', default=False),
|
|
|
|
|
)
|
|
|
|
|
module = AnsibleModule(argument_spec=argument_spec)
|
|
|
|
|
module = AnsibleModule(argument_spec=argument_spec, required_one_of=[['name', 'uuid']])
|
|
|
|
|
|
|
|
|
|
# Prepend /vm if it was missing from the folder path, also strip trailing slashes
|
|
|
|
|
if not module.params['folder'].startswith('/vm') and module.params['folder'].startswith('/'):
|
|
|
|
|
module.params['folder'] = '/vm%(folder)s' % module.params
|
|
|
|
|
# FindByInventoryPath() does not require an absolute path
|
|
|
|
|
# so we should leave the input folder path unmodified
|
|
|
|
|
module.params['folder'] = module.params['folder'].rstrip('/')
|
|
|
|
|
|
|
|
|
|
pyv = PyVmomiHelper(module)
|
|
|
|
|