From a7274774ed74b7c6213c1d5212f725f3175f769d Mon Sep 17 00:00:00 2001 From: rdezavalia Date: Mon, 6 Jun 2016 12:26:12 -0300 Subject: [PATCH] Support etcd v2. Use this version by default (#12312) * Support etcd v2. Use this version by default * default to etcd v1 --- lib/ansible/plugins/lookup/etcd.py | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/lib/ansible/plugins/lookup/etcd.py b/lib/ansible/plugins/lookup/etcd.py index 25b5c049e2..190b214ab1 100644 --- a/lib/ansible/plugins/lookup/etcd.py +++ b/lib/ansible/plugins/lookup/etcd.py @@ -32,10 +32,16 @@ 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_URL = os.environ['ANSIBLE_ETCD_VERSION'] + class Etcd: - def __init__(self, url=ANSIBLE_ETCD_URL, validate_certs=True): + def __init__(self, url=ANSIBLE_ETCD_URL, version=ANSIBLE_ETCD_VERSION, + validate_certs=True): self.url = url - self.baseurl = '%s/v1/keys' % (self.url) + self.version = version + self.baseurl = '%s/%s/keys' % (self.url,self.version) self.validate_certs = validate_certs def get(self, key): @@ -52,8 +58,13 @@ class Etcd: try: # {"action":"get","key":"/name","value":"Jane Jolie","index":5} item = json.loads(data) - if 'value' in item: - value = item['value'] + if self.version == 'v1': + if 'value' in item: + value = item['value'] + else: + if 'node' in item: + value = item['node']['value'] + if 'errorCode' in item: value = "ENOENT" except: