Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VMware: Add support in VMWare modules for BIOS and instance UUID's #44399

Merged
merged 14 commits into from
Feb 25, 2019
Merged
12 changes: 9 additions & 3 deletions lib/ansible/module_utils/vmware.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,8 @@ def find_network_by_name(content, network_name):
return find_object_by_name(content, network_name, [vim.Network])


def find_vm_by_id(content, vm_id, vm_id_type="vm_name", datacenter=None, cluster=None, folder=None, match_first=False):
def find_vm_by_id(content, vm_id, vm_id_type="vm_name", datacenter=None,
cluster=None, folder=None, match_first=False):
""" UUID is unique to a VM, every other id returns the first match. """
si = content.searchIndex
vm = None
Expand All @@ -184,6 +185,8 @@ def find_vm_by_id(content, vm_id, vm_id_type="vm_name", datacenter=None, cluster
elif vm_id_type == 'uuid':
# Search By BIOS UUID rather than instance UUID
vm = si.FindByUuid(datacenter=datacenter, instanceUuid=False, uuid=vm_id, vmSearch=True)
elif vm_id_type == 'instance_uuid':
vm = si.FindByUuid(datacenter=datacenter, instanceUuid=True, uuid=vm_id, vmSearch=True)
elif vm_id_type == 'ip':
vm = si.FindByIp(datacenter=datacenter, ip=vm_id, vmSearch=True)
elif vm_id_type == 'vm_name':
Expand Down Expand Up @@ -861,9 +864,12 @@ def get_vm(self):
vm_obj = None
user_desired_path = None

if self.params['uuid']:
if self.params['uuid'] and not self.params['use_instance_uuid']:
vm_obj = find_vm_by_id(self.content, vm_id=self.params['uuid'], vm_id_type="uuid")

elif self.params['uuid'] and self.params['use_instance_uuid']:
vm_obj = find_vm_by_id(self.content,
vm_id=self.params['uuid'],
vm_id_type="instance_uuid")
elif self.params['name']:
objects = self.get_managed_objects_properties(vim_type=vim.VirtualMachine, properties=['name'])
vms = []
Expand Down
7 changes: 7 additions & 0 deletions lib/ansible/modules/cloud/vmware/vmware_guest.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@
- This is required if C(name) is not supplied.
- If virtual machine does not exists, then this parameter is ignored.
- Please note that a supplied UUID will be ignored on virtual machine creation, as VMware creates the UUID internally.
use_instance_uuid:
description:
- Whether to use the VMWare instance UUID rather than the BIOS UUID.
default: no
type: bool
version_added: '2.8'
template:
description:
- Template or existing virtual machine used to create new virtual machine.
Expand Down Expand Up @@ -2434,6 +2440,7 @@ def main():
name=dict(type='str'),
name_match=dict(type='str', choices=['first', 'last'], default='first'),
uuid=dict(type='str'),
use_instance_uuid=dict(type='bool', default=False),
folder=dict(type='str'),
guest_id=dict(type='str'),
disk=dict(type='list', default=[]),
Expand Down
15 changes: 13 additions & 2 deletions lib/ansible/modules/cloud/vmware/vmware_guest_boot_facts.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,14 @@
- This is required if C(uuid) parameter is not supplied.
uuid:
description:
- UUID of the instance to manage if known, this is VMware's BIOS UUID.
- UUID of the instance to manage if known, this is VMware's BIOS UUID by default.
- This is required if C(name) parameter is not supplied.
use_instance_uuid:
description:
- Whether to use the VMWare instance UUID rather than the BIOS UUID.
default: no
type: bool
version_added: '2.8'
name_match:
description:
- If multiple virtual machines matching the name, use the first or last found.
Expand Down Expand Up @@ -93,13 +99,17 @@ def __init__(self, module):
super(VmBootFactsManager, self).__init__(module)
self.name = self.params['name']
self.uuid = self.params['uuid']
self.use_instance_uuid = self.params['use_instance_uuid']
self.vm = None

def _get_vm(self):
vms = []

if self.uuid:
vm_obj = find_vm_by_id(self.content, vm_id=self.uuid, vm_id_type="uuid")
if self.use_instance_uuid:
vm_obj = find_vm_by_id(self.content, vm_id=self.uuid, vm_id_type="use_instance_uuid")
else:
vm_obj = find_vm_by_id(self.content, vm_id=self.uuid, vm_id_type="uuid")
if vm_obj is None:
self.module.fail_json(msg="Failed to find the virtual machine with UUID : %s" % self.uuid)
vms = [vm_obj]
Expand Down Expand Up @@ -155,6 +165,7 @@ def main():
argument_spec.update(
name=dict(type='str'),
uuid=dict(type='str'),
use_instance_uuid=dict(type='bool', default=False),
name_match=dict(
choices=['first', 'last'],
default='first'
Expand Down
15 changes: 13 additions & 2 deletions lib/ansible/modules/cloud/vmware/vmware_guest_boot_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,14 @@
- This is required if C(uuid) parameter is not supplied.
uuid:
description:
- UUID of the instance to manage if known, this is VMware's BIOS UUID.
- UUID of the instance to manage if known, this is VMware's BIOS UUID by default.
- This is required if C(name) parameter is not supplied.
use_instance_uuid:
description:
- Whether to use the VMWare instance UUID rather than the BIOS UUID.
default: no
type: bool
version_added: '2.8'
boot_order:
description:
- List of the boot devices.
Expand Down Expand Up @@ -152,13 +158,17 @@ def __init__(self, module):
super(VmBootManager, self).__init__(module)
self.name = self.params['name']
self.uuid = self.params['uuid']
self.use_instance_uuid = self.params['use_instance_uuid']
self.vm = None

def _get_vm(self):
vms = []

if self.uuid:
vm_obj = find_vm_by_id(self.content, vm_id=self.uuid, vm_id_type="uuid")
if self.use_instance_uuid:
vm_obj = find_vm_by_id(self.content, vm_id=self.uuid, vm_id_type="instance_uuid")
else:
vm_obj = find_vm_by_id(self.content, vm_id=self.uuid, vm_id_type="uuid")
if vm_obj is None:
self.module.fail_json(msg="Failed to find the virtual machine with UUID : %s" % self.uuid)
vms = [vm_obj]
Expand Down Expand Up @@ -314,6 +324,7 @@ def main():
argument_spec.update(
name=dict(type='str'),
uuid=dict(type='str'),
use_instance_uuid=dict(type='bool', default=False),
boot_order=dict(
type='list',
default=[],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@
description:
- UUID of the virtual machine to manage if known. This is VMware's unique identifier.
naphta marked this conversation as resolved.
Show resolved Hide resolved
- This is required parameter, if C(name) is not supplied.
use_instance_uuid:
description:
- Whether to use the VMWare instance UUID rather than the BIOS UUID.
default: no
type: bool
version_added: '2.8'
folder:
description:
- Absolute path to find an existing guest.
Expand Down Expand Up @@ -180,6 +186,7 @@ def main():
name=dict(required=True, type='str'),
folder=dict(type='str'),
uuid=dict(type='str'),
use_instance_uuid=dict(type='bool', default=False),
state=dict(type='str', default='present',
choices=['absent', 'present']),
attributes=dict(
Expand Down
7 changes: 7 additions & 0 deletions lib/ansible/modules/cloud/vmware/vmware_guest_disk_facts.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@
description:
- UUID of the instance to gather facts if known, this is VMware's unique identifier.
naphta marked this conversation as resolved.
Show resolved Hide resolved
- This is required parameter, if parameter C(name) is not supplied.
use_instance_uuid:
description:
- Whether to use the VMWare instance UUID rather than the BIOS UUID.
default: no
type: bool
version_added: '2.8'
folder:
description:
- Destination folder, absolute or relative path to find an existing guest.
Expand Down Expand Up @@ -166,6 +172,7 @@ def main():
argument_spec.update(
name=dict(type='str'),
uuid=dict(type='str'),
use_instance_uuid=dict(type='bool', default=False),
folder=dict(type='str'),
datacenter=dict(type='str', required=True),
)
Expand Down
8 changes: 8 additions & 0 deletions lib/ansible/modules/cloud/vmware/vmware_guest_facts.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@
description:
- UUID of the instance to manage if known, this is VMware's unique identifier.
- This is required if name is not supplied.
use_instance_uuid:
description:
- Whether to use the VMWare instance UUID rather than the BIOS UUID.
default: no
type: bool
version_added: '2.8'
folder:
description:
- Destination folder, absolute or relative path to find an existing guest.
Expand Down Expand Up @@ -155,6 +161,7 @@
from ansible.module_utils.vmware import PyVmomi, vmware_argument_spec
from ansible.module_utils.vmware_rest_client import VmwareRestClient
try:
from com.vmware.vapi.std_client import DynamicID
from com.vmware.cis.tagging_client import Tag, TagAssociation
HAS_VCLOUD = True
except ImportError:
Expand All @@ -179,6 +186,7 @@ def main():
name=dict(type='str'),
name_match=dict(type='str', choices=['first', 'last'], default='first'),
uuid=dict(type='str'),
use_instance_uuid=dict(type='bool', default=False),
folder=dict(type='str'),
datacenter=dict(type='str', required=True),
tags=dict(type='bool', default=False)
Expand Down
10 changes: 7 additions & 3 deletions lib/ansible/modules/cloud/vmware/vmware_guest_file_operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
default: vm_name
choices:
- 'uuid'
- 'instance_uuid'
- 'dns_name'
- 'inventory_path'
- 'vm_name'
Expand Down Expand Up @@ -194,8 +195,11 @@ def __init__(self, module):
if module.params['vm_id_type'] == 'inventory_path':
vm = find_vm_by_id(self.content, vm_id=module.params['vm_id'], vm_id_type="inventory_path", folder=folder)
else:
vm = find_vm_by_id(self.content, vm_id=module.params['vm_id'], vm_id_type=module.params['vm_id_type'],
datacenter=datacenter, cluster=cluster)
vm = find_vm_by_id(self.content,
vm_id=module.params['vm_id'],
vm_id_type=module.params['vm_id_type'],
datacenter=datacenter,
cluster=cluster)

if not vm:
module.fail_json(msg='Unable to find virtual machine.')
Expand Down Expand Up @@ -391,7 +395,7 @@ def main():
vm_id_type=dict(
default='vm_name',
type='str',
choices=['inventory_path', 'uuid', 'dns_name', 'vm_name']),
choices=['inventory_path', 'uuid', 'instance_uuid', 'dns_name', 'vm_name']),
vm_username=dict(type='str', required=True),
vm_password=dict(type='str', no_log=True, required=True),
directory=dict(
Expand Down
15 changes: 13 additions & 2 deletions lib/ansible/modules/cloud/vmware/vmware_guest_find.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,14 @@
- This is required if C(uuid) parameter is not supplied.
uuid:
description:
- UUID of the instance to manage if known, this is VMware's BIOS UUID.
- UUID of the instance to manage if known, this is VMware's BIOS UUID by default.
- This is required if C(name) parameter is not supplied.
use_instance_uuid:
description:
- Whether to use the VMWare instance UUID rather than the BIOS UUID.
default: no
type: bool
version_added: '2.8'
datacenter:
description:
- Destination datacenter for the find operation.
Expand Down Expand Up @@ -90,13 +96,17 @@ def __init__(self, module):
super(PyVmomiHelper, self).__init__(module)
self.name = self.params['name']
self.uuid = self.params['uuid']
self.use_instance_uuid = self.params['use_instance_uuid']

def getvm_folder_paths(self):
results = []
vms = []

if self.uuid:
vm_obj = find_vm_by_id(self.content, vm_id=self.uuid, vm_id_type="uuid")
if self.use_instance_uuid:
vm_obj = find_vm_by_id(self.content, vm_id=self.uuid, vm_id_type="instance_uuid")
else:
vm_obj = find_vm_by_id(self.content, vm_id=self.uuid, vm_id_type="uuid")
if vm_obj is None:
self.module.fail_json(msg="Failed to find the virtual machine with UUID : %s" % self.uuid)
vms = [vm_obj]
Expand All @@ -119,6 +129,7 @@ def main():
argument_spec.update(
name=dict(type='str'),
uuid=dict(type='str'),
use_instance_uuid=dict(type='bool', default=False),
datacenter=dict(removed_in_version=2.9, type='str')
)

Expand Down
7 changes: 7 additions & 0 deletions lib/ansible/modules/cloud/vmware/vmware_guest_move.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@
description:
- UUID of the virtual machine to manage if known, this is VMware's unique identifier.
naphta marked this conversation as resolved.
Show resolved Hide resolved
- This is required if C(name) is not supplied.
use_instance_uuid:
description:
- Whether to use the VMWare instance UUID rather than the BIOS UUID.
default: no
type: bool
version_added: '2.8'
name_match:
description:
- If multiple virtual machines matching the name, use the first or last found.
Expand Down Expand Up @@ -169,6 +175,7 @@ def main():
name_match=dict(
type='str', choices=['first', 'last'], default='first'),
uuid=dict(type='str'),
use_instance_uuid=dict(type='bool', default=False),
dest_folder=dict(type='str', required=True),
datacenter=dict(type='str', required=True),
)
Expand Down
7 changes: 7 additions & 0 deletions lib/ansible/modules/cloud/vmware/vmware_guest_powerstate.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@
description:
- UUID of the instance to manage if known, this is VMware's unique identifier.
naphta marked this conversation as resolved.
Show resolved Hide resolved
- This is required if name is not supplied.
use_instance_uuid:
description:
- Whether to use the VMWare instance UUID rather than the BIOS UUID.
default: no
type: bool
version_added: '2.8'
folder:
description:
- Destination folder, absolute or relative path to find an existing guest or create the new guest.
Expand Down Expand Up @@ -139,6 +145,7 @@ def main():
name=dict(type='str'),
name_match=dict(type='str', choices=['first', 'last'], default='first'),
uuid=dict(type='str'),
use_instance_uuid=dict(type='bool', default=False),
folder=dict(type='str', default='/vm'),
force=dict(type='bool', default=False),
scheduled_at=dict(type='str'),
Expand Down
11 changes: 9 additions & 2 deletions lib/ansible/modules/cloud/vmware/vmware_guest_snapshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,14 @@
choices: ['first', 'last']
uuid:
description:
- UUID of the instance to manage if known, this is VMware's unique identifier.
- This is required parameter, if C(name) is not supplied.
- UUID of the instance to manage if known, this is VMware's BIOS UUID by default.
- This is required if C(name) parameter is not supplied.
use_instance_uuid:
description:
- Whether to use the VMWare instance UUID rather than the BIOS UUID.
default: no
type: bool
version_added: '2.8'
folder:
description:
- Destination folder, absolute or relative path to find an existing guest.
Expand Down Expand Up @@ -367,6 +373,7 @@ def main():
name=dict(type='str'),
name_match=dict(type='str', choices=['first', 'last'], default='first'),
uuid=dict(type='str'),
use_instance_uuid=dict(type='bool', default=False),
folder=dict(type='str'),
datacenter=dict(required=True, type='str'),
snapshot_name=dict(type='str'),
Expand Down
11 changes: 9 additions & 2 deletions lib/ansible/modules/cloud/vmware/vmware_guest_snapshot_facts.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,15 @@
- This is required if C(uuid) is not supplied.
uuid:
description:
- UUID of the instance to manage if known, this value is VMware's unique identifier.
- This is required if C(name) is not supplied.
- UUID of the instance to manage if known, this is VMware's BIOS UUID by default.
- This is required if C(name) parameter is not supplied.
- The C(folder) is ignored, if C(uuid) is provided.
use_instance_uuid:
description:
- Whether to use the VMWare instance UUID rather than the BIOS UUID.
default: no
type: bool
version_added: '2.8'
folder:
description:
- Destination folder, absolute or relative path to find an existing guest.
Expand Down Expand Up @@ -125,6 +131,7 @@ def main():
argument_spec.update(
name=dict(type='str'),
uuid=dict(type='str'),
use_instance_uuid=dict(type='bool', default=False),
folder=dict(type='str'),
datacenter=dict(required=True, type='str'),
)
Expand Down
7 changes: 7 additions & 0 deletions lib/ansible/modules/cloud/vmware/vmware_guest_tools_wait.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@
description:
- UUID of the VM for which to wait until the tools become available, if known. This is VMware's unique identifier.
naphta marked this conversation as resolved.
Show resolved Hide resolved
- This is required, if C(name) is not supplied.
use_instance_uuid:
description:
- Whether to use the VMWare instance UUID rather than the BIOS UUID.
default: no
type: bool
version_added: '2.8'
extends_documentation_fragment: vmware.documentation
'''

Expand Down Expand Up @@ -146,6 +152,7 @@ def main():
name_match=dict(type='str', default='first', choices=['first', 'last']),
folder=dict(type='str'),
uuid=dict(type='str'),
use_instance_uuid=dict(type='bool', default=False),
)
module = AnsibleModule(
argument_spec=argument_spec,
Expand Down