2013-05-30 04:16:58 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
"""
|
|
|
|
Vagrant external inventory script. Automatically finds the IP of the booted vagrant vm(s), and
|
|
|
|
returns it under the host group 'vagrant'
|
|
|
|
|
|
|
|
Example Vagrant configuration using this script:
|
|
|
|
|
|
|
|
config.vm.provision :ansible do |ansible|
|
|
|
|
ansible.playbook = "./provision/your_playbook.yml"
|
|
|
|
ansible.inventory_file = "./provision/inventory/vagrant.py"
|
|
|
|
ansible.verbose = true
|
|
|
|
end
|
|
|
|
"""
|
|
|
|
|
|
|
|
# Copyright (C) 2013 Mark Mandel <mark@compoundtheory.com>
|
2015-07-21 14:55:25 +00:00
|
|
|
# 2015 Igor Khomyakov <homyakov@gmail.com>
|
2013-05-30 04:16:58 +00:00
|
|
|
#
|
|
|
|
# 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/>.
|
|
|
|
|
|
|
|
#
|
|
|
|
# Thanks to the spacewalk.py inventory script for giving me the basic structure
|
|
|
|
# of this.
|
|
|
|
#
|
|
|
|
|
|
|
|
import sys
|
2015-07-21 14:55:25 +00:00
|
|
|
import os.path
|
2013-05-30 04:16:58 +00:00
|
|
|
import subprocess
|
|
|
|
import re
|
2015-07-21 14:55:25 +00:00
|
|
|
from paramiko import SSHConfig
|
|
|
|
from cStringIO import StringIO
|
2013-05-30 04:16:58 +00:00
|
|
|
from optparse import OptionParser
|
2015-07-21 14:55:25 +00:00
|
|
|
from collections import defaultdict
|
2013-05-30 04:16:58 +00:00
|
|
|
try:
|
|
|
|
import json
|
|
|
|
except:
|
|
|
|
import simplejson as json
|
|
|
|
|
2015-07-21 14:55:25 +00:00
|
|
|
_group = 'vagrant' # a default group
|
|
|
|
_ssh_to_ansible = [('user', 'ansible_ssh_user'),
|
|
|
|
('hostname', 'ansible_ssh_host'),
|
|
|
|
('identityfile', 'ansible_ssh_private_key_file'),
|
|
|
|
('port', 'ansible_ssh_port')]
|
|
|
|
|
2013-05-30 04:16:58 +00:00
|
|
|
# Options
|
2015-07-21 14:55:25 +00:00
|
|
|
# ------------------------------
|
2013-05-30 04:16:58 +00:00
|
|
|
|
|
|
|
parser = OptionParser(usage="%prog [options] --list | --host <machine>")
|
|
|
|
parser.add_option('--list', default=False, dest="list", action="store_true",
|
|
|
|
help="Produce a JSON consumable grouping of Vagrant servers for Ansible")
|
|
|
|
parser.add_option('--host', default=None, dest="host",
|
|
|
|
help="Generate additional host specific details for given host for Ansible")
|
|
|
|
(options, args) = parser.parse_args()
|
|
|
|
|
|
|
|
#
|
|
|
|
# helper functions
|
|
|
|
#
|
|
|
|
|
2015-07-21 14:55:25 +00:00
|
|
|
|
2013-05-30 04:16:58 +00:00
|
|
|
# get all the ssh configs for all boxes in an array of dictionaries.
|
|
|
|
def get_ssh_config():
|
2015-07-21 14:55:25 +00:00
|
|
|
return {k: get_a_ssh_config(k) for k in list_running_boxes()}
|
2013-05-30 04:16:58 +00:00
|
|
|
|
|
|
|
|
2015-07-21 14:55:25 +00:00
|
|
|
# list all the running boxes
|
2013-05-30 04:16:58 +00:00
|
|
|
def list_running_boxes():
|
|
|
|
output = subprocess.check_output(["vagrant", "status"]).split('\n')
|
|
|
|
|
|
|
|
boxes = []
|
|
|
|
|
|
|
|
for line in output:
|
|
|
|
matcher = re.search("([^\s]+)[\s]+running \(.+", line)
|
|
|
|
if matcher:
|
|
|
|
boxes.append(matcher.group(1))
|
|
|
|
|
|
|
|
return boxes
|
|
|
|
|
2015-07-21 14:55:25 +00:00
|
|
|
|
|
|
|
# get the ssh config for a single box
|
2013-05-30 04:16:58 +00:00
|
|
|
def get_a_ssh_config(box_name):
|
|
|
|
"""Gives back a map of all the machine's ssh configurations"""
|
|
|
|
|
2015-07-21 14:55:25 +00:00
|
|
|
output = subprocess.check_output(["vagrant", "ssh-config", box_name])
|
|
|
|
config = SSHConfig()
|
|
|
|
config.parse(StringIO(output))
|
|
|
|
host_config = config.lookup(box_name)
|
2013-05-30 04:16:58 +00:00
|
|
|
|
2015-07-21 14:55:25 +00:00
|
|
|
# man 5 ssh_config:
|
|
|
|
# > It is possible to have multiple identity files ...
|
|
|
|
# > all these identities will be tried in sequence.
|
|
|
|
for id in host_config['identityfile']:
|
|
|
|
if os.path.isfile(id):
|
|
|
|
host_config['identityfile'] = id
|
2013-05-30 04:16:58 +00:00
|
|
|
|
2015-07-21 14:55:25 +00:00
|
|
|
return {v: host_config[k] for k, v in _ssh_to_ansible}
|
2013-05-30 04:16:58 +00:00
|
|
|
|
|
|
|
# List out servers that vagrant has running
|
2015-07-21 14:55:25 +00:00
|
|
|
# ------------------------------
|
2013-05-30 04:16:58 +00:00
|
|
|
if options.list:
|
|
|
|
ssh_config = get_ssh_config()
|
2015-07-21 14:55:25 +00:00
|
|
|
meta = defaultdict(dict)
|
2013-05-30 04:16:58 +00:00
|
|
|
|
2015-07-21 14:55:25 +00:00
|
|
|
for host in ssh_config:
|
|
|
|
meta['hostvars'][host] = ssh_config[host]
|
2013-05-30 04:16:58 +00:00
|
|
|
|
2015-08-28 06:18:13 +00:00
|
|
|
print(json.dumps({_group: list(ssh_config.keys()), '_meta': meta}))
|
2015-03-30 15:39:09 +00:00
|
|
|
sys.exit(0)
|
2013-05-30 04:16:58 +00:00
|
|
|
|
|
|
|
# Get out the host details
|
2015-07-21 14:55:25 +00:00
|
|
|
# ------------------------------
|
2013-05-30 04:16:58 +00:00
|
|
|
elif options.host:
|
2015-08-28 06:18:13 +00:00
|
|
|
print(json.dumps(get_a_ssh_config(options.host)))
|
2015-03-30 15:39:09 +00:00
|
|
|
sys.exit(0)
|
2013-05-30 04:16:58 +00:00
|
|
|
|
|
|
|
# Print out help
|
2015-07-21 14:55:25 +00:00
|
|
|
# ------------------------------
|
2013-05-30 04:16:58 +00:00
|
|
|
else:
|
|
|
|
parser.print_help()
|
2015-03-30 15:39:09 +00:00
|
|
|
sys.exit(0)
|