forked from shankerbalan/ansible
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathxenserver_facts
executable file
·165 lines (135 loc) · 4.78 KB
/
xenserver_facts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/usr/bin/env python
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
DOCUMENTATION = '''
---
module: xenserver_facts
short_description: Gathers facts about XenServer hosts
version_added: "1.0"
options: {}
description:
- This module fetches data from the XenServer hosts.
notes:
- Parameters to filter on xenserver_facts may be added later.
author: "Shanker Balan <[email protected]>"
'''
EXAMPLES = '''
# Conditional example
- name: Gather facts
action: xenserver_facts
- name: Conditional
action: debug msg="This host has uuid {{ ansible_host_uuid }}"
'''
import XenAPI
import socket
import subprocess
import re
hostname = socket.gethostname()
session = XenAPI.xapi_local()
session.xenapi.login_with_password("root", "")
try:
import json
except ImportError:
import simplejson as json
def get_host_uuid(session, hostname):
hosts = session.xenapi.host.get_by_name_label(hostname)
for h in hosts:
host_uuid = session.xenapi.host.get_uuid(h)
return host_uuid
def get_master_uuid(session):
pools = session.xenapi.pool.get_all()
for p in pools:
opaque_ref = session.xenapi.pool.get_master(p)
master_uuid = session.xenapi.host.get_uuid(opaque_ref)
if master_uuid == "":
print "failed to get master"
else:
return master_uuid
def get_patch_list(session):
pool_patches = session.xenapi.pool_patch.get_all()
facts = {}
for p in pool_patches:
name_label = session.xenapi.pool_patch.get_name_label(p)
patch_facts = {}
patch_facts['uuid'] = session.xenapi.pool_patch.get_uuid(p)
patch_facts['name_description'] = session.xenapi.pool_patch.get_name_description(p)
patch_facts['after_apply_guidance'] = session.xenapi.pool_patch.get_after_apply_guidance(p)
hosts = session.xenapi.pool_patch.get_host_patches(p)
for h in hosts:
opaque_ref = session.xenapi.host_patch.get_host(h)
host_name = session.xenapi.host.get_name_label(opaque_ref)
if host_name == hostname:
patch_facts['applied'] = session.xenapi.host_patch.get_applied(h)
facts[name_label] = patch_facts
return facts
def get_dom0_mem():
cmd = '/opt/xensource/libexec/xen-cmdline'
p = subprocess.Popen([cmd, "--get-xen", "dom0_mem"], stdout=subprocess.PIPE)
string, err = p.communicate()
string = string.strip()
dom0_mem = {}
mem_settings = re.split('=', string)[1]
dom0_mem["min"] = re.split(",", mem_settings)[0]
dom0_mem["max"] = re.split(":", mem_settings)[1]
return dom0_mem
def get_network_list(session):
networks = session.xenapi.network.get_all()
network_list = {}
for n in networks:
facts = {}
bridge = session.xenapi.network.get_bridge(n)
facts["name_label"] = session.xenapi.network.get_name_label(n)
facts["uuid"] = session.xenapi.network.get_uuid(n)
facts["name_description"] = session.xenapi.network.get_name_description(n)
network_list[bridge] = facts
return network_list
def get_pif_list(session):
interfaces = session.xenapi.PIF.get_all()
pif_list = {}
for f in interfaces:
facts = {}
device = session.xenapi.PIF.get_device(f)
facts["uuid"] = session.xenapi.PIF.get_uuid(f)
facts["ip"] = session.xenapi.PIF.get_IP(f)
facts["netmask"] = session.xenapi.PIF.get_netmask(f)
facts["gateway"] = session.xenapi.PIF.get_gateway(f)
pif_list[device] = facts
return pif_list
def main():
facts = {}
facts['hostname'] = hostname
facts['host_uuid'] = get_host_uuid(session,hostname)
facts['master_uuid'] = get_master_uuid(session)
facts['hotfixes'] = get_patch_list(session)
facts['dom0_mem'] = get_dom0_mem()
facts['networks'] = get_network_list(session)
facts['pifs'] = get_pif_list(session)
session.xenapi.session.logout()
result = {}
result['xenserver_facts'] = facts
ansible_xenserver_facts = {}
ansible_xenserver_facts['ansible_facts'] = result
module = AnsibleModule(
argument_spec = dict(),
supports_check_mode = True,
)
module.exit_json(**ansible_xenserver_facts)
# this is magic, see lib/ansible/module_common.py
#<<INCLUDE_ANSIBLE_MODULE_COMMON>>
main()
# vi: set ts=2 sw=2 expandtab