plugins/inventory/serf.py: Use SERF_RPC_* env vars
This makes the Serf inventory plugin use the `SERF_RPC_ADDR` and `SERF_RPC_AUTH` environment variables that the `serf` command-line tool already uses. These can be used to get Serf data from a remote node instead of requiring the ansible control host to be running a serf agent and to be a member of the serf cluster.
This commit is contained in:
parent
30c1a2d861
commit
ce42c66e27
1 changed files with 25 additions and 4 deletions
|
@ -20,10 +20,18 @@
|
||||||
# Dynamic inventory script which lets you use nodes discovered by Serf
|
# Dynamic inventory script which lets you use nodes discovered by Serf
|
||||||
# (https://serfdom.io/).
|
# (https://serfdom.io/).
|
||||||
#
|
#
|
||||||
# Requires host to be a member of a Serf cluster and the `serfclient` Python
|
# Requires the `serfclient` Python module from
|
||||||
# module from https://pypi.python.org/pypi/serfclient
|
# https://pypi.python.org/pypi/serfclient
|
||||||
|
#
|
||||||
|
# Environment variables
|
||||||
|
# ---------------------
|
||||||
|
# - `SERF_RPC_ADDR`
|
||||||
|
# - `SERF_RPC_AUTH`
|
||||||
|
#
|
||||||
|
# These variables are described at https://www.serfdom.io/docs/commands/members.html#_rpc_addr
|
||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
|
import os
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
# https://pypi.python.org/pypi/serfclient
|
# https://pypi.python.org/pypi/serfclient
|
||||||
|
@ -37,9 +45,22 @@ except ImportError:
|
||||||
_key = 'serf'
|
_key = 'serf'
|
||||||
|
|
||||||
|
|
||||||
|
def _serf_client():
|
||||||
|
kwargs = {}
|
||||||
|
|
||||||
|
rpc_addr = os.getenv('SERF_RPC_ADDR')
|
||||||
|
if rpc_addr:
|
||||||
|
kwargs['host'], kwargs['port'] = rpc_addr.split(':')
|
||||||
|
|
||||||
|
rpc_auth = os.getenv('SERF_RPC_AUTH')
|
||||||
|
if rpc_auth:
|
||||||
|
kwargs['rpc_auth'] = rpc_auth
|
||||||
|
|
||||||
|
return SerfClient(**kwargs)
|
||||||
|
|
||||||
|
|
||||||
def get_serf_members_data():
|
def get_serf_members_data():
|
||||||
serf = SerfClient()
|
return _serf_client().members().body['Members']
|
||||||
return serf.members().body['Members']
|
|
||||||
|
|
||||||
|
|
||||||
def get_nodes(data):
|
def get_nodes(data):
|
||||||
|
|
Loading…
Reference in a new issue