| 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_info |
| 11 |
short_description: Gathers information for virtual machines running on Citrix Hypervisor/XenServer host or pool |
| 12 |
description: This module can be used to gather essential VM facts. |
| 13 |
author: |
| 14 |
- Bojan Vitnik (@bvitnik) <[email protected]> |
| 15 |
notes: |
| 16 |
- Minimal supported version of XenServer is 5.6. |
| 17 |
- Module was tested with XenServer 6.5, 7.1, 7.2, 7.6, Citrix Hypervisor 8.0, XCP-ng 7.6 and 8.0. |
| 18 |
- 'To acquire XenAPI Python library, just run C(pip install XenAPI) on your Ansible Control Node. The library can also be |
| 19 |
found inside Citrix Hypervisor/XenServer SDK (downloadable from Citrix website). Copy the C(XenAPI.py) file from the SDK |
| 20 |
to your Python site-packages on your Ansible Control Node to use it. Latest version of the library can also be acquired |
| 21 |
from GitHub: U(https://raw.githubusercontent.com/xapi-project/xen-api/master/scripts/examples/python/XenAPI/XenAPI.py).' |
| 22 |
- If no scheme is specified in C(hostname), module defaults to C(http://) because C(https://) is problematic in most setups. |
| 23 |
Make sure you are accessing XenServer host in trusted environment or use C(https://) scheme explicitly. |
| 24 |
- To use C(https://) scheme for C(hostname) you have to either import host certificate to your OS certificate store or use |
| 25 |
O(validate_certs=no) which requires XenAPI library from XenServer 7.2 SDK or newer and Python 2.7.9 or newer. |
| 26 |
requirements: |
| 27 |
- XenAPI |
| 28 |
options: |
| 29 |
name: |
| 30 |
description: |
| 31 |
- Name of the VM to gather facts from. |
| 32 |
- VMs running on XenServer do not necessarily have unique names. The module fails if multiple VMs with same name are |
| 33 |
found. |
| 34 |
- In case of multiple VMs with same name, use O(uuid) to uniquely specify VM to manage. |
| 35 |
- This parameter is case sensitive. |
| 36 |
type: str |
| 37 |
aliases: [name_label] |
| 38 |
uuid: |
| 39 |
description: |
| 40 |
- UUID of the VM to gather fact of. This is XenServer's unique identifier. |
| 41 |
- It is required if name is not unique. |
| 42 |
type: str |
| 43 |
extends_documentation_fragment: |
| 44 |
- community.general._xenserver.documentation |
| 45 |
- community.general._attributes |
| 46 |
- community.general._attributes.info_module |
| 47 |
""" |
| 48 |
|
| 49 |
EXAMPLES = r""" |
| 50 |
- name: Gather facts |
| 51 |
community.general.xenserver_guest_info: |
| 52 |
hostname: "{{ xenserver_hostname }}" |
| 53 |
username: "{{ xenserver_username }}" |
| 54 |
password: "{{ xenserver_password }}" |
| 55 |
name: testvm_11 |
| 56 |
delegate_to: localhost |
| 57 |
register: facts |
| 58 |
""" |
| 59 |
|
| 60 |
RETURN = r""" |
| 61 |
instance: |
| 62 |
description: Metadata about the VM. |
| 63 |
returned: always |
| 64 |
type: dict |
| 65 |
sample: |
| 66 |
{ |
| 67 |
"cdrom": { |
| 68 |
"type": "none" |
| 69 |
}, |
| 70 |
"customization_agent": "native", |
| 71 |
"disks": [ |
| 72 |
{ |
| 73 |
"name": "testvm_11-0", |
| 74 |
"name_desc": "", |
| 75 |
"os_device": "xvda", |
| 76 |
"size": 42949672960, |
| 77 |
"sr": "Local storage", |
| 78 |
"sr_uuid": "0af1245e-bdb0-ba33-1446-57a962ec4075", |
| 79 |
"vbd_userdevice": "0" |
| 80 |
}, |
| 81 |
{ |
| 82 |
"name": "testvm_11-1", |
| 83 |
"name_desc": "", |
| 84 |
"os_device": "xvdb", |
| 85 |
"size": 42949672960, |
| 86 |
"sr": "Local storage", |
| 87 |
"sr_uuid": "0af1245e-bdb0-ba33-1446-57a962ec4075", |
| 88 |
"vbd_userdevice": "1" |
| 89 |
} |
| 90 |
], |
| 91 |
"domid": "56", |
| 92 |
"folder": "", |
| 93 |
"hardware": { |
| 94 |
"memory_mb": 8192, |
| 95 |
"num_cpu_cores_per_socket": 2, |
| 96 |
"num_cpus": 4 |
| 97 |
}, |
| 98 |
"home_server": "", |
| 99 |
"is_template": false, |
| 100 |
"name": "testvm_11", |
| 101 |
"name_desc": "", |
| 102 |
"networks": [ |
| 103 |
{ |
| 104 |
"gateway": "192.168.0.254", |
| 105 |
"gateway6": "fc00::fffe", |
| 106 |
"ip": "192.168.0.200", |
| 107 |
"ip6": [ |
| 108 |
"fe80:0000:0000:0000:e9cb:625a:32c5:c291", |
| 109 |
"fc00:0000:0000:0000:0000:0000:0000:0001" |
| 110 |
], |
| 111 |
"mac": "ba:91:3a:48:20:76", |
| 112 |
"mtu": "1500", |
| 113 |
"name": "Pool-wide network associated with eth1", |
| 114 |
"netmask": "255.255.255.128", |
| 115 |
"prefix": "25", |
| 116 |
"prefix6": "64", |
| 117 |
"vif_device": "0" |
| 118 |
} |
| 119 |
], |
| 120 |
"other_config": { |
| 121 |
"base_template_name": "Windows Server 2016 (64-bit)", |
| 122 |
"import_task": "OpaqueRef:e43eb71c-45d6-5351-09ff-96e4fb7d0fa5", |
| 123 |
"install-methods": "cdrom", |
| 124 |
"instant": "true", |
| 125 |
"mac_seed": "f83e8d8a-cfdc-b105-b054-ef5cb416b77e" |
| 126 |
}, |
| 127 |
"platform": { |
| 128 |
"acpi": "1", |
| 129 |
"apic": "true", |
| 130 |
"cores-per-socket": "2", |
| 131 |
"device_id": "0002", |
| 132 |
"hpet": "true", |
| 133 |
"nx": "true", |
| 134 |
"pae": "true", |
| 135 |
"timeoffset": "-25200", |
| 136 |
"vga": "std", |
| 137 |
"videoram": "8", |
| 138 |
"viridian": "true", |
| 139 |
"viridian_reference_tsc": "true", |
| 140 |
"viridian_time_ref_count": "true" |
| 141 |
}, |
| 142 |
"state": "poweredon", |
| 143 |
"uuid": "e3c0b2d5-5f05-424e-479c-d3df8b3e7cda", |
| 144 |
"xenstore_data": { |
| 145 |
"vm-data": "" |
| 146 |
} |
| 147 |
} |
| 148 |
""" |
| 149 |
|
| 150 |
|
| 151 |
from ansible.module_utils.basic import AnsibleModule |
| 152 |
|
| 153 |
from ansible_collections.community.general.plugins.module_utils._xenserver import ( |
| 154 |
XenServerObject, |
| 155 |
gather_vm_facts, |
| 156 |
gather_vm_params, |
| 157 |
get_object_ref, |
| 158 |
xenserver_common_argument_spec, |
| 159 |
) |
| 160 |
|
| 161 |
|
| 162 |
class XenServerVM(XenServerObject): |
| 163 |
"""Class for managing XenServer VM. |
| 164 |
|
| 165 |
Attributes: |
| 166 |
vm_ref (str): XAPI reference to VM. |
| 167 |
vm_params (dict): A dictionary with VM parameters as returned |
| 168 |
by gather_vm_params() function. |
| 169 |
""" |
| 170 |
|
| 171 |
def __init__(self, module): |
| 172 |
"""Inits XenServerVM using module parameters. |
| 173 |
|
| 174 |
Args: |
| 175 |
module: Reference to AnsibleModule object. |
| 176 |
""" |
| 177 |
super().__init__(module) |
| 178 |
|
| 179 |
self.vm_ref = get_object_ref( |
| 180 |
self.module, |
| 181 |
self.module.params["name"], |
| 182 |
self.module.params["uuid"], |
| 183 |
obj_type="VM", |
| 184 |
fail=True, |
| 185 |
msg_prefix="VM search: ", |
| 186 |
) |
| 187 |
self.gather_params() |
| 188 |
|
| 189 |
def gather_params(self): |
| 190 |
"""Gathers all VM parameters available in XAPI database.""" |
| 191 |
self.vm_params = gather_vm_params(self.module, self.vm_ref) |
| 192 |
|
| 193 |
def gather_facts(self): |
| 194 |
"""Gathers and returns VM facts.""" |
| 195 |
return gather_vm_facts(self.module, self.vm_params) |
| 196 |
|
| 197 |
|
| 198 |
def main(): |
| 199 |
argument_spec = xenserver_common_argument_spec() |
| 200 |
argument_spec.update( |
| 201 |
name=dict(type="str", aliases=["name_label"]), |
| 202 |
uuid=dict(type="str"), |
| 203 |
) |
| 204 |
|
| 205 |
module = AnsibleModule( |
| 206 |
argument_spec=argument_spec, |
| 207 |
supports_check_mode=True, |
| 208 |
required_one_of=[ |
| 209 |
["name", "uuid"], |
| 210 |
], |
| 211 |
) |
| 212 |
|
| 213 |
result = {"failed": False, "changed": False} |
| 214 |
|
| 215 |
# Module will exit with an error message if no VM is found. |
| 216 |
vm = XenServerVM(module) |
| 217 |
|
| 218 |
# Gather facts. |
| 219 |
result["instance"] = vm.gather_facts() |
| 220 |
|
| 221 |
if result["failed"]: |
| 222 |
module.fail_json(**result) |
| 223 |
else: |
| 224 |
module.exit_json(**result) |
| 225 |
|
| 226 |
|
| 227 |
if __name__ == "__main__": |
| 228 |
main() |