#!/usr/bin/env python

# (c) 2015, Marc Abramowitz <marca@surveymonkey.com>
#
# This file is part of Ansible.
#
# Ansible 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.
#
# Ansible 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 Ansible. If not, see <http://www.gnu.org/licenses/>.

# Dynamic inventory script which lets you use nodes discovered by Serf
# (https://serfdom.io/).
#
# Requires host to be a member of a Serf cluster and the `serfclient` Python
# module from https://pypi.python.org/pypi/serfclient

import argparse
import sys

# https://pypi.python.org/pypi/serfclient
from serfclient.client import SerfClient

try:
    import json
except ImportError:
    import simplejson as json

_key = 'serf'


def get_serf_members_data():
    serf = SerfClient()
    return serf.members().body['Members']


def get_nodes(data):
    return [node['Name'] for node in data]


def get_meta(data):
    meta = {'hostvars': {}}
    for node in data:
        meta['hostvars'][node['Name']] = node['Tags']
    return meta


def print_list():
    data = get_serf_members_data()
    nodes = get_nodes(data)
    meta = get_meta(data)
    print(json.dumps({_key: nodes, '_meta': meta}))


def print_host(host):
    data = get_serf_members_data()
    meta = get_meta(data)
    print(json.dumps(meta['hostvars'][host]))


def get_args(args_list):
    parser = argparse.ArgumentParser(
        description='ansible inventory script reading from serf cluster')
    mutex_group = parser.add_mutually_exclusive_group(required=True)
    help_list = 'list all hosts from serf cluster'
    mutex_group.add_argument('--list', action='store_true', help=help_list)
    help_host = 'display variables for a host'
    mutex_group.add_argument('--host', help=help_host)
    return parser.parse_args(args_list)


def main(args_list):
    args = get_args(args_list)
    if args.list:
        print_list()
    if args.host:
        print_host(args.host)


if __name__ == '__main__':
    main(sys.argv[1:])

@@ -836,50 +836,8 @@
 res 
-host to be a member of a Serf cluster and 
 the 
@@ -855,18 +855,16 @@
 %60 Python
-%0A#
  module 
@@ -867,16 +867,18 @@
 ule from
+%0A#
  https:/
@@ -909,16 +909,207 @@
 rfclient
+%0A#%0A# Environment variables%0A# ---------------------%0A#   - %60SERF_RPC_ADDR%60%0A#   - %60SERF_RPC_AUTH%60%0A#%0A# These variables are described at https://www.serfdom.io/docs/commands/members.html#_rpc_addr
 %0A%0Aimport
@@ -1118,16 +1118,26 @@
 rgparse%0A
+import os%0A
 import s
@@ -1320,56 +1320,322 @@
 def 
-get
 _serf_
-members_data():%0A    serf = SerfClient()
+client():%0A    kwargs = %7B%7D%0A%0A    rpc_addr = os.getenv('SERF_RPC_ADDR')%0A    if rpc_addr:%0A        kwargs%5B'host'%5D, kwargs%5B'port'%5D = rpc_addr.split(':')%0A%0A    rpc_auth = os.getenv('SERF_RPC_AUTH')%0A    if rpc_auth:%0A        kwargs%5B'rpc_auth'%5D = rpc_auth%0A%0A    return SerfClient(**kwargs)%0A%0A%0Adef get_serf_members_data():
 %0A   
@@ -1642,20 +1642,30 @@
  return 
+_
 serf
+_client()
 .members

Use SERF_RPC_* env vars