-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvagrant_inventory.py
executable file
·56 lines (49 loc) · 1.7 KB
/
vagrant_inventory.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
#!/usr/bin/env python
import argparse
import os
import vagrant
import json
# Requirements:
# pip install python-vagrant
def main():
parser = argparse.ArgumentParser(description="Parse list parameter")
parser.add_argument("--list", default=False, action="store_true" , help="Flag to do list hosts")
parser.add_argument("--host", default="", action="store" , help="Flag to get host")
args = parser.parse_args()
stack = vagrant.Vagrant(os.getcwd())
current = stack.conf()
if stack.status()[0].state == "running":
if(args.host):
if(args.host == current["Host"] ):
print json.dumps({
"ansible_host": current["HostName"],
"ansible_port": current["Port"],
"ansible_user": current["User"],
"ansible_ssh_private_key_file": current["IdentityFile"]
}, indent=4)
else:
print {}
if(args.list):
print json.dumps({
"vagrant" : {
"hosts" : [
current["Host"]
],
"vars": {
"ansible_user": current["User"],
"ansible_ssh_private_key_file": current["IdentityFile"]
}
},
"_meta": {
"hostvars": {
current["Host"]: {
"ansible_host": current["HostName"],
"ansible_port": current["Port"]
}
}
}
}, indent=4)
else:
print {}
if __name__ == '__main__':
main()