|
|
@ -25,6 +25,9 @@ description:
|
|
|
|
cluster. These sessions can then be used in conjunction with key value pairs
|
|
|
|
cluster. These sessions can then be used in conjunction with key value pairs
|
|
|
|
to implement distributed locks. In depth documentation for working with
|
|
|
|
to implement distributed locks. In depth documentation for working with
|
|
|
|
sessions can be found here http://www.consul.io/docs/internals/sessions.html
|
|
|
|
sessions can be found here http://www.consul.io/docs/internals/sessions.html
|
|
|
|
|
|
|
|
requirements:
|
|
|
|
|
|
|
|
- python-consul
|
|
|
|
|
|
|
|
- requests
|
|
|
|
version_added: "1.9"
|
|
|
|
version_added: "1.9"
|
|
|
|
author: Steve Gargan (steve.gargan@gmail.com)
|
|
|
|
author: Steve Gargan (steve.gargan@gmail.com)
|
|
|
|
options:
|
|
|
|
options:
|
|
|
@ -50,7 +53,8 @@ options:
|
|
|
|
description:
|
|
|
|
description:
|
|
|
|
- the optional lock delay that can be attached to the session when it
|
|
|
|
- the optional lock delay that can be attached to the session when it
|
|
|
|
is created. Locks for invalidated sessions ar blocked from being
|
|
|
|
is created. Locks for invalidated sessions ar blocked from being
|
|
|
|
acquired until this delay has expired.
|
|
|
|
acquired until this delay has expired. Valid units for delays
|
|
|
|
|
|
|
|
include 'ns', 'us', 'ms', 's', 'm', 'h'
|
|
|
|
default: 15s
|
|
|
|
default: 15s
|
|
|
|
required: false
|
|
|
|
required: false
|
|
|
|
node:
|
|
|
|
node:
|
|
|
@ -99,7 +103,7 @@ EXAMPLES = '''
|
|
|
|
- name: register a session with lock_delay
|
|
|
|
- name: register a session with lock_delay
|
|
|
|
consul_session:
|
|
|
|
consul_session:
|
|
|
|
name: session_with_delay
|
|
|
|
name: session_with_delay
|
|
|
|
delay: 20
|
|
|
|
delay: 20s
|
|
|
|
|
|
|
|
|
|
|
|
- name: retrieve info about session by id
|
|
|
|
- name: retrieve info about session by id
|
|
|
|
consul_session: id=session_id state=info
|
|
|
|
consul_session: id=session_id state=info
|
|
|
@ -113,12 +117,10 @@ import urllib2
|
|
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
import consul
|
|
|
|
import consul
|
|
|
|
|
|
|
|
from requests.exceptions import ConnectionError
|
|
|
|
|
|
|
|
python_consul_installed = True
|
|
|
|
except ImportError, e:
|
|
|
|
except ImportError, e:
|
|
|
|
print "failed=True msg='python-consul required for this module. see "\
|
|
|
|
python_consul_installed = False
|
|
|
|
"http://python-consul.readthedocs.org/en/latest/#installation'"
|
|
|
|
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
from requests.errors import ConnectionError
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def execute(module):
|
|
|
|
def execute(module):
|
|
|
|
|
|
|
|
|
|
|
@ -186,7 +188,7 @@ def update_session(module):
|
|
|
|
session = consul.session.create(
|
|
|
|
session = consul.session.create(
|
|
|
|
name=name,
|
|
|
|
name=name,
|
|
|
|
node=node,
|
|
|
|
node=node,
|
|
|
|
lock_delay=delay,
|
|
|
|
lock_delay=validate_duration('delay', delay),
|
|
|
|
dc=datacenter,
|
|
|
|
dc=datacenter,
|
|
|
|
checks=checks
|
|
|
|
checks=checks
|
|
|
|
)
|
|
|
|
)
|
|
|
@ -219,15 +221,27 @@ def remove_session(module):
|
|
|
|
module.fail_json(msg="Could not remove session with id '%s' %s" % (
|
|
|
|
module.fail_json(msg="Could not remove session with id '%s' %s" % (
|
|
|
|
session_id, e))
|
|
|
|
session_id, e))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def validate_duration(name, duration):
|
|
|
|
|
|
|
|
if duration:
|
|
|
|
|
|
|
|
duration_units = ['ns', 'us', 'ms', 's', 'm', 'h']
|
|
|
|
|
|
|
|
if not any((duration.endswith(suffix) for suffix in duration_units)):
|
|
|
|
|
|
|
|
raise Exception('Invalid %s %s you must specify units (%s)' %
|
|
|
|
|
|
|
|
(name, duration, ', '.join(duration_units)))
|
|
|
|
|
|
|
|
return duration
|
|
|
|
|
|
|
|
|
|
|
|
def get_consul_api(module):
|
|
|
|
def get_consul_api(module):
|
|
|
|
return consul.Consul(host=module.params.get('host'),
|
|
|
|
return consul.Consul(host=module.params.get('host'),
|
|
|
|
port=module.params.get('port'))
|
|
|
|
port=module.params.get('port'))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_dependencies(module):
|
|
|
|
|
|
|
|
if not python_consul_installed:
|
|
|
|
|
|
|
|
module.fail_json(msg="python-consul required for this module. "\
|
|
|
|
|
|
|
|
"see http://python-consul.readthedocs.org/en/latest/#installation")
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
def main():
|
|
|
|
argument_spec = dict(
|
|
|
|
argument_spec = dict(
|
|
|
|
checks=dict(default=None, required=False, type='list'),
|
|
|
|
checks=dict(default=None, required=False, type='list'),
|
|
|
|
delay=dict(required=False,type='int', default=15),
|
|
|
|
delay=dict(required=False,type='str', default='15s'),
|
|
|
|
host=dict(default='localhost'),
|
|
|
|
host=dict(default='localhost'),
|
|
|
|
port=dict(default=8500, type='int'),
|
|
|
|
port=dict(default=8500, type='int'),
|
|
|
|
id=dict(required=False),
|
|
|
|
id=dict(required=False),
|
|
|
@ -237,7 +251,9 @@ def main():
|
|
|
|
choices=['present', 'absent', 'info', 'node', 'list'])
|
|
|
|
choices=['present', 'absent', 'info', 'node', 'list'])
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
module = AnsibleModule(argument_spec, supports_check_mode=True)
|
|
|
|
module = AnsibleModule(argument_spec, supports_check_mode=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
test_dependencies(module)
|
|
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
execute(module)
|
|
|
|
execute(module)
|
|
|
|