add lookups to config system (#33026)

* add lookups to config system

use etcd as proof of concept

* some doc updates
pull/32998/merge
Brian Coca 7 years ago committed by GitHub
parent 53cbc9f6ee
commit 6bca5e5a4a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -210,7 +210,7 @@ class PluginLoader:
type_name = get_plugin_class(self.class_name) type_name = get_plugin_class(self.class_name)
# FIXME: expand from just connection and callback # FIXME: expand from just connection and callback
if type_name in ('callback', 'connection', 'inventory'): if type_name in ('callback', 'connection', 'inventory', 'lookup'):
dstring = read_docstring(path, verbose=False, ignore_errors=False) dstring = read_docstring(path, verbose=False, ignore_errors=False)
if dstring.get('doc', False): if dstring.get('doc', False):

@ -19,10 +19,10 @@
from __future__ import (absolute_import, division, print_function) from __future__ import (absolute_import, division, print_function)
__metaclass__ = type __metaclass__ = type
from abc import ABCMeta, abstractmethod from abc import abstractmethod
from ansible.module_utils.six import with_metaclass
from ansible.errors import AnsibleFileNotFound from ansible.errors import AnsibleFileNotFound
from ansible.plugins import AnsiblePlugin
try: try:
from __main__ import display from __main__ import display
@ -33,10 +33,15 @@ except ImportError:
__all__ = ['LookupBase'] __all__ = ['LookupBase']
class LookupBase(with_metaclass(ABCMeta, object)): class LookupBase(AnsiblePlugin):
def __init__(self, loader=None, templar=None, **kwargs): def __init__(self, loader=None, templar=None, **kwargs):
super(LookupBase, self).__init__()
self._loader = loader self._loader = loader
self._templar = templar self._templar = templar
# Backwards compat: self._display isn't really needed, just import the global display and use that. # Backwards compat: self._display isn't really needed, just import the global display and use that.
self._display = display self._display = display

@ -34,30 +34,34 @@ DOCUMENTATION = '''
type: list type: list
elements: string elements: string
required: True required: True
_etcd_url: url:
description: description:
- Environment variable with the url for the etcd server - Environment variable with the url for the etcd server
default: 'http://127.0.0.1:4001' default: 'http://127.0.0.1:4001'
env: env:
- name: ANSIBLE_ETCD_URL - name: ANSIBLE_ETCD_URL
yaml: version:
- key: etcd.url
_etcd_version:
description: description:
- Environment variable with the etcd protocol version - Environment variable with the etcd protocol version
default: 'v1' default: 'v1'
env: env:
- name: ANSIBLE_ETCD_VERSION - name: ANSIBLE_ETCD_VERSION
yaml: validate_certs:
- key: etcd.version description:
- toggle checking that the ssl ceritificates are valid, you normally only want to turn this off with self-signed certs.
default: True
type: boolean
''' '''
EXAMPLES = ''' EXAMPLES = '''
- name: "a value from a locally running etcd" - name: "a value from a locally running etcd"
debug: msg={{ lookup('etcd', 'foo/bar') }} debug: msg={{ lookup('etcd', 'foo/bar') }}
- name: "a values from a folder on a locally running etcd" - name: "values from multiple folders on a locally running etcd"
debug: msg={{ lookup('etcd', 'foo') }} debug: msg={{ lookup('etcd', 'foo', 'bar', 'baz') }}
- name: "since Ansible 2.5 you can set server options inline"
debug: msg="{{ lookup('etcd', 'foo', version='v2', url='http://192.168.0.27:4001') }}"
''' '''
RETURN = ''' RETURN = '''
@ -68,12 +72,7 @@ RETURN = '''
elements: strings elements: strings
''' '''
import os import json
try:
import json
except ImportError:
import simplejson as json
from ansible.plugins.lookup import LookupBase from ansible.plugins.lookup import LookupBase
from ansible.module_utils.urls import open_url from ansible.module_utils.urls import open_url
@ -102,18 +101,10 @@ from ansible.module_utils.urls import open_url
# #
# #
# #
ANSIBLE_ETCD_URL = 'http://127.0.0.1:4001'
if os.getenv('ANSIBLE_ETCD_URL') is not None:
ANSIBLE_ETCD_URL = os.environ['ANSIBLE_ETCD_URL']
ANSIBLE_ETCD_VERSION = 'v1'
if os.getenv('ANSIBLE_ETCD_VERSION') is not None:
ANSIBLE_ETCD_VERSION = os.environ['ANSIBLE_ETCD_VERSION']
class Etcd: class Etcd:
def __init__(self, url=ANSIBLE_ETCD_URL, version=ANSIBLE_ETCD_VERSION, def __init__(self, url, version, validate_certs):
validate_certs=True):
self.url = url self.url = url
self.version = version self.version = version
self.baseurl = '%s/%s/keys' % (self.url, self.version) self.baseurl = '%s/%s/keys' % (self.url, self.version)
@ -143,7 +134,7 @@ class Etcd:
try: try:
r = open_url(url, validate_certs=self.validate_certs) r = open_url(url, validate_certs=self.validate_certs)
data = r.read() data = r.read()
except: except Exception:
return None return None
try: try:
@ -161,7 +152,7 @@ class Etcd:
if 'errorCode' in item: if 'errorCode' in item:
# Here return an error when an unknown entry responds # Here return an error when an unknown entry responds
value = "ENOENT" value = "ENOENT"
except: except Exception:
raise raise
return value return value
@ -171,9 +162,11 @@ class LookupModule(LookupBase):
def run(self, terms, variables, **kwargs): def run(self, terms, variables, **kwargs):
validate_certs = kwargs.get('validate_certs', True) self.set_options(var_options=variables, direct=kwargs)
url = kwargs.get('url', '')
version = kwargs.get('version', '') validate_certs = self._options['validate_certs']
url = self._options['url']
version = self._options['version']
etcd = Etcd(url=url, version=version, validate_certs=validate_certs) etcd = Etcd(url=url, version=version, validate_certs=validate_certs)

Loading…
Cancel
Save