[2.8] VMware: Use short unique task name for schedule task

* Updated documentation
* Updated task name logic

Fixes: #56987

Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>
This commit is contained in:
Abhijeet Kasurde 2019-06-01 09:32:57 +05:30 committed by Toshio Kuratomi
parent 36580c4450
commit 4ddc8b4769
2 changed files with 27 additions and 12 deletions

View file

@ -0,0 +1,2 @@
minor_changes:
- Use shorter and unique random task name for scheduled task created by vmware_guest_powerstate (https://github.com/ansible/ansible/issues/56987).

View file

@ -29,28 +29,37 @@ options:
- Set the state of the virtual machine. - Set the state of the virtual machine.
choices: [ powered-off, powered-on, reboot-guest, restarted, shutdown-guest, suspended, present] choices: [ powered-off, powered-on, reboot-guest, restarted, shutdown-guest, suspended, present]
default: present default: present
type: str
required: False
name: name:
description: description:
- Name of the virtual machine to work with. - Name of the virtual machine to work with.
- Virtual machine names in vCenter are not necessarily unique, which may be problematic, see C(name_match). - Virtual machine names in vCenter are not necessarily unique, which may be problematic, see C(name_match).
type: str
required: False
name_match: name_match:
description: description:
- If multiple virtual machines matching the name, use the first or last found. - If multiple virtual machines matching the name, use the first or last found.
default: first default: first
choices: [ first, last ] choices: [ first, last ]
type: str
required: False
uuid: uuid:
description: description:
- UUID of the instance to manage if known, this is VMware's unique identifier. - UUID of the instance to manage if known, this is VMware's unique identifier.
- This is required if name is not supplied. - This is required if name is not supplied.
type: str
required: False
use_instance_uuid: use_instance_uuid:
description: description:
- Whether to use the VMWare instance UUID rather than the BIOS UUID. - Whether to use the VMWare instance UUID rather than the BIOS UUID.
default: no default: no
type: bool type: bool
version_added: '2.8' version_added: '2.8'
required: False
folder: folder:
description: description:
- Destination folder, absolute or relative path to find an existing guest or create the new guest. - Destination folder path to find an existing guest.
- The folder should include the datacenter. ESX's datacenter is ha-datacenter - The folder should include the datacenter. ESX's datacenter is ha-datacenter
- 'Examples:' - 'Examples:'
- ' folder: /ha-datacenter/vm' - ' folder: /ha-datacenter/vm'
@ -62,14 +71,15 @@ options:
- ' folder: /folder1/datacenter1/vm' - ' folder: /folder1/datacenter1/vm'
- ' folder: folder1/datacenter1/vm' - ' folder: folder1/datacenter1/vm'
- ' folder: /folder1/datacenter1/vm/folder2' - ' folder: /folder1/datacenter1/vm/folder2'
- ' folder: vm/folder2' type: str
- ' folder: folder2' required: False
default: /vm
scheduled_at: scheduled_at:
description: description:
- Date and time in string format at which specificed task needs to be performed. - Date and time in string format at which specificed task needs to be performed.
- "The required format for date and time - 'dd/mm/yyyy hh:mm'." - "The required format for date and time - 'dd/mm/yyyy hh:mm'."
- Scheduling task requires vCenter server. A standalone ESXi server does not support this option. - Scheduling task requires vCenter server. A standalone ESXi server does not support this option.
type: str
required: False
force: force:
description: description:
- Ignore warnings and complete the actions. - Ignore warnings and complete the actions.
@ -77,6 +87,7 @@ options:
default: False default: False
type: bool type: bool
version_added: 2.5 version_added: 2.5
required: False
state_change_timeout: state_change_timeout:
description: description:
- If the C(state) is set to C(shutdown-guest), by default the module will return immediately after sending the shutdown signal. - If the C(state) is set to C(shutdown-guest), by default the module will return immediately after sending the shutdown signal.
@ -84,6 +95,7 @@ options:
- The value sets a timeout in seconds for the module to wait for the state change. - The value sets a timeout in seconds for the module to wait for the state change.
default: 0 default: 0
version_added: '2.6' version_added: '2.6'
required: False
extends_documentation_fragment: vmware.documentation extends_documentation_fragment: vmware.documentation
''' '''
@ -131,6 +143,7 @@ try:
except ImportError: except ImportError:
pass pass
from random import randint
from datetime import datetime from datetime import datetime
from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.vmware import PyVmomi, set_vm_power_state, vmware_argument_spec from ansible.module_utils.vmware import PyVmomi, set_vm_power_state, vmware_argument_spec
@ -146,18 +159,17 @@ def main():
name_match=dict(type='str', choices=['first', 'last'], default='first'), name_match=dict(type='str', choices=['first', 'last'], default='first'),
uuid=dict(type='str'), uuid=dict(type='str'),
use_instance_uuid=dict(type='bool', default=False), use_instance_uuid=dict(type='bool', default=False),
folder=dict(type='str', default='/vm'), folder=dict(type='str'),
force=dict(type='bool', default=False), force=dict(type='bool', default=False),
scheduled_at=dict(type='str'), scheduled_at=dict(type='str'),
state_change_timeout=dict(type='int', default=0), state_change_timeout=dict(type='int', default=0),
) )
module = AnsibleModule(argument_spec=argument_spec, module = AnsibleModule(
supports_check_mode=False, argument_spec=argument_spec,
mutually_exclusive=[ supports_check_mode=False,
['name', 'uuid'], mutually_exclusive=[['name', 'uuid']],
], )
)
result = dict(changed=False,) result = dict(changed=False,)
@ -174,6 +186,7 @@ def main():
module.fail_json(msg="Scheduling task requires vCenter, hostname %s " module.fail_json(msg="Scheduling task requires vCenter, hostname %s "
"is an ESXi server." % module.params.get('hostname')) "is an ESXi server." % module.params.get('hostname'))
powerstate = { powerstate = {
'present': vim.VirtualMachine.PowerOn,
'powered-off': vim.VirtualMachine.PowerOff, 'powered-off': vim.VirtualMachine.PowerOff,
'powered-on': vim.VirtualMachine.PowerOn, 'powered-on': vim.VirtualMachine.PowerOn,
'reboot-guest': vim.VirtualMachine.RebootGuest, 'reboot-guest': vim.VirtualMachine.RebootGuest,
@ -191,7 +204,7 @@ def main():
schedule_task_desc = 'Schedule task for vm %s for operation %s at %s' % (vm.name, schedule_task_desc = 'Schedule task for vm %s for operation %s at %s' % (vm.name,
module.params.get('state'), module.params.get('state'),
scheduled_at) scheduled_at)
schedule_task_spec.name = schedule_task_desc schedule_task_spec.name = 'task_%s' % str(randint(10000, 99999))
schedule_task_spec.description = schedule_task_desc schedule_task_spec.description = schedule_task_desc
schedule_task_spec.scheduler = vim.scheduler.OnceTaskScheduler() schedule_task_spec.scheduler = vim.scheduler.OnceTaskScheduler()
schedule_task_spec.scheduler.runAt = dt schedule_task_spec.scheduler.runAt = dt