| 1 |
#!/usr/bin/python |
| 2 |
# |
| 3 |
# Copyright Ansible Project |
| 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_facts |
| 11 |
short_description: Get facts reported on xenserver |
| 12 |
description: |
| 13 |
- Reads data out of XenAPI, can be used instead of multiple C(xe) commands. |
| 14 |
author: |
| 15 |
- Andy Hill (@andyhky) |
| 16 |
- Tim Rupp (@caphrim007) |
| 17 |
- Robin Lee (@cheese) |
| 18 |
extends_documentation_fragment: |
| 19 |
- community.general._attributes |
| 20 |
- community.general._attributes.facts |
| 21 |
- community.general._attributes.facts_module |
| 22 |
attributes: |
| 23 |
check_mode: |
| 24 |
version_added: 3.3.0 |
| 25 |
# This was backported to 2.5.4 and 1.3.11 as well, since this was a bugfix |
| 26 |
options: {} |
| 27 |
""" |
| 28 |
|
| 29 |
EXAMPLES = r""" |
| 30 |
- name: Gather facts from xenserver |
| 31 |
community.general.xenserver_facts: |
| 32 |
|
| 33 |
- name: Print running VMs |
| 34 |
ansible.builtin.debug: |
| 35 |
msg: "{{ item }}" |
| 36 |
with_items: "{{ xs_vms.keys() }}" |
| 37 |
when: xs_vms[item]['power_state'] == "Running" |
| 38 |
|
| 39 |
# Which will print: |
| 40 |
# |
| 41 |
# TASK: [Print running VMs] *********************************************************** |
| 42 |
# skipping: [10.13.0.22] => (item=CentOS 4.7 (32-bit)) |
| 43 |
# ok: [10.13.0.22] => (item=Control domain on host: 10.0.13.22) => { |
| 44 |
# "item": "Control domain on host: 10.0.13.22", |
| 45 |
# "msg": "Control domain on host: 10.0.13.22" |
| 46 |
# } |
| 47 |
""" |
| 48 |
|
| 49 |
|
| 50 |
HAVE_XENAPI = False |
| 51 |
try: |
| 52 |
import XenAPI |
| 53 |
|
| 54 |
HAVE_XENAPI = True |
| 55 |
except ImportError: |
| 56 |
pass |
| 57 |
|
| 58 |
from ansible.module_utils import distro |
| 59 |
from ansible.module_utils.basic import AnsibleModule |
| 60 |
|
| 61 |
|
| 62 |
class XenServerFacts: |
| 63 |
def __init__(self): |
| 64 |
self.codes = { |
| 65 |
"5.5.0": "george", |
| 66 |
"5.6.100": "oxford", |
| 67 |
"6.0.0": "boston", |
| 68 |
"6.1.0": "tampa", |
| 69 |
"6.2.0": "clearwater", |
| 70 |
} |
| 71 |
|
| 72 |
@property |
| 73 |
def version(self): |
| 74 |
result = distro.linux_distribution()[1] |
| 75 |
return result |
| 76 |
|
| 77 |
@property |
| 78 |
def codename(self): |
| 79 |
if self.version in self.codes: |
| 80 |
result = self.codes[self.version] |
| 81 |
else: |
| 82 |
result = None |
| 83 |
|
| 84 |
return result |
| 85 |
|
| 86 |
|
| 87 |
def get_xenapi_session(): |
| 88 |
session = XenAPI.xapi_local() |
| 89 |
session.xenapi.login_with_password("", "") |
| 90 |
return session |
| 91 |
|
| 92 |
|
| 93 |
def get_networks(session): |
| 94 |
recs = session.xenapi.network.get_all_records() |
| 95 |
networks = change_keys(recs, key="name_label") |
| 96 |
return networks |
| 97 |
|
| 98 |
|
| 99 |
def get_pifs(session): |
| 100 |
recs = session.xenapi.PIF.get_all_records() |
| 101 |
pifs = change_keys(recs, key="uuid") |
| 102 |
xs_pifs = {} |
| 103 |
devicenums = range(0, 7) |
| 104 |
for pif in pifs.values(): |
| 105 |
for eth in devicenums: |
| 106 |
interface_name = f"eth{eth}" |
| 107 |
bond_name = interface_name.replace("eth", "bond") |
| 108 |
if pif["device"] == interface_name: |
| 109 |
xs_pifs[interface_name] = pif |
| 110 |
elif pif["device"] == bond_name: |
| 111 |
xs_pifs[bond_name] = pif |
| 112 |
return xs_pifs |
| 113 |
|
| 114 |
|
| 115 |
def get_vlans(session): |
| 116 |
recs = session.xenapi.VLAN.get_all_records() |
| 117 |
return change_keys(recs, key="tag") |
| 118 |
|
| 119 |
|
| 120 |
def change_keys(recs, key="uuid", filter_func=None): |
| 121 |
""" |
| 122 |
Take a xapi dict, and make the keys the value of recs[ref][key]. |
| 123 |
|
| 124 |
Preserves the ref in rec['ref'] |
| 125 |
|
| 126 |
""" |
| 127 |
new_recs = {} |
| 128 |
|
| 129 |
for ref, rec in recs.items(): |
| 130 |
if filter_func is not None and not filter_func(rec): |
| 131 |
continue |
| 132 |
|
| 133 |
for param_name, param_value in rec.items(): |
| 134 |
# param_value may be of type xmlrpc.client.DateTime, |
| 135 |
# which is not simply convertible to str. |
| 136 |
# Use 'value' attr to get the str value, |
| 137 |
# following an example in xmlrpc.client.DateTime document |
| 138 |
if hasattr(param_value, "value"): |
| 139 |
rec[param_name] = param_value.value |
| 140 |
new_recs[rec[key]] = rec |
| 141 |
new_recs[rec[key]]["ref"] = ref |
| 142 |
|
| 143 |
return new_recs |
| 144 |
|
| 145 |
|
| 146 |
def get_host(session): |
| 147 |
"""Get the host""" |
| 148 |
host_recs = session.xenapi.host.get_all() |
| 149 |
# We only have one host, so just return its entry |
| 150 |
return session.xenapi.host.get_record(host_recs[0]) |
| 151 |
|
| 152 |
|
| 153 |
def get_vms(session): |
| 154 |
recs = session.xenapi.VM.get_all_records() |
| 155 |
if not recs: |
| 156 |
return None |
| 157 |
vms = change_keys(recs, key="name_label") |
| 158 |
return vms |
| 159 |
|
| 160 |
|
| 161 |
def get_srs(session): |
| 162 |
recs = session.xenapi.SR.get_all_records() |
| 163 |
if not recs: |
| 164 |
return None |
| 165 |
srs = change_keys(recs, key="name_label") |
| 166 |
return srs |
| 167 |
|
| 168 |
|
| 169 |
def main(): |
| 170 |
module = AnsibleModule({}, supports_check_mode=True) |
| 171 |
|
| 172 |
if not HAVE_XENAPI: |
| 173 |
module.fail_json(changed=False, msg="python xen api required for this module") |
| 174 |
|
| 175 |
obj = XenServerFacts() |
| 176 |
try: |
| 177 |
session = get_xenapi_session() |
| 178 |
except XenAPI.Failure as e: |
| 179 |
module.fail_json(msg=str(e)) |
| 180 |
|
| 181 |
data = {"xenserver_version": obj.version, "xenserver_codename": obj.codename} |
| 182 |
|
| 183 |
xs_networks = get_networks(session) |
| 184 |
xs_pifs = get_pifs(session) |
| 185 |
xs_vlans = get_vlans(session) |
| 186 |
xs_vms = get_vms(session) |
| 187 |
xs_srs = get_srs(session) |
| 188 |
|
| 189 |
if xs_vlans: |
| 190 |
data["xs_vlans"] = xs_vlans |
| 191 |
if xs_pifs: |
| 192 |
data["xs_pifs"] = xs_pifs |
| 193 |
if xs_networks: |
| 194 |
data["xs_networks"] = xs_networks |
| 195 |
|
| 196 |
if xs_vms: |
| 197 |
data["xs_vms"] = xs_vms |
| 198 |
|
| 199 |
if xs_srs: |
| 200 |
data["xs_srs"] = xs_srs |
| 201 |
|
| 202 |
module.exit_json(ansible_facts=data) |
| 203 |
|
| 204 |
|
| 205 |
if __name__ == "__main__": |
| 206 |
main() |