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

Promote vmware guest #4

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
158 commits
Select commit Hold shift + click to select a range
4b3d7c6
Initial commit
goneri Mar 2, 2020
37022d9
vmware_guest_network: idempotency with state=absent
goneri Jan 31, 2020
5d4aac1
vmware_guest: fix dns_suffix list
Jan 30, 2020
ae8ce15
Not specify new disk filename
cuixiangz Feb 21, 2020
edb775a
vmware_guest_info: Handle list in guest facts
Akasurde Mar 5, 2020
028faad
setup_dvswitch: bump switch_version to 6.5.0 (#68)
goneri Mar 25, 2020
57bbafc
tox: enable flake8 (#70)
goneri Mar 25, 2020
c6a7c19
Vmware module maintenance (#69)
ChrisAHolland Mar 26, 2020
9b1b15b
Update Hardware version to include 15 - 6.7U2 (#99)
qadv Apr 7, 2020
5e2ab8d
zuul: define govcsim target (#95)
goneri Apr 8, 2020
97bf2af
define vcenter_1esxi_with_nested alias (#105)
goneri Apr 9, 2020
b6426eb
prepare_vmware_tests: isolate the 'VM Network' (#90)
goneri Apr 9, 2020
a82c3f4
re-enable vmware_guest_network test (#86)
goneri Apr 9, 2020
80826dc
vmware_guest_tools_wait: reenable the test (#97)
goneri Apr 10, 2020
72e6d8e
temporarily disable vmware_guest_network (#131)
goneri Apr 20, 2020
5300f40
Temporarily disable Tag and Category tests (#130)
Akasurde Apr 21, 2020
eaff91e
Revert "temporarily disable vmware_guest_network (#131)" (#147)
goneri Apr 27, 2020
49e1966
VMware: add hardware version 17 support in vmware_guest module (#153)
Tomorrow9 Apr 29, 2020
d41f7d7
VMware: add SATA cdrom support in vmware_guest module (#65)
Tomorrow9 May 11, 2020
04ca4f3
Add check mode, diff and some new functionality to vmware_guest_netwo…
acalm May 11, 2020
c36488f
Include FQCN in module examples (#168)
Akasurde May 14, 2020
be6d5ee
galaxy: ensure the import works (#194)
goneri May 20, 2020
ab0014b
Removed ANSIBLE_METADATA from all the modules (#189)
Akasurde May 25, 2020
5627c20
vmware_guest: add support for SATA and NVME disk controller types (#201)
Tomorrow9 May 27, 2020
1e30180
vmware_guest_disk: add sharing (multi-writer) capability (#212) (#214)
cspeidel Jun 4, 2020
38513cf
Revert "Temporarily disable Tag and Category tests" (#135)
goneri Jun 4, 2020
7bc8b2a
test: add ability to run govcsim with systemd-run (#231)
goneri Jun 11, 2020
7b862cb
VMware: Match state param in vmware_guest and vmware_guest_powerstate…
goneri Jun 17, 2020
5aef52f
vcsim/systemd: use the full path to vcsim (#246)
goneri Jun 19, 2020
a8e0c8e
Use FQCN to M() references (#291)
bmillemathias Jul 14, 2020
e71c301
tests: remove shippable entries from aliases (#281)
goneri Jul 14, 2020
334ff15
handle cdrom controller_number and unit_number integer (#277)
Tomorrow9 Jul 15, 2020
15559e0
Reset disk['destroy'] to False when user set not remove disk file exp…
goneri Jul 17, 2020
e7e3131
sanity: pylint fixes (#310)
Akasurde Jul 18, 2020
5e8bdf4
sanity: Doc fixes for vmware_guest (#312)
Akasurde Jul 21, 2020
82c726c
vmware_guest: Deprecate CDROM config as dict (#317)
Akasurde Aug 2, 2020
43387bb
Fix vmware_guest_serial_port to have idempotency (#358)
sky-joker Aug 27, 2020
86e00b1
Support special characters in an object name (#380)
sky-joker Sep 9, 2020
e13696e
Ensure that VM are created on the requested ESXi host (#400)
xenlo Sep 25, 2020
69adcba
vmware_guest_network: Fix to port group changes to work properly (#401)
sky-joker Sep 30, 2020
daf52c0
vmware_guest: fix issue of checking specified hardware version when d…
Tomorrow9 Oct 5, 2020
54a3b39
Fix the special characters aren't decoded by the find_obj method (#460)
sky-joker Nov 3, 2020
3379969
replace yes with true and no with false (#448)
rndmh3ro Nov 9, 2020
a7fd57f
tox: run yamllint (#478)
goneri Nov 9, 2020
62e99a4
vmware_guest: Document networks connected and start_connected sub-opt…
mariolenz Nov 23, 2020
1126921
Shorten URL for vApp Properties (#520)
Akasurde Nov 26, 2020
3ad055c
vmware_guest: Fix an issue with vSphere 7 when adding several virtual…
mariolenz Dec 11, 2020
baafea1
update teardown to support vsphere 7.0u1 (#568)
laidbackware Dec 16, 2020
71e1992
vmware_guest_disk: add SATA and NVMe disk support (#306)
Tomorrow9 Dec 17, 2020
2453611
vmware_guest: handle computer name while customization (#574)
Akasurde Dec 18, 2020
384f8c0
vmware_resource_pool: add integration test for using esxi_hostname pa…
sky-joker Dec 18, 2020
aa61a66
ensure the sanity tests pass with 2.10.5 (#620)
goneri Jan 21, 2021
4b320ae
vmware_guest: Define sub-options of hardware and customization in arg…
mariolenz Jan 21, 2021
a3f7617
vmware_guest_register_operation: Support the check mode (#626)
sky-joker Jan 25, 2021
0fc5b66
vmware_content_deploy_template: Make parameter optional (#476)
Akasurde Feb 1, 2021
4491be3
vmware_guest: Remove unnecessary hardware version check (#639)
mariolenz Feb 3, 2021
df7e518
Fix vmware_guest advanced_settings and customvalues (#179)
adarobin Feb 6, 2021
7c3be96
vmware_guest: Define sub-options of disk in argument_spec (#640)
mariolenz Feb 8, 2021
2a23680
instant clone module (#675)
Anant99-sys Mar 8, 2021
f329db3
vmware_guest: Correct check for advanced_settings (#701)
Akasurde Mar 9, 2021
9fad2a3
vmware_guest: Fix disk parameter validation (#705)
mariolenz Mar 16, 2021
a191d4a
New module: vmware_guest_storage_policy (#659)
tgates81 Mar 25, 2021
e22b145
vmware_guest: Fix integration test (#749)
mariolenz Mar 30, 2021
7bf3890
vmware_guest_instant_clone: CI test starts VM (#756)
goneri Mar 31, 2021
d65f7a3
CI: ensure vmware_guest tests only run on nested capable host (#755)
goneri Mar 31, 2021
5cd608f
vmware_guest_instance_clone: tests: Unable to set power state (#762)
goneri Apr 1, 2021
32b0d9c
vmware_guest_storage_policy: test need datastore (#774)
goneri Apr 4, 2021
d6cd9d0
vmware_guest_storage_policy: test: don't clean up the test VM (#772)
goneri Apr 4, 2021
2d06e88
tests: dont test the _facts modules (#771)
goneri Apr 4, 2021
3d1135b
vmware_guest_instant_clone: properly initialize the env before the te…
goneri Apr 4, 2021
ee67627
vmware_guest_register_operation: fix the test-suite (#770)
goneri Apr 5, 2021
95fd782
vmware_tag: Modify the category_id parameter to be required for all s…
sky-joker Apr 14, 2021
f946d48
vmware_dvs_portgroup: MAC learning, elastic PGs and uplink configurat…
mariolenz Apr 20, 2021
d33f70b
Clarify is_template in vmware_guest docs (#794)
timblaktu Apr 24, 2021
c57780f
Skip customvalues while deploying VM on a standalone ESXi (#812)
Akasurde Apr 26, 2021
974bf13
vmware_cluster_drs/ha/vsan: Announce change of enable parameter defau…
mariolenz May 5, 2021
33b2f13
vmware_guest - add integration test for delete_vm (#832)
abikouo May 6, 2021
b891372
vmware_guest: Improve virtualization based security (#816)
mariolenz May 15, 2021
5965cd9
Adding GuestOs Customization for Instant Clone feature (#796)
Anant99-sys Jun 11, 2021
ed9e89e
vmware_guest: Fix hostname is None when not used in playbook. (#831)
0x6d6b Jun 17, 2021
bc4f05a
Fix multiple content deploy issues (#747)
n3pjk Jun 18, 2021
be16f5e
vmware_guest_powerstate: Add the answer option to require in starting…
sky-joker Jun 20, 2021
19226a6
Move PyVmomiDeviceHelper to module_utils.vm_device_helper (#909)
mariolenz Jun 26, 2021
261f6ff
vmware_guest_powerstate: Fix an error that the datacenter key error h…
sky-joker Jul 11, 2021
095d3a6
govcsim: wait for the service for 1s (#954)
goneri Jul 13, 2021
cb240e9
tests: disable vmware_guest_instant_clone for now (#958)
goneri Jul 14, 2021
da6dd47
tests: remove zuul/vmware/vcenter_1esxi_with_nested (#957)
goneri Jul 14, 2021
c4a4869
Revert PR 957 (#967)
mariolenz Jul 15, 2021
761b2d3
tests: ensure we can create 'VM Network' (#966)
goneri Jul 19, 2021
086f893
Improve prepare_vmware_tests (#968)
mariolenz Jul 20, 2021
217078b
Revert "Revert PR 957 (#967)" (#970)
goneri Jul 21, 2021
278b33a
vmware_guest_instant_clone: Remove the default of the guestinfo_vars …
sky-joker Jul 22, 2021
ee9ebb3
Add NVDIMM device support in vmware_guest module (#960)
Tomorrow9 Aug 10, 2021
c21cf01
vmware_guest: Provide a meaningful message when customization failed …
VladRusakov Aug 10, 2021
dce617c
Add a new module vmware_vm_config_option (#998)
Tomorrow9 Sep 20, 2021
cdbb850
vmware_guest: Update documentation about "Resources" example (#1040)
JakeDEvans Sep 23, 2021
1359844
vmware_guest_disk/test: use vmx-13 (#1116)
goneri Nov 15, 2021
9936d39
vmware_guest/tests: drop useless tests (#1123)
goneri Nov 15, 2021
b776a92
test VM should not use more than 1 CPU (#1104)
goneri Nov 15, 2021
32715e1
always use debian8_64Guest in our functinal tests (#1105)
goneri Nov 15, 2021
292b07f
vmware_guest/test: reduce network_with_device res usage (#1110)
goneri Nov 15, 2021
3ad4663
tests: use hw version 11 (#1107)
goneri Nov 15, 2021
1d56ed9
tests: use vmware.vmware_rest to clean up the resources (#1125)
goneri Nov 15, 2021
ca32895
tests: remove linked_clone_d1_c1_f0.yml (#1108)
goneri Nov 15, 2021
dfb4d67
vmware_guest: fix the Windows Administrator password being set to str…
tomasodehnal Dec 6, 2021
5bc92e9
Fix version_added of vmware_guest.advanced_settings (#1156)
albers Dec 14, 2021
869bc3e
vmware_dvs_portgroup: Remove portgroup_type (#1150)
mariolenz Dec 25, 2021
6ce2852
vmware_guest: Make VBS requirements explicit (#1149)
mariolenz Dec 27, 2021
afc37bf
tests/govcsim: ignore some broken tests (#1193)
goneri Jan 18, 2022
3781170
Added support for multiple SCSI controllers (#1210)
kpenza Feb 3, 2022
578e826
vmware_guest_powerstate: Use FQCN in examples (#1276)
mariolenz Apr 5, 2022
bfd8fb7
vmware_guest_powerstate: Ignore trailing slash in folder parameter (#…
mariolenz Apr 9, 2022
7797e9f
Remove vcsim from tests (#1287)
mariolenz Apr 13, 2022
8916ad3
Remove when: vcsim is not defined from integration tests (#1288)
mariolenz Apr 21, 2022
48e0841
vmware_guest_move/tests: clean up the f1 directory (#1292)
goneri Apr 22, 2022
a53c8c0
VCSIM removal part 3 (#1293)
mariolenz Apr 25, 2022
50d2393
VCSIM removal part 4 (#1294)
mariolenz May 2, 2022
ddc4bf5
add a supplement description to set network parameters to a guest (#1…
sky-joker Jun 29, 2022
957e51d
vmware_guest: change fail to fail_json function (#1402)
Tomorrow9 Jul 19, 2022
dc3909d
Use SPDX-License-Identifier, mention all licenses in galaxy.yml, add …
mariolenz Jul 20, 2022
d2ccd76
Improve module documentation (#1416)
mariolenz Aug 4, 2022
014a345
vmware_guest_boot_manager: Add initial integration test (#1445)
mariolenz Sep 4, 2022
d7da41f
vmware_guest_boot_manager: Remove secure_boot_enabled default (#1461)
mariolenz Sep 17, 2022
dedc287
vmware_guest_network: Remove deprecated parameter networks (#1459)
mariolenz Sep 18, 2022
9aafead
Remove version_added: 1.x part 1 (#1473)
mariolenz Sep 24, 2022
cf5f7ae
This Commit adds support for the script_text Parameter. (#1485)
Oct 17, 2022
82a9d0e
Fix non-matching defaults in docs (#1530)
mariolenz Nov 2, 2022
7b8b586
vmware_guest_custom_attributes: Improve the code quality and add the …
sky-joker Nov 7, 2022
05d0944
vmware_guest.py - Add CPU/MEM shares support on guest (#1539)
rverdam Nov 18, 2022
1e0097d
vmware_guest_boot_manager: Add a new parameter 'boot_hdd_name' to spe…
Tomorrow9 Nov 22, 2022
1c90a42
vmware_guest: Add vPMC support (#1552)
mariolenz Nov 24, 2022
81bda51
vmware_guest: Fix check mode (#1553)
mariolenz Dec 30, 2022
9af39bf
vmware_guest_network: add support pvrdma network adapter (#1579)
Tomorrow9 Jan 13, 2023
0c4da58
vmware_guest_disk: add IDE disk support (#1616)
Tomorrow9 Feb 6, 2023
03e4978
vmware_guest: fix adding nvdimm device issue on ESXi host (#1657)
Tomorrow9 Mar 7, 2023
06f8a65
Issue1660 2 (#1665)
mariolenz Mar 8, 2023
7a4bbb2
Issue 1660 part 3 (#1666)
mariolenz Mar 9, 2023
b2b1c4b
Issue 1660 part 5 (#1669)
mariolenz Mar 12, 2023
0549b8e
Issue 1660 part 6 (#1671)
mariolenz Mar 13, 2023
dc84a55
vmware_guest - set custom attributes on VM create (#1713)
dericcrago Apr 22, 2023
9e5390b
Update community.vmware.vmware_guest_module.rst (#1714)
cutrightjm Apr 25, 2023
f61ee1f
CI: Remove unnecessary prepare step (#1754)
mariolenz May 30, 2023
604099b
Do not fail when state is absent and disk is not found (#1766)
ivangeel74 Jun 19, 2023
15ec21c
Also look at NFS mounted datastores, not just VMFS (#1751)
nikatbu Jun 25, 2023
897b4ea
Implement encryption configuration for vm vmotion and ft (#1779)
Alestor Jul 26, 2023
f5464a3
Fix typo in Docs for vmware_guest module (#1836)
Alestor Sep 6, 2023
8575e05
Add snapshot_id option to vmware_guest_snapshot module (#1847)
sky-joker Oct 6, 2023
8c383f4
vmware_guest: Remove CD-ROM configuration as dict (#1882)
mariolenz Oct 23, 2023
52574c5
Implement semantic markup (4) (#1888)
mariolenz Oct 25, 2023
ad9149f
Implement semantic markup (5) (#1890)
mariolenz Oct 25, 2023
b56b679
Fix integration tests (#1941)
mariolenz Dec 5, 2023
b45a2af
Add IPv6 support for VM network interfaces (#1937)
svluder Dec 14, 2023
4c20c4d
Fix failure of vm reconfiguration with enabled virt_based_security (#…
Yannik Jan 24, 2024
5b8dbec
Document that all parameters and VMware object names are case sensiti…
mariolenz Mar 7, 2024
878f305
Update runtime
machacekondra Mar 8, 2024
9355f4e
Update FQDN
machacekondra Mar 8, 2024
7cb0088
Update import path
machacekondra Mar 8, 2024
702b3d0
Remove collection reference inside the tests
machacekondra Mar 8, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion meta/runtime.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
---
requires_ansible: '>=2.11.0' # Use '>=2.9.10' instead, if needed
requires_ansible: '>=2.11.0'
3,632 changes: 3,632 additions & 0 deletions plugins/modules/vmware_guest.py

Large diffs are not rendered by default.

348 changes: 348 additions & 0 deletions plugins/modules/vmware_guest_powerstate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,348 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-

# Copyright: (c) 2017, Abhijeet Kasurde <[email protected]>
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
# SPDX-License-Identifier: GPL-3.0-or-later

from __future__ import absolute_import, division, print_function
__metaclass__ = type


DOCUMENTATION = r"""
---
module: vmware_guest_powerstate
short_description: Manages power states of virtual machines in vCenter
description:
- Power on / Power off / Restart a virtual machine.
author:
- Abhijeet Kasurde (@Akasurde) <[email protected]>
options:
datacenter:
description:
- The datacenter where the VM you'd like to operate the power.
default: ha-datacenter
type: str
state:
description:
- Set the state of the virtual machine.
choices: [ powered-off, powered-on, reboot-guest, restarted, shutdown-guest, suspended, present]
default: present
type: str
name:
description:
- Name of the virtual machine to work with.
- Virtual machine names in vCenter are not necessarily unique, which may be problematic, see O(name_match).
type: str
name_match:
description:
- If multiple virtual machines matching the name, use the first or last found.
default: first
choices: [ first, last ]
type: str
uuid:
description:
- UUID of the instance to manage if known, this is VMware's unique identifier.
- This is required if O(name) or O(moid) is not supplied.
type: str
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 O(name) or O(uuid) is not supplied.
type: str
use_instance_uuid:
description:
- Whether to use the VMware instance UUID rather than the BIOS UUID.
default: false
type: bool
folder:
description:
- Destination folder, absolute or relative path to find an existing guest.
- 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'
type: str
scheduled_at:
description:
- Date and time in string format at which specified task needs to be performed.
- "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.
type: str
schedule_task_name:
description:
- Name of schedule task.
- Valid only if O(scheduled_at) is specified.
type: str
required: false
schedule_task_description:
description:
- Description of schedule task.
- Valid only if O(scheduled_at) is specified.
type: str
required: false
schedule_task_enabled:
description:
- Flag to indicate whether the scheduled task is enabled or disabled.
type: bool
required: false
default: true
force:
description:
- Ignore warnings and complete the actions.
- This parameter is useful while forcing virtual machine state.
default: false
type: bool
state_change_timeout:
description:
- If the O(state=shutdown-guest), by default the module will return immediately after sending the shutdown signal.
- If this argument is set to a positive integer, the module will instead wait for the VM to reach the poweredoff state.
- The value sets a timeout in seconds for the module to wait for the state change.
default: 0
type: int
answer:
description:
- A list of questions to answer, should one or more arise while waiting for the task to complete.
- Some common uses are to allow a cdrom to be changed even if locked, or to answer the question as to whether a VM was copied or moved.
- Can be used if O(state=powered-on).
suboptions:
question:
description:
- The message id, for example C(msg.uuid.altered).
type: str
required: true
response:
description:
- The choice key, for example C(button.uuid.copiedTheVM).
type: str
required: true
type: list
elements: dict
extends_documentation_fragment:
- community.vmware.vmware.documentation
"""

EXAMPLES = r"""
- name: Set the state of a virtual machine to poweroff
vmware.vmware.vmware_guest_powerstate:
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
folder: "/{{ datacenter_name }}/vm/my_folder"
name: "{{ guest_name }}"
state: powered-off
delegate_to: localhost
register: deploy

- name: Set the state of a virtual machine to poweron using MoID
vmware.vmware.vmware_guest_powerstate:
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
folder: "/{{ datacenter_name }}/vm/my_folder"
moid: vm-42
state: powered-on
delegate_to: localhost
register: deploy

- name: Set the state of a virtual machine to poweroff at given scheduled time
vmware.vmware.vmware_guest_powerstate:
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
folder: "/{{ datacenter_name }}/vm/my_folder"
name: "{{ guest_name }}"
state: powered-off
scheduled_at: "09/01/2018 10:18"
schedule_task_name: "task_00001"
schedule_task_description: "Sample task to poweroff VM"
schedule_task_enabled: true
delegate_to: localhost
register: deploy_at_schedule_datetime

- name: Wait for the virtual machine to shutdown
vmware.vmware.vmware_guest_powerstate:
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
name: "{{ guest_name }}"
state: shutdown-guest
state_change_timeout: 200
delegate_to: localhost
register: deploy

- name: Automatically answer if a question locked a virtual machine
block:
- name: Power on a virtual machine without the answer param
vmware.vmware.vmware_guest_powerstate:
hostname: "{{ esxi_hostname }}"
username: "{{ esxi_username }}"
password: "{{ esxi_password }}"
validate_certs: false
folder: "{{ f1 }}"
name: "{{ vm_name }}"
state: powered-on
rescue:
- name: Power on a virtual machine with the answer param
vmware.vmware.vmware_guest_powerstate:
hostname: "{{ esxi_hostname }}"
username: "{{ esxi_username }}"
password: "{{ esxi_password }}"
validate_certs: false
folder: "{{ f1 }}"
name: "{{ vm_name }}"
answer:
- question: "msg.uuid.altered"
response: "button.uuid.copiedTheVM"
state: powered-on
"""

RETURN = r""" # """

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

from random import randint
from datetime import datetime
from ansible.module_utils.basic import AnsibleModule
from ansible_collections.vmware.vmware.plugins.module_utils.vmware import PyVmomi, set_vm_power_state, vmware_argument_spec, \
check_answer_question_status, make_answer_response, answer_question, gather_vm_facts
from ansible.module_utils._text import to_native


def main():
argument_spec = vmware_argument_spec()
argument_spec.update(
datacenter=dict(type='str', default='ha-datacenter'),
state=dict(type='str', default='present',
choices=['present', 'powered-off', 'powered-on', 'reboot-guest', 'restarted', 'shutdown-guest', 'suspended']),
name=dict(type='str'),
name_match=dict(type='str', choices=['first', 'last'], default='first'),
uuid=dict(type='str'),
moid=dict(type='str'),
use_instance_uuid=dict(type='bool', default=False),
folder=dict(type='str'),
force=dict(type='bool', default=False),
scheduled_at=dict(type='str'),
schedule_task_name=dict(),
schedule_task_description=dict(),
schedule_task_enabled=dict(type='bool', default=True),
state_change_timeout=dict(type='int', default=0),
answer=dict(type='list',
elements='dict',
options=dict(
question=dict(type='str', required=True),
response=dict(type='str', required=True)
))
)

module = AnsibleModule(
argument_spec=argument_spec,
supports_check_mode=False,
mutually_exclusive=[
['name', 'uuid', 'moid'],
['scheduled_at', 'answer']
],
)

result = dict(changed=False,)

if module.params['folder']:
module.params['folder'] = module.params['folder'].rstrip('/')

pyv = PyVmomi(module)

# Check if the VM exists before continuing
vm = pyv.get_vm()

if vm:
# VM already exists, so set power state
scheduled_at = module.params.get('scheduled_at', None)
if scheduled_at:
if not pyv.is_vcenter():
module.fail_json(msg="Scheduling task requires vCenter, hostname %s "
"is an ESXi server." % module.params.get('hostname'))
powerstate = {
'present': vim.VirtualMachine.PowerOn,
'powered-off': vim.VirtualMachine.PowerOff,
'powered-on': vim.VirtualMachine.PowerOn,
'reboot-guest': vim.VirtualMachine.RebootGuest,
'restarted': vim.VirtualMachine.Reset,
'shutdown-guest': vim.VirtualMachine.ShutdownGuest,
'suspended': vim.VirtualMachine.Suspend,
}
dt = ''
try:
dt = datetime.strptime(scheduled_at, '%d/%m/%Y %H:%M')
except ValueError as e:
module.fail_json(msg="Failed to convert given date and time string to Python datetime object,"
"please specify string in 'dd/mm/yyyy hh:mm' format: %s" % to_native(e))
schedule_task_spec = vim.scheduler.ScheduledTaskSpec()
schedule_task_name = module.params['schedule_task_name'] or 'task_%s' % str(randint(10000, 99999))
schedule_task_desc = module.params['schedule_task_description']
if schedule_task_desc is None:
schedule_task_desc = 'Schedule task for vm %s for ' \
'operation %s at %s' % (vm.name, module.params['state'], scheduled_at)
schedule_task_spec.name = schedule_task_name
schedule_task_spec.description = schedule_task_desc
schedule_task_spec.scheduler = vim.scheduler.OnceTaskScheduler()
schedule_task_spec.scheduler.runAt = dt
schedule_task_spec.action = vim.action.MethodAction()
schedule_task_spec.action.name = powerstate[module.params['state']]
schedule_task_spec.enabled = module.params['schedule_task_enabled']

try:
pyv.content.scheduledTaskManager.CreateScheduledTask(vm, schedule_task_spec)
# As this is async task, we create scheduled task and mark state to changed.
module.exit_json(changed=True)
except vim.fault.InvalidName as e:
module.fail_json(msg="Failed to create scheduled task %s for %s : %s" % (module.params.get('state'),
vm.name,
to_native(e.msg)))
except vim.fault.DuplicateName as e:
module.exit_json(changed=False, details=to_native(e.msg))
except vmodl.fault.InvalidArgument as e:
module.fail_json(msg="Failed to create scheduled task %s as specifications "
"given are invalid: %s" % (module.params.get('state'),
to_native(e.msg)))
else:
# Check if a virtual machine is locked by a question
if check_answer_question_status(vm) and module.params['answer']:
try:
responses = make_answer_response(vm, module.params['answer'])
answer_question(vm, responses)
except Exception as e:
module.fail_json(msg="%s" % e)

# Wait until a virtual machine is unlocked
while True:
if check_answer_question_status(vm) is False:
break

result['changed'] = True
result['instance'] = gather_vm_facts(pyv.content, vm)
else:
result = set_vm_power_state(pyv.content, vm, module.params['state'], module.params['force'], module.params['state_change_timeout'],
module.params['answer'])
result['answer'] = module.params['answer']
else:
id = module.params.get('uuid') or module.params.get('moid') or module.params.get('name')
module.fail_json(msg="Unable to set power state for non-existing virtual machine : '%s'" % id)

if result.get('failed') is True:
module.fail_json(**result)

module.exit_json(**result)


if __name__ == '__main__':
main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
allow_duplicates: true
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
---
- include_tasks: teardown_with_esxi.yml
when: esxi_hosts|length > 0
- include_tasks: teardown.yml

- when: setup_esxi_instance is not defined
block:
- include_tasks: setup_datacenter.yml
- include_tasks: setup_cluster.yml
- include_tasks: setup_attach_hosts.yml
when: setup_attach_host is defined
- include_tasks: move_host_out_of_cluster.yml
when: move_host_out_of_cluster is defined
- include_tasks: setup_datastore.yml
when: setup_datastore is defined
- include_tasks: setup_virtualmachines.yml
when: setup_virtualmachines is defined
- include_tasks: setup_switch.yml
when: setup_switch is defined
- include_tasks: setup_dvswitch.yml
when: setup_dvswitch is defined
- include_tasks: setup_resource_pool.yml
when: setup_resource_pool is defined
- include_tasks: setup_category.yml
when: setup_category is defined
- include_tasks: setup_tag.yml
when: setup_tag is defined
- include_tasks: setup_content_library.yml
when: setup_content_library is defined
- include_tasks: setup_dvs_portgroup.yml
when: setup_dvs_portgroup is defined
Loading
Loading