|
|
@ -40,28 +40,32 @@ class LookupModule(object):
|
|
|
|
raise errors.AnsibleError("Can't LOOKUP(redis_kv): module redis is not installed")
|
|
|
|
raise errors.AnsibleError("Can't LOOKUP(redis_kv): module redis is not installed")
|
|
|
|
|
|
|
|
|
|
|
|
def run(self, terms, **kwargs):
|
|
|
|
def run(self, terms, **kwargs):
|
|
|
|
|
|
|
|
if isinstance(terms, basestring):
|
|
|
|
|
|
|
|
terms = [ terms ]
|
|
|
|
|
|
|
|
ret = []
|
|
|
|
|
|
|
|
for term in terms:
|
|
|
|
|
|
|
|
(url,key) = term.split(',')
|
|
|
|
|
|
|
|
if url == "":
|
|
|
|
|
|
|
|
url = 'redis://localhost:6379'
|
|
|
|
|
|
|
|
|
|
|
|
(url,key) = terms.split(',')
|
|
|
|
# urlsplit on Python 2.6.1 is broken. Hmm. Probably also the reason
|
|
|
|
if url == "":
|
|
|
|
# Redis' from_url() doesn't work here.
|
|
|
|
url = 'redis://localhost:6379'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# urlsplit on Python 2.6.1 is broken. Hmm. Probably also the reason
|
|
|
|
p = '(?P<scheme>[^:]+)://?(?P<host>[^:/ ]+).?(?P<port>[0-9]*).*'
|
|
|
|
# Redis' from_url() doesn't work here.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
p = '(?P<scheme>[^:]+)://?(?P<host>[^:/ ]+).?(?P<port>[0-9]*).*'
|
|
|
|
try:
|
|
|
|
|
|
|
|
m = re.search(p, url)
|
|
|
|
|
|
|
|
host = m.group('host')
|
|
|
|
|
|
|
|
port = int(m.group('port'))
|
|
|
|
|
|
|
|
except AttributeError:
|
|
|
|
|
|
|
|
raise errors.AnsibleError("Bad URI in redis lookup")
|
|
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
m = re.search(p, url)
|
|
|
|
conn = redis.Redis(host=host, port=port)
|
|
|
|
host = m.group('host')
|
|
|
|
res = conn.get(key)
|
|
|
|
port = int(m.group('port'))
|
|
|
|
if res is None:
|
|
|
|
except AttributeError:
|
|
|
|
res = ""
|
|
|
|
raise errors.AnsibleError("Bad URI in redis lookup")
|
|
|
|
ret.append(res)
|
|
|
|
|
|
|
|
except:
|
|
|
|
try:
|
|
|
|
ret.append("") # connection failed or key not found
|
|
|
|
conn = redis.Redis(host=host, port=port)
|
|
|
|
return ret
|
|
|
|
res = conn.get(key)
|
|
|
|
|
|
|
|
if res is None:
|
|
|
|
|
|
|
|
res = ""
|
|
|
|
|
|
|
|
return res
|
|
|
|
|
|
|
|
except:
|
|
|
|
|
|
|
|
return "" # connection failed or key not found
|
|
|
|
|
|
|
|