| 1 |
#!/usr/bin/python |
| 2 |
# |
| 3 |
# Copyright (c) 2018, Bojan Vitnik <[email protected]> |
| 4 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 6 |
|
| 7 |
from __future__ import annotations |
| 8 |
|
| 9 |
DOCUMENTATION = r""" |
| 10 |
module: xenserver_guest_powerstate |
| 11 |
short_description: Manages power states of virtual machines running on Citrix Hypervisor/XenServer host or pool |
| 12 |
description: This module can be used to power on, power off, restart or suspend virtual machine and gracefully reboot or shutdown |
| 13 |
guest OS of virtual machine. |
| 14 |
author: |
| 15 |
- Bojan Vitnik (@bvitnik) <[email protected]> |
| 16 |
notes: |
| 17 |
- Minimal supported version of XenServer is 5.6. |
| 18 |
- Module was tested with XenServer 6.5, 7.1, 7.2, 7.6, Citrix Hypervisor 8.0, XCP-ng 7.6 and 8.0. |
| 19 |
- 'To acquire XenAPI Python library, just run C(pip install XenAPI) on your Ansible Control Node. The library can also be |
| 20 |
found inside Citrix Hypervisor/XenServer SDK (downloadable from Citrix website). Copy the C(XenAPI.py) file from the SDK |
| 21 |
to your Python site-packages on your Ansible Control Node to use it. Latest version of the library can also be acquired |
| 22 |
from GitHub: U(https://raw.githubusercontent.com/xapi-project/xen-api/master/scripts/examples/python/XenAPI/XenAPI.py).' |
| 23 |
- If no scheme is specified in C(hostname), module defaults to C(http://) because C(https://) is problematic in most setups. |
| 24 |
Make sure you are accessing XenServer host in trusted environment or use C(https://) scheme explicitly. |
| 25 |
- 'To use C(https://) scheme for C(hostname) you have to either import host certificate to your OS certificate store or |
| 26 |
use C(validate_certs: no) which requires XenAPI library from XenServer 7.2 SDK or newer and Python 2.7.9 or newer.' |
| 27 |
requirements: |
| 28 |
- XenAPI |
| 29 |
attributes: |
| 30 |
check_mode: |
| 31 |
support: full |
| 32 |
diff_mode: |
| 33 |
support: none |
| 34 |
options: |
| 35 |
state: |
| 36 |
description: |
| 37 |
- Specify the state VM should be in. |
| 38 |
- If O(state) is set to value other than V(present), then VM is transitioned into required state and facts are returned. |
| 39 |
- If O(state) is set to V(present), then VM is just checked for existence and facts are returned. |
| 40 |
type: str |
| 41 |
default: present |
| 42 |
choices: [powered-on, powered-off, restarted, shutdown-guest, reboot-guest, suspended, present] |
| 43 |
name: |
| 44 |
description: |
| 45 |
- Name of the VM to manage. |
| 46 |
- VMs running on XenServer do not necessarily have unique names. The module fails if multiple VMs with same name are |
| 47 |
found. |
| 48 |
- In case of multiple VMs with same name, use O(uuid) to uniquely specify VM to manage. |
| 49 |
- This parameter is case sensitive. |
| 50 |
type: str |
| 51 |
aliases: [name_label] |
| 52 |
uuid: |
| 53 |
description: |
| 54 |
- UUID of the VM to manage if known. This is XenServer's unique identifier. |
| 55 |
- It is required if name is not unique. |
| 56 |
type: str |
| 57 |
wait_for_ip_address: |
| 58 |
description: |
| 59 |
- Wait until XenServer detects an IP address for the VM. |
| 60 |
- This requires XenServer Tools to be preinstalled on the VM to work properly. |
| 61 |
type: bool |
| 62 |
default: false |
| 63 |
state_change_timeout: |
| 64 |
description: |
| 65 |
- By default, module waits indefinitely for VM to change state or acquire an IP address if O(wait_for_ip_address=true). |
| 66 |
- If this parameter is set to positive value, the module instead waits specified number of seconds for the state change. |
| 67 |
- In case of timeout, module generates an error message. |
| 68 |
type: int |
| 69 |
default: 0 |
| 70 |
extends_documentation_fragment: |
| 71 |
- community.general._xenserver.documentation |
| 72 |
- community.general._attributes |
| 73 |
""" |
| 74 |
|
| 75 |
EXAMPLES = r""" |
| 76 |
- name: Power on VM |
| 77 |
community.general.xenserver_guest_powerstate: |
| 78 |
hostname: "{{ xenserver_hostname }}" |
| 79 |
username: "{{ xenserver_username }}" |
| 80 |
password: "{{ xenserver_password }}" |
| 81 |
name: testvm_11 |
| 82 |
state: powered-on |
| 83 |
delegate_to: localhost |
| 84 |
register: facts |
| 85 |
""" |
| 86 |
|
| 87 |
RETURN = r""" |
| 88 |
instance: |
| 89 |
description: Metadata about the VM. |
| 90 |
returned: always |
| 91 |
type: dict |
| 92 |
sample: |
| 93 |
{ |
| 94 |
"cdrom": { |
| 95 |
"type": "none" |
| 96 |
}, |
| 97 |
"customization_agent": "native", |
| 98 |
"disks": [ |
| 99 |
{ |
| 100 |
"name": "windows-template-testing-0", |
| 101 |
"name_desc": "", |
| 102 |
"os_device": "xvda", |
| 103 |
"size": 42949672960, |
| 104 |
"sr": "Local storage", |
| 105 |
"sr_uuid": "0af1245e-bdb0-ba33-1446-57a962ec4075", |
| 106 |
"vbd_userdevice": "0" |
| 107 |
}, |
| 108 |
{ |
| 109 |
"name": "windows-template-testing-1", |
| 110 |
"name_desc": "", |
| 111 |
"os_device": "xvdb", |
| 112 |
"size": 42949672960, |
| 113 |
"sr": "Local storage", |
| 114 |
"sr_uuid": "0af1245e-bdb0-ba33-1446-57a962ec4075", |
| 115 |
"vbd_userdevice": "1" |
| 116 |
} |
| 117 |
], |
| 118 |
"domid": "56", |
| 119 |
"folder": "", |
| 120 |
"hardware": { |
| 121 |
"memory_mb": 8192, |
| 122 |
"num_cpu_cores_per_socket": 2, |
| 123 |
"num_cpus": 4 |
| 124 |
}, |
| 125 |
"home_server": "", |
| 126 |
"is_template": false, |
| 127 |
"name": "windows-template-testing", |
| 128 |
"name_desc": "", |
| 129 |
"networks": [ |
| 130 |
{ |
| 131 |
"gateway": "192.168.0.254", |
| 132 |
"gateway6": "fc00::fffe", |
| 133 |
"ip": "192.168.0.200", |
| 134 |
"ip6": [ |
| 135 |
"fe80:0000:0000:0000:e9cb:625a:32c5:c291", |
| 136 |
"fc00:0000:0000:0000:0000:0000:0000:0001" |
| 137 |
], |
| 138 |
"mac": "ba:91:3a:48:20:76", |
| 139 |
"mtu": "1500", |
| 140 |
"name": "Pool-wide network associated with eth1", |
| 141 |
"netmask": "255.255.255.128", |
| 142 |
"prefix": "25", |
| 143 |
"prefix6": "64", |
| 144 |
"vif_device": "0" |
| 145 |
} |
| 146 |
], |
| 147 |
"other_config": { |
| 148 |
"base_template_name": "Windows Server 2016 (64-bit)", |
| 149 |
"import_task": "OpaqueRef:e43eb71c-45d6-5351-09ff-96e4fb7d0fa5", |
| 150 |
"install-methods": "cdrom", |
| 151 |
"instant": "true", |
| 152 |
"mac_seed": "f83e8d8a-cfdc-b105-b054-ef5cb416b77e" |
| 153 |
}, |
| 154 |
"platform": { |
| 155 |
"acpi": "1", |
| 156 |
"apic": "true", |
| 157 |
"cores-per-socket": "2", |
| 158 |
"device_id": "0002", |
| 159 |
"hpet": "true", |
| 160 |
"nx": "true", |
| 161 |
"pae": "true", |
| 162 |
"timeoffset": "-25200", |
| 163 |
"vga": "std", |
| 164 |
"videoram": "8", |
| 165 |
"viridian": "true", |
| 166 |
"viridian_reference_tsc": "true", |
| 167 |
"viridian_time_ref_count": "true" |
| 168 |
}, |
| 169 |
"state": "poweredon", |
| 170 |
"uuid": "e3c0b2d5-5f05-424e-479c-d3df8b3e7cda", |
| 171 |
"xenstore_data": { |
| 172 |
"vm-data": "" |
| 173 |
} |
| 174 |
} |
| 175 |
""" |
| 176 |
|
| 177 |
|
| 178 |
from ansible.module_utils.basic import AnsibleModule |
| 179 |
|
| 180 |
from ansible_collections.community.general.plugins.module_utils._xenserver import ( |
| 181 |
XenServerObject, |
| 182 |
gather_vm_facts, |
| 183 |
gather_vm_params, |
| 184 |
get_object_ref, |
| 185 |
set_vm_power_state, |
| 186 |
wait_for_vm_ip_address, |
| 187 |
xenserver_common_argument_spec, |
| 188 |
) |
| 189 |
|
| 190 |
|
| 191 |
class XenServerVM(XenServerObject): |
| 192 |
"""Class for managing XenServer VM. |
| 193 |
|
| 194 |
Attributes: |
| 195 |
vm_ref (str): XAPI reference to VM. |
| 196 |
vm_params (dict): A dictionary with VM parameters as returned |
| 197 |
by gather_vm_params() function. |
| 198 |
""" |
| 199 |
|
| 200 |
def __init__(self, module): |
| 201 |
"""Inits XenServerVM using module parameters. |
| 202 |
|
| 203 |
Args: |
| 204 |
module: Reference to Ansible module object. |
| 205 |
""" |
| 206 |
super().__init__(module) |
| 207 |
|
| 208 |
self.vm_ref = get_object_ref( |
| 209 |
self.module, |
| 210 |
self.module.params["name"], |
| 211 |
self.module.params["uuid"], |
| 212 |
obj_type="VM", |
| 213 |
fail=True, |
| 214 |
msg_prefix="VM search: ", |
| 215 |
) |
| 216 |
self.gather_params() |
| 217 |
|
| 218 |
def gather_params(self): |
| 219 |
"""Gathers all VM parameters available in XAPI database.""" |
| 220 |
self.vm_params = gather_vm_params(self.module, self.vm_ref) |
| 221 |
|
| 222 |
def gather_facts(self): |
| 223 |
"""Gathers and returns VM facts.""" |
| 224 |
return gather_vm_facts(self.module, self.vm_params) |
| 225 |
|
| 226 |
def set_power_state(self, power_state): |
| 227 |
"""Controls VM power state.""" |
| 228 |
state_changed, current_state = set_vm_power_state( |
| 229 |
self.module, self.vm_ref, power_state, self.module.params["state_change_timeout"] |
| 230 |
) |
| 231 |
|
| 232 |
# If state has changed, update vm_params. |
| 233 |
if state_changed: |
| 234 |
self.vm_params["power_state"] = current_state.capitalize() |
| 235 |
|
| 236 |
return state_changed |
| 237 |
|
| 238 |
def wait_for_ip_address(self): |
| 239 |
"""Waits for VM to acquire an IP address.""" |
| 240 |
self.vm_params["guest_metrics"] = wait_for_vm_ip_address( |
| 241 |
self.module, self.vm_ref, self.module.params["state_change_timeout"] |
| 242 |
) |
| 243 |
|
| 244 |
|
| 245 |
def main(): |
| 246 |
argument_spec = xenserver_common_argument_spec() |
| 247 |
argument_spec.update( |
| 248 |
state=dict( |
| 249 |
type="str", |
| 250 |
default="present", |
| 251 |
choices=[ |
| 252 |
"powered-on", |
| 253 |
"powered-off", |
| 254 |
"restarted", |
| 255 |
"shutdown-guest", |
| 256 |
"reboot-guest", |
| 257 |
"suspended", |
| 258 |
"present", |
| 259 |
], |
| 260 |
), |
| 261 |
name=dict(type="str", aliases=["name_label"]), |
| 262 |
uuid=dict(type="str"), |
| 263 |
wait_for_ip_address=dict(type="bool", default=False), |
| 264 |
state_change_timeout=dict(type="int", default=0), |
| 265 |
) |
| 266 |
|
| 267 |
module = AnsibleModule( |
| 268 |
argument_spec=argument_spec, |
| 269 |
supports_check_mode=True, |
| 270 |
required_one_of=[ |
| 271 |
["name", "uuid"], |
| 272 |
], |
| 273 |
) |
| 274 |
|
| 275 |
result = {"failed": False, "changed": False} |
| 276 |
|
| 277 |
# Module will exit with an error message if no VM is found. |
| 278 |
vm = XenServerVM(module) |
| 279 |
|
| 280 |
# Set VM power state. |
| 281 |
if module.params["state"] != "present": |
| 282 |
result["changed"] = vm.set_power_state(module.params["state"]) |
| 283 |
|
| 284 |
if module.params["wait_for_ip_address"]: |
| 285 |
vm.wait_for_ip_address() |
| 286 |
|
| 287 |
result["instance"] = vm.gather_facts() |
| 288 |
|
| 289 |
if result["failed"]: |
| 290 |
module.fail_json(**result) |
| 291 |
else: |
| 292 |
module.exit_json(**result) |
| 293 |
|
| 294 |
|
| 295 |
if __name__ == "__main__": |
| 296 |
main() |