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: Use managed object id to find VM #59143

Merged
merged 1 commit into from
Jul 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/ansible/module_utils/vmware.py
Original file line number Diff line number Diff line change
Expand Up @@ -880,7 +880,7 @@ def get_managed_objects_properties(self, vim_type, properties=None):
# Virtual Machine related functions
def get_vm(self):
"""
Find unique virtual machine either by UUID or Name.
Find unique virtual machine either by UUID, MoID or Name.
Returns: virtual machine object if found, else None.

"""
Expand Down Expand Up @@ -959,6 +959,8 @@ def get_vm(self):
elif vms:
# Unique virtual machine found.
vm_obj = vms[0]
elif self.params['moid']:
vm_obj = VmomiSupport.templateOf('VirtualMachine')(self.params['moid'], self.si._stub)

if vm_obj:
self.current_vm_obj = vm_obj
Expand Down
13 changes: 10 additions & 3 deletions lib/ansible/modules/cloud/vmware/vmware_export_ovf.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,17 @@
name:
description:
- Name of the virtual machine to export.
- This is a required parameter, if parameter C(uuid) is not supplied.
- This is a required parameter, if parameter C(uuid) or C(moid) is not supplied.
uuid:
description:
- Uuid of the virtual machine to export.
- This is a required parameter, if parameter C(name) is not supplied.
- This is a required parameter, if parameter C(name) or C(moid) is not supplied.
moid:
description:
- Managed Object ID of the instance to manage if known, this is a unique identifier only within a single vCenter instance.
- This is required if C(name) or C(uuid) is not supplied.
version_added: '2.9'
type: str
datacenter:
default: ha-datacenter
description:
Expand Down Expand Up @@ -304,6 +310,7 @@ def main():
argument_spec.update(
name=dict(type='str'),
uuid=dict(type='str'),
moid=dict(type='str'),
folder=dict(type='str'),
datacenter=dict(type='str', default='ha-datacenter'),
export_dir=dict(type='str'),
Expand All @@ -313,7 +320,7 @@ def main():
module = AnsibleModule(argument_spec=argument_spec,
supports_check_mode=True,
required_one_of=[
['name', 'uuid'],
['name', 'uuid', 'moid'],
],
)
pyv = VMwareExportVmOvf(module)
Expand Down
38 changes: 30 additions & 8 deletions lib/ansible/modules/cloud/vmware/vmware_guest_boot_facts.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
module: vmware_guest_boot_facts
short_description: Gather facts about boot options for the given virtual machine
description:
- This module can be used to gather facts aboyt boot options for the given virtual machine.
- Gather facts about boot options for the given virtual machine.
version_added: 2.7
author:
- Abhijeet Kasurde (@Akasurde) <akasurde@redhat.com>
- Abhijeet Kasurde (@Akasurde)
notes:
- Tested on vSphere 6.5
requirements:
Expand All @@ -33,11 +33,17 @@
name:
description:
- Name of the VM to work with.
- This is required if C(uuid) parameter is not supplied.
- This is required if C(uuid) or C(moid) parameter is not supplied.
uuid:
description:
- 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.
- This is required if C(name) or C(moid) parameter is not supplied.
moid:
description:
- Managed Object ID of the instance to manage if known, this is a unique identifier only within a single vCenter instance.
- This is required if C(name) or C(uuid) is not supplied.
version_added: '2.9'
type: str
use_instance_uuid:
description:
- Whether to use the VMware instance UUID rather than the BIOS UUID.
Expand All @@ -61,6 +67,15 @@
validate_certs: no
name: "{{ vm_name }}"
register: vm_boot_order_facts

- name: Gather facts about virtual machine's boot order using MoID
vmware_guest_boot_facts:
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
validate_certs: no
moid: "vm-42"
register: vm_moid_boot_order_facts
'''

RETURN = r"""
Expand Down Expand Up @@ -89,7 +104,7 @@
from ansible.module_utils.vmware import PyVmomi, vmware_argument_spec, find_vm_by_id

try:
from pyVmomi import vim
from pyVmomi import vim, VmomiSupport
except ImportError:
pass

Expand All @@ -99,6 +114,7 @@ def __init__(self, module):
super(VmBootFactsManager, self).__init__(module)
self.name = self.params['name']
self.uuid = self.params['uuid']
self.moid = self.params['moid']
self.use_instance_uuid = self.params['use_instance_uuid']
self.vm = None

Expand All @@ -120,13 +136,18 @@ def _get_vm(self):
if temp_vm_object.obj.name == self.name:
vms.append(temp_vm_object.obj)

elif self.moid:
vm_obj = VmomiSupport.templateOf('VirtualMachine')(self.module.params['moid'], self.si._stub)
if vm_obj:
vms.append(vm_obj)

if vms:
if self.params.get('name_match') == 'first':
self.vm = vms[0]
elif self.params.get('name_match') == 'last':
self.vm = vms[-1]
else:
self.module.fail_json(msg="Failed to find virtual machine using %s" % (self.name or self.uuid))
self.module.fail_json(msg="Failed to find virtual machine using %s" % (self.name or self.uuid or self.moid))

@staticmethod
def humanize_boot_order(boot_order):
Expand Down Expand Up @@ -165,6 +186,7 @@ def main():
argument_spec.update(
name=dict(type='str'),
uuid=dict(type='str'),
moid=dict(type='str'),
use_instance_uuid=dict(type='bool', default=False),
name_match=dict(
choices=['first', 'last'],
Expand All @@ -175,10 +197,10 @@ def main():
module = AnsibleModule(
argument_spec=argument_spec,
required_one_of=[
['name', 'uuid']
['name', 'uuid', 'moid']
],
mutually_exclusive=[
['name', 'uuid']
['name', 'uuid', 'moid']
],
supports_check_mode=True,
)
Expand Down
43 changes: 38 additions & 5 deletions lib/ansible/modules/cloud/vmware/vmware_guest_boot_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,17 @@
name:
description:
- Name of the VM to work with.
- This is required if C(uuid) parameter is not supplied.
- This is required if C(uuid) or C(moid) parameter is not supplied.
uuid:
description:
- 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.
- This is required if C(name) or C(moid) parameter is not supplied.
moid:
description:
- Managed Object ID of the instance to manage if known, this is a unique identifier only within a single vCenter instance.
- This is required if C(name) or C(uuid) is not supplied.
version_added: '2.9'
type: str
use_instance_uuid:
description:
- Whether to use the VMware instance UUID rather than the BIOS UUID.
Expand Down Expand Up @@ -107,6 +113,26 @@
- disk
delegate_to: localhost
register: vm_boot_order

- name: Change virtual machine's boot order using Virtual Machine MoID
vmware_guest_boot_manager:
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
moid: vm-42
boot_delay: 2000
enter_bios_setup: True
boot_retry_enabled: True
boot_retry_delay: 22300
boot_firmware: bios
secure_boot_enabled: False
boot_order:
- floppy
- cdrom
- ethernet
- disk
delegate_to: localhost
register: vm_boot_order
'''

RETURN = r"""
Expand Down Expand Up @@ -148,7 +174,7 @@
from ansible.module_utils.vmware import PyVmomi, vmware_argument_spec, find_vm_by_id, wait_for_task, TaskError

try:
from pyVmomi import vim
from pyVmomi import vim, VmomiSupport
except ImportError:
pass

Expand All @@ -158,6 +184,7 @@ def __init__(self, module):
super(VmBootManager, self).__init__(module)
self.name = self.params['name']
self.uuid = self.params['uuid']
self.moid = self.params['moid']
self.use_instance_uuid = self.params['use_instance_uuid']
self.vm = None

Expand All @@ -179,6 +206,11 @@ def _get_vm(self):
if temp_vm_object.obj.name == self.name:
vms.append(temp_vm_object.obj)

elif self.moid:
vm_obj = VmomiSupport.templateOf('VirtualMachine')(self.module.params['moid'], self.si._stub)
if vm_obj:
vms.append(vm_obj)

if vms:
if self.params.get('name_match') == 'first':
self.vm = vms[0]
Expand Down Expand Up @@ -324,6 +356,7 @@ def main():
argument_spec.update(
name=dict(type='str'),
uuid=dict(type='str'),
moid=dict(type='str'),
use_instance_uuid=dict(type='bool', default=False),
boot_order=dict(
type='list',
Expand Down Expand Up @@ -362,10 +395,10 @@ def main():
module = AnsibleModule(
argument_spec=argument_spec,
required_one_of=[
['name', 'uuid']
['name', 'uuid', 'moid']
],
mutually_exclusive=[
['name', 'uuid']
['name', 'uuid', 'moid']
],
)

Expand Down
28 changes: 25 additions & 3 deletions lib/ansible/modules/cloud/vmware/vmware_guest_custom_attributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
name:
description:
- Name of the virtual machine to work with.
- This is required parameter, if C(uuid) or C(moid) is not supplied.
required: True
state:
description:
Expand All @@ -47,7 +48,13 @@
uuid:
description:
- UUID of the virtual machine to manage if known. This is VMware's unique identifier.
- This is required parameter, if C(name) is not supplied.
- This is required parameter, if C(name) or C(moid) is not supplied.
moid:
description:
- Managed Object ID of the instance to manage if known, this is a unique identifier only within a single vCenter instance.
- This is required if C(name) or C(uuid) is not supplied.
version_added: '2.9'
type: str
use_instance_uuid:
description:
- Whether to use the VMware instance UUID rather than the BIOS UUID.
Expand Down Expand Up @@ -111,6 +118,17 @@
delegate_to: localhost
register: attributes

- name: Remove virtual machine Attribute using Virtual Machine MoID
vmware_guest_custom_attributes:
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
moid: vm-42
state: absent
attributes:
- name: MyAttribute
delegate_to: localhost
register: attributes
'''

RETURN = """
Expand Down Expand Up @@ -185,6 +203,7 @@ def main():
name=dict(required=True, type='str'),
folder=dict(type='str'),
uuid=dict(type='str'),
moid=dict(type='str'),
use_instance_uuid=dict(type='bool', default=False),
state=dict(type='str', default='present',
choices=['absent', 'present']),
Expand All @@ -201,7 +220,9 @@ def main():
module = AnsibleModule(
argument_spec=argument_spec,
supports_check_mode=True,
required_one_of=[['name', 'uuid']],
required_one_of=[
['name', 'uuid', 'moid']
],
)

if module.params.get('folder'):
Expand All @@ -224,8 +245,9 @@ def main():
module.exit_json(**results)
else:
# virtual machine does not exists
vm_id = (module.params.get('name') or module.params.get('uuid') or module.params.get('moid'))
module.fail_json(msg="Unable to manage custom attributes for non-existing"
" virtual machine %s" % (module.params.get('name') or module.params.get('uuid')))
" virtual machine %s" % vm_id)


if __name__ == '__main__':
Expand Down