From d214f49be729b6fecea0b74f5fb0ba9b265802cb Mon Sep 17 00:00:00 2001 From: wilfriedroset Date: Sun, 31 Jul 2022 13:17:43 +0200 Subject: [PATCH] consul: add support for session TTL (#4996) Signed-off-by: Wilfried Roset --- changelogs/fragments/4996-consul-session-ttl.yml | 2 ++ .../modules/clustering/consul/consul_session.py | 14 ++++++++++++++ .../targets/consul/tasks/consul_session.yml | 12 ++++++++++++ 3 files changed, 28 insertions(+) create mode 100644 changelogs/fragments/4996-consul-session-ttl.yml diff --git a/changelogs/fragments/4996-consul-session-ttl.yml b/changelogs/fragments/4996-consul-session-ttl.yml new file mode 100644 index 0000000000..99b7c27e9e --- /dev/null +++ b/changelogs/fragments/4996-consul-session-ttl.yml @@ -0,0 +1,2 @@ +minor_changes: + - consul - adds ``ttl`` parameter for session (https://github.com/ansible-collections/community.general/pull/4996). diff --git a/plugins/modules/clustering/consul/consul_session.py b/plugins/modules/clustering/consul/consul_session.py index 7ace1f89a8..3039fac175 100644 --- a/plugins/modules/clustering/consul/consul_session.py +++ b/plugins/modules/clustering/consul/consul_session.py @@ -95,6 +95,11 @@ options: choices: [ delete, release ] type: str default: release + ttl: + description: + - Specifies the duration of a session in seconds (between 10 and 86400). + type: int + version_added: 5.4.0 ''' EXAMPLES = ''' @@ -121,6 +126,11 @@ EXAMPLES = ''' - name: Retrieve active sessions community.general.consul_session: state: list + +- name: Register session with a ttl + community.general.consul_session: + name: session-with-ttl + ttl: 600 # sec ''' try: @@ -185,6 +195,7 @@ def update_session(module): datacenter = module.params.get('datacenter') node = module.params.get('node') behavior = module.params.get('behavior') + ttl = module.params.get('ttl') consul_client = get_consul_api(module) @@ -192,6 +203,7 @@ def update_session(module): session = consul_client.session.create( name=name, behavior=behavior, + ttl=ttl, node=node, lock_delay=delay, dc=datacenter, @@ -201,6 +213,7 @@ def update_session(module): session_id=session, name=name, behavior=behavior, + ttl=ttl, delay=delay, checks=checks, node=node) @@ -241,6 +254,7 @@ def main(): checks=dict(type='list', elements='str'), delay=dict(type='int', default='15'), behavior=dict(type='str', default='release', choices=['release', 'delete']), + ttl=dict(type='int'), host=dict(type='str', default='localhost'), port=dict(type='int', default=8500), scheme=dict(type='str', default='http'), diff --git a/tests/integration/targets/consul/tasks/consul_session.yml b/tests/integration/targets/consul/tasks/consul_session.yml index 1827c9c381..5feeb07c0d 100644 --- a/tests/integration/targets/consul/tasks/consul_session.yml +++ b/tests/integration/targets/consul/tasks/consul_session.yml @@ -158,3 +158,15 @@ that: - search_deleted is skipped # each iteration is skipped - search_deleted is not changed # and then unchanged + +- name: ensure session can be created with a ttl + consul_session: + state: present + name: session-with-ttl + ttl: 180 # sec + register: result + +- assert: + that: + - result is changed + - result['ttl'] == 180