import os
import urllib2
import json
import sys
from urlparse import urljoin

from ansible import utils, errors
from ansible.utils import template

class LookupModule(object):

    def __init__(self, basedir=None, **kwargs):
        self.basedir = basedir

    def run(self, terms, inject=None, **kwargs):

        try:
            terms = template.template(self.basedir, terms, inject)
        except Exception, e:
            pass

        url = os.getenv('VAULT_ADDR')
        if not url:
            raise errors.AnsibleError('VAULT_ADDR environment variable is missing')

        token = os.getenv('VAULT_TOKEN')
        if not token:
            raise errors.AnsibleError('VAULT_TOKEN environment variable is missing')

        request_url = urljoin(url, "v1/%s" % (terms))
        try:
            headers = { 'X-Vault-Token' : token }
            req = urllib2.Request(request_url, None, headers)
            response = urllib2.urlopen(req)
        except urllib2.HTTPError as e:
            raise errors.AnsibleError('Unable to read %s from vault: %s' % (terms, e))
        except:
            raise errors.AnsibleError('Unable to read %s from vault' % terms)
        result = json.loads(response.read())
        return [result['data']['value']]

@@ -83,24 +83,31 @@
  ansible
+.errors
  import 
 utils, e
@@ -102,21 +102,20 @@
 ort 
-utils, e
+AnsibleE
 rror
-s
 %0Afro
@@ -128,138 +128,74 @@
 ble.
-utils import template%0A%0Aclass LookupModule(object):%0A%0A    def __init__(self, basedir=None, **kwargs):%0A        self.basedir = basedir
+plugins.lookup import LookupBase%0A%0A%0Aclass LookupModule(LookupBase):
 %0A%0A  
@@ -221,19 +221,17 @@
 ms, 
-inject=None
+variables
 , **
@@ -239,25 +239,24 @@
 wargs):%0A
-%0A
         
 try:%0A   
@@ -251,125 +251,22 @@
     
-try:%0A            terms = template.template(self.basedir, terms, inject)%0A        except Exception, e:%0A            pass
+key = terms%5B0%5D
 %0A%0A  
@@ -331,39 +331,32 @@
           raise 
-errors.
 AnsibleError('VA
@@ -472,39 +472,32 @@
           raise 
-errors.
 AnsibleError('VA
@@ -587,21 +587,19 @@
 /%25s%22 %25 (
-terms
+key
 ))%0A     
@@ -811,39 +811,32 @@
           raise 
-errors.
 AnsibleError('Un
@@ -870,21 +870,19 @@
  %25s' %25 (
-terms
+key
 , e))%0A  
@@ -917,15 +917,8 @@
 ise 
-errors.
 Ansi
@@ -963,14 +963,13 @@
 ' %25 
-terms)
+key)%0A
 %0A   
@@ -1010,16 +1010,17 @@
 read())%0A
+%0A
         

Update plugin for ansible 2.0