2020-03-09 10:11:07 +01:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2022-08-05 12:28:29 +02:00
|
|
|
# Copyright (c) 2015, Steve Gargan <steve.gargan@gmail.com>
|
|
|
|
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
from __future__ import absolute_import, division, print_function
|
|
|
|
__metaclass__ = type
|
|
|
|
|
|
|
|
DOCUMENTATION = '''
|
|
|
|
module: consul_session
|
|
|
|
short_description: Manipulate consul sessions
|
|
|
|
description:
|
|
|
|
- Allows the addition, modification and deletion of sessions in a consul
|
|
|
|
cluster. These sessions can then be used in conjunction with key value pairs
|
|
|
|
to implement distributed locks. In depth documentation for working with
|
|
|
|
sessions can be found at http://www.consul.io/docs/internals/sessions.html
|
|
|
|
author:
|
Add attributes to consul, datadog, jenkins, pagerduty, rundeck, and sensu modules (#5957)
* Add attributes to consul, datadog, jenkins, pagerduty, rundeck, and sensu modules.
* Fix error.
2023-02-24 09:23:56 +01:00
|
|
|
- Steve Gargan (@sgargan)
|
2023-07-07 07:49:10 +02:00
|
|
|
- Håkon Lerring (@Hakon)
|
Add attributes to consul, datadog, jenkins, pagerduty, rundeck, and sensu modules (#5957)
* Add attributes to consul, datadog, jenkins, pagerduty, rundeck, and sensu modules.
* Fix error.
2023-02-24 09:23:56 +01:00
|
|
|
extends_documentation_fragment:
|
2024-01-21 18:51:45 +01:00
|
|
|
- community.general.consul
|
Add attributes to consul, datadog, jenkins, pagerduty, rundeck, and sensu modules (#5957)
* Add attributes to consul, datadog, jenkins, pagerduty, rundeck, and sensu modules.
* Fix error.
2023-02-24 09:23:56 +01:00
|
|
|
- community.general.attributes
|
|
|
|
attributes:
|
|
|
|
check_mode:
|
|
|
|
support: none
|
|
|
|
diff_mode:
|
|
|
|
support: none
|
2020-03-09 10:11:07 +01:00
|
|
|
options:
|
|
|
|
id:
|
|
|
|
description:
|
2023-06-15 15:46:33 +02:00
|
|
|
- ID of the session, required when O(state) is either V(info) or
|
|
|
|
V(remove).
|
2020-03-09 10:11:07 +01:00
|
|
|
type: str
|
|
|
|
state:
|
|
|
|
description:
|
|
|
|
- Whether the session should be present i.e. created if it doesn't
|
2023-06-15 15:46:33 +02:00
|
|
|
exist, or absent, removed if present. If created, the O(id) for the
|
|
|
|
session is returned in the output. If V(absent), O(id) is
|
2020-03-09 10:11:07 +01:00
|
|
|
required to remove the session. Info for a single session, all the
|
|
|
|
sessions for a node or all available sessions can be retrieved by
|
2023-06-15 15:46:33 +02:00
|
|
|
specifying V(info), V(node) or V(list) for the O(state); for V(node)
|
|
|
|
or V(info), the node O(name) or session O(id) is required as parameter.
|
2020-03-09 10:11:07 +01:00
|
|
|
choices: [ absent, info, list, node, present ]
|
|
|
|
type: str
|
|
|
|
default: present
|
|
|
|
name:
|
|
|
|
description:
|
|
|
|
- The name that should be associated with the session. Required when
|
2023-06-15 15:46:33 +02:00
|
|
|
O(state=node) is used.
|
2020-03-09 10:11:07 +01:00
|
|
|
type: str
|
|
|
|
delay:
|
|
|
|
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. Durations are in seconds.
|
|
|
|
type: int
|
|
|
|
default: 15
|
|
|
|
node:
|
|
|
|
description:
|
|
|
|
- The name of the node that with which the session will be associated.
|
|
|
|
by default this is the name of the agent.
|
|
|
|
type: str
|
|
|
|
datacenter:
|
|
|
|
description:
|
|
|
|
- The name of the datacenter in which the session exists or should be
|
|
|
|
created.
|
|
|
|
type: str
|
|
|
|
checks:
|
|
|
|
description:
|
|
|
|
- Checks that will be used to verify the session health. If
|
|
|
|
all the checks fail, the session will be invalidated and any locks
|
|
|
|
associated with the session will be release and can be acquired once
|
|
|
|
the associated lock delay has expired.
|
|
|
|
type: list
|
2021-02-26 11:37:23 +01:00
|
|
|
elements: str
|
2020-03-09 10:11:07 +01:00
|
|
|
behavior:
|
|
|
|
description:
|
|
|
|
- The optional behavior that can be attached to the session when it
|
|
|
|
is created. This controls the behavior when a session is invalidated.
|
|
|
|
choices: [ delete, release ]
|
|
|
|
type: str
|
|
|
|
default: release
|
2022-07-31 13:17:43 +02:00
|
|
|
ttl:
|
|
|
|
description:
|
|
|
|
- Specifies the duration of a session in seconds (between 10 and 86400).
|
|
|
|
type: int
|
|
|
|
version_added: 5.4.0
|
2022-09-03 12:17:54 +02:00
|
|
|
token:
|
|
|
|
version_added: 5.6.0
|
2020-03-09 10:11:07 +01:00
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
2020-05-16 15:07:51 +02:00
|
|
|
- name: Register basic session with consul
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.consul_session:
|
2020-03-09 10:11:07 +01:00
|
|
|
name: session1
|
|
|
|
|
2020-05-16 15:07:51 +02:00
|
|
|
- name: Register a session with an existing check
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.consul_session:
|
2020-03-09 10:11:07 +01:00
|
|
|
name: session_with_check
|
|
|
|
checks:
|
|
|
|
- existing_check_name
|
|
|
|
|
2020-05-16 15:07:51 +02:00
|
|
|
- name: Register a session with lock_delay
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.consul_session:
|
2020-03-09 10:11:07 +01:00
|
|
|
name: session_with_delay
|
|
|
|
delay: 20s
|
|
|
|
|
2020-05-16 15:07:51 +02:00
|
|
|
- name: Retrieve info about session by id
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.consul_session:
|
2020-03-09 10:11:07 +01:00
|
|
|
id: session_id
|
|
|
|
state: info
|
|
|
|
|
2020-05-16 15:07:51 +02:00
|
|
|
- name: Retrieve active sessions
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.consul_session:
|
2020-03-09 10:11:07 +01:00
|
|
|
state: list
|
2022-07-31 13:17:43 +02:00
|
|
|
|
|
|
|
- name: Register session with a ttl
|
|
|
|
community.general.consul_session:
|
|
|
|
name: session-with-ttl
|
|
|
|
ttl: 600 # sec
|
2020-03-09 10:11:07 +01:00
|
|
|
'''
|
|
|
|
|
2023-07-07 07:49:10 +02:00
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
2024-01-21 18:51:45 +01:00
|
|
|
from ansible_collections.community.general.plugins.module_utils.consul import (
|
|
|
|
auth_argument_spec, _ConsulModule
|
|
|
|
)
|
2023-07-07 07:49:10 +02:00
|
|
|
|
2020-03-09 10:11:07 +01:00
|
|
|
|
2024-01-21 18:51:45 +01:00
|
|
|
def execute(module, consul_module):
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
state = module.params.get('state')
|
|
|
|
|
|
|
|
if state in ['info', 'list', 'node']:
|
2024-01-21 18:51:45 +01:00
|
|
|
lookup_sessions(module, consul_module)
|
2020-03-09 10:11:07 +01:00
|
|
|
elif state == 'present':
|
2024-01-21 18:51:45 +01:00
|
|
|
update_session(module, consul_module)
|
2023-07-07 07:49:10 +02:00
|
|
|
else:
|
2024-01-21 18:51:45 +01:00
|
|
|
remove_session(module, consul_module)
|
2023-07-07 07:49:10 +02:00
|
|
|
|
|
|
|
|
2024-01-21 18:51:45 +01:00
|
|
|
def list_sessions(consul_module, datacenter):
|
|
|
|
return consul_module.get(
|
|
|
|
'session/list',
|
|
|
|
params={'dc': datacenter})
|
2023-07-07 07:49:10 +02:00
|
|
|
|
|
|
|
|
2024-01-21 18:51:45 +01:00
|
|
|
def list_sessions_for_node(consul_module, node, datacenter):
|
|
|
|
return consul_module.get(
|
|
|
|
('session', 'node', node),
|
|
|
|
params={'dc': datacenter})
|
2023-07-07 07:49:10 +02:00
|
|
|
|
|
|
|
|
2024-01-21 18:51:45 +01:00
|
|
|
def get_session_info(consul_module, session_id, datacenter):
|
|
|
|
return consul_module.get(
|
|
|
|
('session', 'info', session_id),
|
|
|
|
params={'dc': datacenter})
|
2023-07-07 07:49:10 +02:00
|
|
|
|
|
|
|
|
2024-01-21 18:51:45 +01:00
|
|
|
def lookup_sessions(module, consul_module):
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
datacenter = module.params.get('datacenter')
|
|
|
|
|
|
|
|
state = module.params.get('state')
|
|
|
|
try:
|
|
|
|
if state == 'list':
|
2024-01-21 18:51:45 +01:00
|
|
|
sessions_list = list_sessions(consul_module, datacenter)
|
2020-03-09 10:11:07 +01:00
|
|
|
# Ditch the index, this can be grabbed from the results
|
|
|
|
if sessions_list and len(sessions_list) >= 2:
|
|
|
|
sessions_list = sessions_list[1]
|
|
|
|
module.exit_json(changed=True,
|
|
|
|
sessions=sessions_list)
|
|
|
|
elif state == 'node':
|
|
|
|
node = module.params.get('node')
|
2024-01-21 18:51:45 +01:00
|
|
|
sessions = list_sessions_for_node(consul_module, node, datacenter)
|
2020-03-09 10:11:07 +01:00
|
|
|
module.exit_json(changed=True,
|
|
|
|
node=node,
|
|
|
|
sessions=sessions)
|
|
|
|
elif state == 'info':
|
|
|
|
session_id = module.params.get('id')
|
|
|
|
|
2024-01-21 18:51:45 +01:00
|
|
|
session_by_id = get_session_info(consul_module, session_id, datacenter)
|
2020-03-09 10:11:07 +01:00
|
|
|
module.exit_json(changed=True,
|
|
|
|
session_id=session_id,
|
|
|
|
sessions=session_by_id)
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
module.fail_json(msg="Could not retrieve session info %s" % e)
|
|
|
|
|
|
|
|
|
2024-01-21 18:51:45 +01:00
|
|
|
def create_session(consul_module, name, behavior, ttl, node,
|
2023-07-07 07:49:10 +02:00
|
|
|
lock_delay, datacenter, checks):
|
|
|
|
create_data = {
|
|
|
|
"LockDelay": lock_delay,
|
|
|
|
"Node": node,
|
|
|
|
"Name": name,
|
|
|
|
"Checks": checks,
|
|
|
|
"Behavior": behavior,
|
|
|
|
}
|
|
|
|
if ttl is not None:
|
|
|
|
create_data["TTL"] = "%ss" % str(ttl) # TTL is in seconds
|
2024-01-21 18:51:45 +01:00
|
|
|
create_session_response_dict = consul_module.put(
|
|
|
|
'session/create',
|
2023-07-07 07:49:10 +02:00
|
|
|
params={
|
|
|
|
'dc': datacenter},
|
2024-01-21 18:51:45 +01:00
|
|
|
data=create_data)
|
2023-07-07 07:49:10 +02:00
|
|
|
return create_session_response_dict["ID"]
|
|
|
|
|
|
|
|
|
2024-01-21 18:51:45 +01:00
|
|
|
def update_session(module, consul_module):
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
name = module.params.get('name')
|
|
|
|
delay = module.params.get('delay')
|
|
|
|
checks = module.params.get('checks')
|
|
|
|
datacenter = module.params.get('datacenter')
|
|
|
|
node = module.params.get('node')
|
|
|
|
behavior = module.params.get('behavior')
|
2022-07-31 13:17:43 +02:00
|
|
|
ttl = module.params.get('ttl')
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
try:
|
2024-01-21 18:51:45 +01:00
|
|
|
session = create_session(consul_module,
|
2023-07-07 07:49:10 +02:00
|
|
|
name=name,
|
|
|
|
behavior=behavior,
|
|
|
|
ttl=ttl,
|
|
|
|
node=node,
|
|
|
|
lock_delay=delay,
|
|
|
|
datacenter=datacenter,
|
|
|
|
checks=checks
|
|
|
|
)
|
2020-03-09 10:11:07 +01:00
|
|
|
module.exit_json(changed=True,
|
|
|
|
session_id=session,
|
|
|
|
name=name,
|
|
|
|
behavior=behavior,
|
2022-07-31 13:17:43 +02:00
|
|
|
ttl=ttl,
|
2020-03-09 10:11:07 +01:00
|
|
|
delay=delay,
|
|
|
|
checks=checks,
|
|
|
|
node=node)
|
|
|
|
except Exception as e:
|
|
|
|
module.fail_json(msg="Could not create/update session %s" % e)
|
|
|
|
|
|
|
|
|
2024-01-21 18:51:45 +01:00
|
|
|
def destroy_session(consul_module, session_id):
|
|
|
|
return consul_module.put(('session', 'destroy', session_id))
|
2023-07-07 07:49:10 +02:00
|
|
|
|
|
|
|
|
2024-01-21 18:51:45 +01:00
|
|
|
def remove_session(module, consul_module):
|
2020-03-09 10:11:07 +01:00
|
|
|
session_id = module.params.get('id')
|
|
|
|
|
|
|
|
try:
|
2024-01-21 18:51:45 +01:00
|
|
|
destroy_session(consul_module, session_id)
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
module.exit_json(changed=True,
|
|
|
|
session_id=session_id)
|
|
|
|
except Exception as e:
|
|
|
|
module.fail_json(msg="Could not remove session with id '%s' %s" % (
|
|
|
|
session_id, e))
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
argument_spec = dict(
|
2021-02-26 11:37:23 +01:00
|
|
|
checks=dict(type='list', elements='str'),
|
2020-03-09 10:11:07 +01:00
|
|
|
delay=dict(type='int', default='15'),
|
2023-07-07 07:49:10 +02:00
|
|
|
behavior=dict(
|
|
|
|
type='str',
|
|
|
|
default='release',
|
|
|
|
choices=[
|
|
|
|
'release',
|
|
|
|
'delete']),
|
2022-07-31 13:17:43 +02:00
|
|
|
ttl=dict(type='int'),
|
2020-03-09 10:11:07 +01:00
|
|
|
id=dict(type='str'),
|
|
|
|
name=dict(type='str'),
|
|
|
|
node=dict(type='str'),
|
2023-07-07 07:49:10 +02:00
|
|
|
state=dict(
|
|
|
|
type='str',
|
|
|
|
default='present',
|
|
|
|
choices=[
|
|
|
|
'absent',
|
|
|
|
'info',
|
|
|
|
'list',
|
|
|
|
'node',
|
|
|
|
'present']),
|
2020-03-09 10:11:07 +01:00
|
|
|
datacenter=dict(type='str'),
|
2024-01-21 18:51:45 +01:00
|
|
|
**auth_argument_spec()
|
2020-03-09 10:11:07 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec=argument_spec,
|
|
|
|
required_if=[
|
|
|
|
('state', 'node', ['name']),
|
|
|
|
('state', 'info', ['id']),
|
|
|
|
('state', 'remove', ['id']),
|
|
|
|
],
|
|
|
|
supports_check_mode=False
|
|
|
|
)
|
2024-01-21 18:51:45 +01:00
|
|
|
consul_module = _ConsulModule(module)
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
try:
|
2024-01-21 18:51:45 +01:00
|
|
|
execute(module, consul_module)
|
2020-03-09 10:11:07 +01:00
|
|
|
except Exception as e:
|
|
|
|
module.fail_json(msg=str(e))
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|