diff --git a/examples/ansible.cfg b/examples/ansible.cfg index a9306a61193..2a1e6687125 100644 --- a/examples/ansible.cfg +++ b/examples/ansible.cfg @@ -102,6 +102,9 @@ filter_plugins = /usr/share/ansible_plugins/filter_plugins # set to 1 if you don't want colors, or export ANSIBLE_NOCOLOR=1 #nocolor = 1 +# default URL for `etcd' lookup plugin +etcd_url = 'http://127.0.0.1:4001' + [paramiko_connection] # uncomment this line to cause the paramiko connection plugin to not record new host diff --git a/lib/ansible/constants.py b/lib/ansible/constants.py index 8007b36915d..76889d1faf0 100644 --- a/lib/ansible/constants.py +++ b/lib/ansible/constants.py @@ -152,6 +152,9 @@ PARAMIKO_PTY = get_config(p, 'paramiko_connection', 'pty', 'AN # characters included in auto-generated passwords DEFAULT_PASSWORD_CHARS = ascii_letters + digits + ".,:-_" +# LOOKUP PLUGIN RELATED +ANSIBLE_ETCD_URL = get_config(p, DEFAULTS, 'etcd_url', 'ANSIBLE_ETCD_URL', 'http://127.0.0.1:4001') + # non-configurable things DEFAULT_SUDO_PASS = None DEFAULT_REMOTE_PASS = None diff --git a/lib/ansible/runner/lookup_plugins/etcd.py b/lib/ansible/runner/lookup_plugins/etcd.py new file mode 100644 index 00000000000..63726cdd5fe --- /dev/null +++ b/lib/ansible/runner/lookup_plugins/etcd.py @@ -0,0 +1,73 @@ +# (c) 2013, Jan-Piet Mens +# +# 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 . + +from ansible import utils, errors, constants +import os +import urllib2 +try: + import json +except ImportError: + import simplejson as json + +class etcd(): + def __init__(self, url='http://127.0.0.1:4001'): + self.url = url + self.baseurl = '%s/v1/keys' % (self.url) + + def get(self, key): + url = "%s/%s" % (self.baseurl, key) + + data = None + value = "" + try: + r = urllib2.urlopen(url) + data = r.read() + except: + return value + + try: + # {"action":"get","key":"/name","value":"Jane Jolie","index":5} + item = json.loads(data) + if 'value' in item: + value = item['value'] + if 'errorCode' in item: + value = "ENOENT" + except: + raise + pass + + return value + +class LookupModule(object): + + def __init__(self, basedir=None, **kwargs): + self.basedir = basedir + self.etcd = etcd(constants.ANSIBLE_ETCD_URL) + + def run(self, terms, inject=None, **kwargs): + + terms = utils.listify_lookup_plugin_terms(terms, self.basedir, inject) + + if isinstance(terms, basestring): + terms = [ terms ] + + ret = [] + for term in terms: + key = term.split()[0] + value = self.etcd.get(key) + ret.append(value) + return ret