-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathVagrant2Inventory.py
executable file
·127 lines (104 loc) · 3.06 KB
/
Vagrant2Inventory.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
#!/usr/bin/env python
try:
import re
import sys
import getopt
import pprint
import commands
except ImportError as e:
print "Import error:",e
sys.exit(2)
def usage():
print "Usage: Vagrant2Inventory -a ipaddr -s regexp -u user -p pass -v"
print ""
print " -a: specify host IP address in VirtualBox environment (default: 10.0.2.2)"
print " -s: skip hosts matched by regexp"
print " -u: SSH username (default: vagrant)"
print " -p: SSH password (default: vagrant)"
print " -v: verbose"
def getOptions():
try:
options, args = getopt.getopt(sys.argv[1:], "a:s:u:p:v", ["address=", "skip=", "username=", "password="])
except getopt.GetoptError as err:
# print help information and exit:
print str(err) # will print something like "option -a not recognized"
usage()
sys.exit(2)
opts = { 'username': 'vagrant', 'password': 'vagrant' }
for opt,arg in options:
if opt in ("-a","-address"):
opts['address'] = arg
elif opt in ("-u","-username"):
opts['username'] = arg
elif opt in ("-p","-password"):
opts['password'] = arg
elif opt in ("-s","-skip"):
opts['skip'] = arg
elif opt == "-v":
opts['verbose'] = True
elif opt == "-h":
usage()
sys.exit(0)
else:
assert False,format("Unhandled option {0}",opt)
return opts
def getSSHConfig():
try:
results = commands.getstatusoutput("vagrant ssh-config")
except:
print "Failed: cannot execute ssh-config"
sys.exit(1)
if (results[0]):
print "vagrant ssh-config failed"
print ""
print results[1]
sys.exit(1)
return results[1]
def parseSSHConfig(c,o):
nodes = []
node = {}
def addPreviousNode(l,n):
if "host" in n: l.append(n)
host = re.compile("Host\s+(.+)$")
addr = re.compile("\s*HostName\s+(.+)$") #[0-9a-fA-F.]
port = re.compile("\s*Port\s+([0-9]+)$")
for line in c.split("\n"):
m_host = host.match(line)
m_addr = addr.match(line)
m_port = port.match(line)
if m_host:
addPreviousNode(nodes,node)
node = { 'host' : m_host.groups()[0]}
elif m_addr:
node['addr'] = m_addr.groups()[0]
elif m_port:
node['port'] = m_port.groups()[0]
addPreviousNode(nodes,node)
return nodes
def printInventory(nodeList,opts):
if "skip" in opts:
skipMatch = re.compile(opts["skip"])
for node in nodeList:
d = node.copy()
d.update(opts)
if (d['addr'] == "127.0.0.1") and ("address" in d):
d['addr'] = d['address']
skip = None
if "skip" in opts:
skip = skipMatch.match(d['host'])
if not(skip):
print "{host:<10} ansible_host={addr:<16} ansible_port={port:<5} ansible_user={username} ansible_ssh_pass={password}".format(**d)
def main():
opts = getOptions()
if 'verbose' in opts: print "Executing vagrant ssh-config"
sshconf = getSSHConfig()
if 'verbose' in opts:
print "Returned by ssh-config:"
print sshconf
print
nodeList = parseSSHConfig(sshconf,opts)
if 'verbose' in opts:
print "Parsing results:"
print pprint.pprint(nodeList)
printInventory(nodeList,opts)
main()