Fix session-based kv acquire/release (#2181)
* Fix session-based kv acquire/release * add example of using session in doc
This commit is contained in:
parent
9677961b8c
commit
cd03f10b9c
1 changed files with 27 additions and 13 deletions
|
@ -130,6 +130,13 @@ EXAMPLES = '''
|
||||||
consul_kv:
|
consul_kv:
|
||||||
key: ansible/groups/dc1/somenode
|
key: ansible/groups/dc1/somenode
|
||||||
value: 'top_secret'
|
value: 'top_secret'
|
||||||
|
|
||||||
|
- name: Register a key/value pair with an associated session
|
||||||
|
consul_kv:
|
||||||
|
key: stg/node/server_birthday
|
||||||
|
value: 20160509
|
||||||
|
session: "{{ sessionid }}"
|
||||||
|
state: acquire
|
||||||
'''
|
'''
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
|
@ -157,6 +164,8 @@ def execute(module):
|
||||||
|
|
||||||
def lock(module, state):
|
def lock(module, state):
|
||||||
|
|
||||||
|
consul_api = get_consul_api(module)
|
||||||
|
|
||||||
session = module.params.get('session')
|
session = module.params.get('session')
|
||||||
key = module.params.get('key')
|
key = module.params.get('key')
|
||||||
value = module.params.get('value')
|
value = module.params.get('value')
|
||||||
|
@ -166,18 +175,22 @@ def lock(module, state):
|
||||||
msg='%s of lock for %s requested but no session supplied' %
|
msg='%s of lock for %s requested but no session supplied' %
|
||||||
(state, key))
|
(state, key))
|
||||||
|
|
||||||
if state == 'acquire':
|
index, existing = consul_api.kv.get(key)
|
||||||
successful = consul_api.kv.put(key, value,
|
|
||||||
cas=module.params.get('cas'),
|
|
||||||
acquire=session,
|
|
||||||
flags=module.params.get('flags'))
|
|
||||||
else:
|
|
||||||
successful = consul_api.kv.put(key, value,
|
|
||||||
cas=module.params.get('cas'),
|
|
||||||
release=session,
|
|
||||||
flags=module.params.get('flags'))
|
|
||||||
|
|
||||||
module.exit_json(changed=successful,
|
changed = not existing or (existing and existing['Value'] != value)
|
||||||
|
if changed and not module.check_mode:
|
||||||
|
if state == 'acquire':
|
||||||
|
changed = consul_api.kv.put(key, value,
|
||||||
|
cas=module.params.get('cas'),
|
||||||
|
acquire=session,
|
||||||
|
flags=module.params.get('flags'))
|
||||||
|
else:
|
||||||
|
changed = consul_api.kv.put(key, value,
|
||||||
|
cas=module.params.get('cas'),
|
||||||
|
release=session,
|
||||||
|
flags=module.params.get('flags'))
|
||||||
|
|
||||||
|
module.exit_json(changed=changed,
|
||||||
index=index,
|
index=index,
|
||||||
key=key)
|
key=key)
|
||||||
|
|
||||||
|
@ -251,9 +264,10 @@ def main():
|
||||||
port=dict(default=8500, type='int'),
|
port=dict(default=8500, type='int'),
|
||||||
recurse=dict(required=False, type='bool'),
|
recurse=dict(required=False, type='bool'),
|
||||||
retrieve=dict(required=False, default=True),
|
retrieve=dict(required=False, default=True),
|
||||||
state=dict(default='present', choices=['present', 'absent']),
|
state=dict(default='present', choices=['present', 'absent', 'acquire', 'release']),
|
||||||
token=dict(required=False, default='anonymous', no_log=True),
|
token=dict(required=False, default='anonymous', no_log=True),
|
||||||
value=dict(required=False)
|
value=dict(required=False),
|
||||||
|
session=dict(required=False)
|
||||||
)
|
)
|
||||||
|
|
||||||
module = AnsibleModule(argument_spec, supports_check_mode=False)
|
module = AnsibleModule(argument_spec, supports_check_mode=False)
|
||||||
|
|
Loading…
Reference in a new issue