forked from RaymiiOrg/ansible-vmware
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquery.py
executable file
·131 lines (103 loc) · 3.9 KB
/
query.py
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
#!/usr/bin/env python2
# Copyright (C) 2013 - Remy van Elst
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import pysphere
import re
import sys
import argparse
try:
import json
except ImportError:
import simplejson as json
server_fqdn = ""
server_username = ""
server_password = ""
def vcenter_connect(server_fqdn, server_username, server_password):
vserver = pysphere.VIServer()
try:
vserver.connect(server_fqdn, server_username, server_password)
except Exception as error:
print(('Could not connect to vCenter: %s') % (error))
return vserver
def hostinfo(name):
vserver = vcenter_connect(server_fqdn,server_username,server_password)
try:
vm = vserver.get_vm_by_name(name)
except Exception as e:
print("[Error]: %s" % e)
sys.exit(1)
# Inject some variables for all hosts
vars = {
'admin' : 'sysadmin@example.org',
'source_database' : 'VMWare'
}
if 'ldap' in name.lower():
vars['baseDN'] = 'dc=example,dc=org'
print json.dumps(vars, indent=4)
def grouplist():
inventory ={}
inventory['local'] = [ '127.0.0.1' ]
vserver = vcenter_connect(server_fqdn,server_username,server_password)
vms_in_vserver = vserver.get_registered_vms(status='poweredOn')
inventory["no_group"] = {
'hosts' : []
}
for vsphere_vm in vms_in_vserver:
virtual_machine = vserver.get_vm_by_path(vsphere_vm)
virtual_machine_name = virtual_machine.get_property('name')
inventory['no_group']['hosts'].append(virtual_machine_name)
print json.dumps(inventory, indent=4)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('-s', '--server', help='fqdn of vsphere server',
action='store', required='True')
parser.add_argument('-u', '--username', help='your vsphere username',
action='store', required='True')
parser.add_argument('-p', '--password', help='your vsphere password',
action='store')
parser.add_argument('-l', '--list', help='List all guest VMs',
action='store_true')
parser.add_argument('-g', '--guest', help='Print a single guest',
action='store')
parser.add_argument('-n', '--no-ssl-verify',
help="Do not do SSL Cert Validation", action='store_true')
args = parser.parse_args()
if args.no_ssl_verify is True:
import ssl
try:
_create_unverified_https_context = ssl._create_unverified_context
except AttributeError:
# Legacy Python that doesn't verify HTTPS certificates by default
pass
else:
# Handle target environment that doesn't support HTTPS verification
ssl._create_default_https_context = _create_unverified_https_context
if args.server:
server_fqdn = args.server
if args.username:
server_username = args.username
if args.password:
server_password = args.password
else:
import getpass
server_password = getpass.getpass()
if (server_fqdn, server_username, server_password):
if args.list:
grouplist()
elif args.guest:
hostinfo(args.guest)
else:
parser.print_help()
sys.exit(1)
else:
parser.print_help()
sys.exit(1)