From fbc1cd553ca6d083a9801a32fae1dfa40e7b9f67 Mon Sep 17 00:00:00 2001 From: Andrew Rothstein Date: Tue, 14 Oct 2014 07:29:21 -0400 Subject: [PATCH 1/2] an ansible inventory garnered from fleetctl --- plugins/inventory/fleet.py | 107 +++++++++++++++++++++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100755 plugins/inventory/fleet.py diff --git a/plugins/inventory/fleet.py b/plugins/inventory/fleet.py new file mode 100755 index 00000000000..d6d7e4d2925 --- /dev/null +++ b/plugins/inventory/fleet.py @@ -0,0 +1,107 @@ +#!/usr/bin/env python +""" +fleetctl base external inventory script. Automatically finds the IPs of the booted coreos instances and +returns it under the host group 'coreos' +""" + +# Copyright (C) 2014 Andrew Rothstein +# +# 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 . + +# +# Thanks to the vagrant.py inventory script for giving me the basic structure +# of this. +# + +import sys +import subprocess +import re +import string +from optparse import OptionParser +try: + import json +except: + import simplejson as json + +# Options +#------------------------------ + +parser = OptionParser(usage="%prog [options] --list | --host ") +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 +# + +def get_ssh_config() : + configs = [] + for box in list_running_boxes() : + config = get_a_ssh_config(box) + configs.append(config) + return configs + +#list all the running instances in the fleet +def list_running_boxes(): + boxes = [] + for line in subprocess.check_output(["fleetctl", "list-machines"]).split('\n') : + matcher = re.search("[^\s]+[\s]+([^\s]+).+", line) + if matcher and matcher.group(1) != "IP": + boxes.append(matcher.group(1)) + + return boxes + +def get_a_ssh_config(box_name) : + config = {} + config['Host'] = box_name + config['ansible_ssh_user'] = 'core' + config['ansible_python_interpreter'] = '/opt/bin/python' + return config + +# List out servers that vagrant has running +#------------------------------ +if options.list: + ssh_config = get_ssh_config() + hosts = { 'coreos': []} + + for data in ssh_config : + hosts['coreos'].append(data['Host']) + + print json.dumps(hosts) + sys.exit(1) + +# Get out the host details +#------------------------------ +elif options.host: + result = {} + ssh_config = get_ssh_config() + + details = filter(lambda x: (x['Host'] == options.host), ssh_config) + if len(details) > 0: + #pass through the port, in case it's non standard. + result = details[0] + result + + print json.dumps(result) + sys.exit(1) + + +# Print out help +#------------------------------ +else: + parser.print_help() + sys.exit(1) From 4ecaa78c79bd919c7d3c6107025ebff0fc8ef123 Mon Sep 17 00:00:00 2001 From: Andrew Rothstein Date: Fri, 28 Nov 2014 00:00:35 -0500 Subject: [PATCH 2/2] incorporated code review feedback --- plugins/inventory/fleet.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/plugins/inventory/fleet.py b/plugins/inventory/fleet.py index d6d7e4d2925..3267aeb2ea5 100755 --- a/plugins/inventory/fleet.py +++ b/plugins/inventory/fleet.py @@ -39,7 +39,7 @@ except: parser = OptionParser(usage="%prog [options] --list | --host ") parser.add_option('--list', default=False, dest="list", action="store_true", - help="Produce a JSON consumable grouping of Vagrant servers for Ansible") + help="Produce a JSON consumable grouping of servers in your fleet") parser.add_option('--host', default=None, dest="host", help="Generate additional host specific details for given host for Ansible") (options, args) = parser.parse_args() @@ -48,9 +48,9 @@ parser.add_option('--host', default=None, dest="host", # helper functions # -def get_ssh_config() : +def get_ssh_config(): configs = [] - for box in list_running_boxes() : + for box in list_running_boxes(): config = get_a_ssh_config(box) configs.append(config) return configs @@ -58,14 +58,14 @@ def get_ssh_config() : #list all the running instances in the fleet def list_running_boxes(): boxes = [] - for line in subprocess.check_output(["fleetctl", "list-machines"]).split('\n') : + for line in subprocess.check_output(["fleetctl", "list-machines"]).split('\n'): matcher = re.search("[^\s]+[\s]+([^\s]+).+", line) if matcher and matcher.group(1) != "IP": boxes.append(matcher.group(1)) return boxes -def get_a_ssh_config(box_name) : +def get_a_ssh_config(box_name): config = {} config['Host'] = box_name config['ansible_ssh_user'] = 'core' @@ -78,7 +78,7 @@ if options.list: ssh_config = get_ssh_config() hosts = { 'coreos': []} - for data in ssh_config : + for data in ssh_config: hosts['coreos'].append(data['Host']) print json.dumps(hosts)