Repository: ansible/ansible-modules-extras

Issue:
consul_session: consul.session.create() requires a lock_delay without units
##### ISSUE TYPE
- Bug Report
##### COMPONENT NAME

consul_session
##### ANSIBLE VERSION

```
ansible 2.0.2.0
  config file =
  configured module search path = Default w/o overrides
```
##### CONFIGURATION

N/A
##### OS / ENVIRONMENT

```
pip freeze | grep consul
python-consul==0.6.0
```
##### SUMMARY

When creating a consul session, the module requires passing a unit with the `lock_delay` parameter. This is not a valid option in the [underlying python-consul module](http://python-consul.readthedocs.io/en/latest/#consul.base.Consul.Session), which states `lock_delay is an integer of seconds`.

Fix here: https://github.com/quantopian/ansible-modules-extras/commit/763c8f75c19b926618d1a066ec792c058872fd43  
##### STEPS TO REPRODUCE

``` yaml
    - name: Register new session
      consul_session:
        name: "{{ consul_session_name }}"
```

`ansible-playbook -i 'localhost,' -c local consul.yml`
##### EXPECTED RESULTS
- successful playbook execution
- successful creation of a named session in consul
##### ACTUAL RESULTS

```
fatal: [ec2.compute-1.amazonaws.com]: FAILED! => {"changed": false, "failed": true, "invocation": {"module_args": {"checks": null, "datacenter": null, "delay": "15s", "host": "localhost", "id": null, "name": "foo", "node": null, "port": 8500, "state": "present"}, "module_name": "consul_session"}, "msg": "Could not create/update session No JSON object could be decoded"}
```

A local debug session provided the error:

```
[10] > /usr/lib/python2.7/json/decoder.py(384)raw_decode()
-> raise ValueError("No JSON object could be decoded")
(Pdb++) locals()
{'s': u'Request decode failed: time: unknown unit ss in duration 15ss', 'self': <json.decoder.JSONDecoder object at 0x7f9a3bb48ed0>, 'idx': 0}
```



Discussion:
<!-- @irlevesque: Thanks for filing this bug report! -->

@sgargan ping, this issue is waiting for your response.
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)

<!--- boilerplate: issue_bug_new --->


Resolving patch:
diff --git a/clustering/consul_session.py b/clustering/consul_session.py
--- a/clustering/consul_session.py
+++ b/clustering/consul_session.py
@@ -1,4 +1,5 @@
 #!/usr/bin/python
+# -*- coding: utf-8 -*-
 #
 # (c) 2015, Steve Gargan <steve.gargan@gmail.com>
 #
@@ -30,7 +31,7 @@
   - python-consul
   - requests
 version_added: "2.0"
-author: "Steve Gargan (@sgargan)"
+author: "Steve Gargan @sgargan"
 options:
     state:
         description:
@@ -54,9 +55,8 @@
         description:
           - the optional lock delay that can be attached to the session when it
             is created. Locks for invalidated sessions ar blocked from being
-            acquired until this delay has expired. Valid units for delays
-            include 'ns', 'us', 'ms', 's', 'm', 'h' 
-        default: 15s
+            acquired until this delay has expired. Durations are in seconds
+        default: 15
         required: false
     node:
         description:
@@ -114,7 +114,7 @@
 - name: register basic session with consul
   consul_session:
     name: session1
-    
+
 - name: register a session with an existing check
   consul_session:
     name: session_with_check
@@ -201,12 +201,11 @@ def update_session(module):
     consul_client = get_consul_api(module)
 
     try:
-        
         session = consul_client.session.create(
             name=name,
             behavior=behavior,
             node=node,
-            lock_delay=validate_duration('delay', delay),
+            lock_delay=delay,
             dc=datacenter,
             checks=checks
         )
@@ -223,7 +222,6 @@ def update_session(module):
 
 def remove_session(module):
     session_id = module.params.get('id')
-
     if not session_id:
         module.fail_json(msg="""A session id must be supplied in order to
         remove a session.""")
@@ -239,18 +237,10 @@ def remove_session(module):
         module.fail_json(msg="Could not remove session with id '%s' %s" % (
                          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):
     return consul.Consul(host=module.params.get('host'),
                          port=module.params.get('port'))
-                         
+
 def test_dependencies(module):
     if not python_consul_installed:
         module.fail_json(msg="python-consul required for this module. "\
@@ -259,7 +249,7 @@ def test_dependencies(module):
 def main():
     argument_spec = dict(
         checks=dict(default=None, required=False, type='list'),
-        delay=dict(required=False,type='str', default='15s'),
+        delay=dict(required=False,type='int', default='15'),
         behavior=dict(required=False,type='str', default='release',
                       choices=['release', 'delete']),
         host=dict(default='localhost'),
@@ -275,9 +265,9 @@ def main():
     )
 
     module = AnsibleModule(argument_spec, supports_check_mode=False)
-    
+
     test_dependencies(module)
-    
+
     try:
         execute(module)
     except ConnectionError, e:


Test patch: