2020-03-09 10:11:07 +01:00
|
|
|
#!/usr/bin/python
|
2021-08-08 10:40:22 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
2020-03-09 10:11:07 +01:00
|
|
|
# Copyright 2015 WP Engine, Inc. All rights reserved.
|
2022-08-05 12:28:29 +02:00
|
|
|
# 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: znode
|
|
|
|
short_description: Create, delete, retrieve, and update znodes using ZooKeeper
|
|
|
|
description:
|
|
|
|
- Create, delete, retrieve, and update znodes using ZooKeeper.
|
2023-02-20 17:29:14 +01:00
|
|
|
attributes:
|
|
|
|
check_mode:
|
|
|
|
support: none
|
|
|
|
diff_mode:
|
|
|
|
support: none
|
|
|
|
extends_documentation_fragment:
|
|
|
|
- community.general.attributes
|
2020-03-09 10:11:07 +01:00
|
|
|
options:
|
|
|
|
hosts:
|
|
|
|
description:
|
|
|
|
- A list of ZooKeeper servers (format '[server]:[port]').
|
|
|
|
required: true
|
2021-02-26 11:37:23 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
name:
|
|
|
|
description:
|
|
|
|
- The path of the znode.
|
|
|
|
required: true
|
2021-02-26 11:37:23 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
value:
|
|
|
|
description:
|
|
|
|
- The value assigned to the znode.
|
2021-02-26 11:37:23 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
op:
|
|
|
|
description:
|
|
|
|
- An operation to perform. Mutually exclusive with state.
|
2020-11-27 21:16:47 +01:00
|
|
|
choices: [ get, wait, list ]
|
2021-02-26 11:37:23 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
state:
|
|
|
|
description:
|
|
|
|
- The state to enforce. Mutually exclusive with op.
|
2020-11-27 21:16:47 +01:00
|
|
|
choices: [ present, absent ]
|
2021-02-26 11:37:23 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
timeout:
|
|
|
|
description:
|
|
|
|
- The amount of time to wait for a node to appear.
|
|
|
|
default: 300
|
2021-02-26 11:37:23 +01:00
|
|
|
type: int
|
2020-03-09 10:11:07 +01:00
|
|
|
recursive:
|
|
|
|
description:
|
|
|
|
- Recursively delete node and all its children.
|
|
|
|
type: bool
|
2022-08-24 20:00:11 +02:00
|
|
|
default: false
|
2022-10-05 08:23:15 +02:00
|
|
|
auth_scheme:
|
|
|
|
description:
|
|
|
|
- 'Authentication scheme.'
|
|
|
|
choices: [ digest, sasl ]
|
|
|
|
type: str
|
|
|
|
default: "digest"
|
|
|
|
required: false
|
|
|
|
version_added: 5.8.0
|
|
|
|
auth_credential:
|
|
|
|
description:
|
|
|
|
- The authentication credential value. Depends on I(auth_scheme).
|
|
|
|
- The format for I(auth_scheme=digest) is C(user:password),
|
|
|
|
and the format for I(auth_scheme=sasl) is C(user:password).
|
|
|
|
type: str
|
|
|
|
required: false
|
|
|
|
version_added: 5.8.0
|
2023-03-14 23:01:59 +01:00
|
|
|
use_tls:
|
|
|
|
description:
|
|
|
|
- Using TLS/SSL or not.
|
|
|
|
type: bool
|
|
|
|
default: false
|
|
|
|
required: false
|
|
|
|
version_added: '6.5.0'
|
2020-03-09 10:11:07 +01:00
|
|
|
requirements:
|
|
|
|
- kazoo >= 2.1
|
|
|
|
- python >= 2.6
|
|
|
|
author: "Trey Perry (@treyperry)"
|
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = """
|
2020-05-15 12:12:41 +02:00
|
|
|
- name: Creating or updating a znode with a given value
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.znode:
|
2020-03-09 10:11:07 +01:00
|
|
|
hosts: 'localhost:2181'
|
|
|
|
name: /mypath
|
|
|
|
value: myvalue
|
|
|
|
state: present
|
|
|
|
|
2020-05-15 12:12:41 +02:00
|
|
|
- name: Getting the value and stat structure for a znode
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.znode:
|
2020-03-09 10:11:07 +01:00
|
|
|
hosts: 'localhost:2181'
|
|
|
|
name: /mypath
|
|
|
|
op: get
|
|
|
|
|
2022-10-05 08:23:15 +02:00
|
|
|
- name: Getting the value and stat structure for a znode using digest authentication
|
|
|
|
community.general.znode:
|
|
|
|
hosts: 'localhost:2181'
|
|
|
|
auth_credential: 'user1:s3cr3t'
|
|
|
|
name: /secretmypath
|
|
|
|
op: get
|
|
|
|
|
2020-05-15 12:12:41 +02:00
|
|
|
- name: Listing a particular znode's children
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.znode:
|
2020-03-09 10:11:07 +01:00
|
|
|
hosts: 'localhost:2181'
|
|
|
|
name: /zookeeper
|
|
|
|
op: list
|
|
|
|
|
2020-05-15 12:12:41 +02:00
|
|
|
- name: Waiting 20 seconds for a znode to appear at path /mypath
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.znode:
|
2020-03-09 10:11:07 +01:00
|
|
|
hosts: 'localhost:2181'
|
|
|
|
name: /mypath
|
|
|
|
op: wait
|
|
|
|
timeout: 20
|
|
|
|
|
2020-05-15 12:12:41 +02:00
|
|
|
- name: Deleting a znode at path /mypath
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.znode:
|
2020-03-09 10:11:07 +01:00
|
|
|
hosts: 'localhost:2181'
|
|
|
|
name: /mypath
|
|
|
|
state: absent
|
|
|
|
|
2020-05-15 12:12:41 +02:00
|
|
|
- name: Creating or updating a znode with a given value on a remote Zookeeper
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.znode:
|
2020-03-09 10:11:07 +01:00
|
|
|
hosts: 'my-zookeeper-node:2181'
|
|
|
|
name: /mypath
|
|
|
|
value: myvalue
|
|
|
|
state: present
|
|
|
|
delegate_to: 127.0.0.1
|
|
|
|
"""
|
|
|
|
|
|
|
|
import time
|
|
|
|
import traceback
|
|
|
|
|
|
|
|
KAZOO_IMP_ERR = None
|
|
|
|
try:
|
|
|
|
from kazoo.client import KazooClient
|
|
|
|
from kazoo.handlers.threading import KazooTimeoutError
|
|
|
|
KAZOO_INSTALLED = True
|
|
|
|
except ImportError:
|
|
|
|
KAZOO_IMP_ERR = traceback.format_exc()
|
|
|
|
KAZOO_INSTALLED = False
|
|
|
|
|
|
|
|
from ansible.module_utils.basic import AnsibleModule, missing_required_lib
|
2021-06-26 23:59:11 +02:00
|
|
|
from ansible.module_utils.common.text.converters import to_bytes
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec=dict(
|
|
|
|
hosts=dict(required=True, type='str'),
|
|
|
|
name=dict(required=True, type='str'),
|
2021-02-26 11:37:23 +01:00
|
|
|
value=dict(type='str'),
|
|
|
|
op=dict(choices=['get', 'wait', 'list']),
|
2020-03-09 10:11:07 +01:00
|
|
|
state=dict(choices=['present', 'absent']),
|
2021-02-26 11:37:23 +01:00
|
|
|
timeout=dict(default=300, type='int'),
|
2022-10-05 08:23:15 +02:00
|
|
|
recursive=dict(default=False, type='bool'),
|
|
|
|
auth_scheme=dict(default='digest', choices=['digest', 'sasl']),
|
|
|
|
auth_credential=dict(type='str', no_log=True),
|
2023-03-14 23:01:59 +01:00
|
|
|
use_tls=dict(default=False, type='bool'),
|
2020-03-09 10:11:07 +01:00
|
|
|
),
|
|
|
|
supports_check_mode=False
|
|
|
|
)
|
|
|
|
|
|
|
|
if not KAZOO_INSTALLED:
|
|
|
|
module.fail_json(msg=missing_required_lib('kazoo >= 2.1'), exception=KAZOO_IMP_ERR)
|
|
|
|
|
|
|
|
check = check_params(module.params)
|
|
|
|
if not check['success']:
|
|
|
|
module.fail_json(msg=check['msg'])
|
|
|
|
|
|
|
|
zoo = KazooCommandProxy(module)
|
|
|
|
try:
|
|
|
|
zoo.start()
|
|
|
|
except KazooTimeoutError:
|
|
|
|
module.fail_json(msg='The connection to the ZooKeeper ensemble timed out.')
|
|
|
|
|
|
|
|
command_dict = {
|
|
|
|
'op': {
|
|
|
|
'get': zoo.get,
|
|
|
|
'list': zoo.list,
|
|
|
|
'wait': zoo.wait
|
|
|
|
},
|
|
|
|
'state': {
|
|
|
|
'present': zoo.present,
|
|
|
|
'absent': zoo.absent
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
command_type = 'op' if 'op' in module.params and module.params['op'] is not None else 'state'
|
|
|
|
method = module.params[command_type]
|
|
|
|
result, result_dict = command_dict[command_type][method]()
|
|
|
|
zoo.shutdown()
|
|
|
|
|
|
|
|
if result:
|
|
|
|
module.exit_json(**result_dict)
|
|
|
|
else:
|
|
|
|
module.fail_json(**result_dict)
|
|
|
|
|
|
|
|
|
|
|
|
def check_params(params):
|
|
|
|
if not params['state'] and not params['op']:
|
|
|
|
return {'success': False, 'msg': 'Please define an operation (op) or a state.'}
|
|
|
|
|
|
|
|
if params['state'] and params['op']:
|
|
|
|
return {'success': False, 'msg': 'Please choose an operation (op) or a state, but not both.'}
|
|
|
|
|
|
|
|
return {'success': True}
|
|
|
|
|
|
|
|
|
|
|
|
class KazooCommandProxy():
|
|
|
|
def __init__(self, module):
|
|
|
|
self.module = module
|
2023-03-14 23:01:59 +01:00
|
|
|
self.zk = KazooClient(module.params['hosts'], use_ssl=module.params['use_tls'])
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
def absent(self):
|
|
|
|
return self._absent(self.module.params['name'])
|
|
|
|
|
|
|
|
def exists(self, znode):
|
|
|
|
return self.zk.exists(znode)
|
|
|
|
|
|
|
|
def list(self):
|
|
|
|
children = self.zk.get_children(self.module.params['name'])
|
|
|
|
return True, {'count': len(children), 'items': children, 'msg': 'Retrieved znodes in path.',
|
|
|
|
'znode': self.module.params['name']}
|
|
|
|
|
|
|
|
def present(self):
|
|
|
|
return self._present(self.module.params['name'], self.module.params['value'])
|
|
|
|
|
|
|
|
def get(self):
|
|
|
|
return self._get(self.module.params['name'])
|
|
|
|
|
|
|
|
def shutdown(self):
|
|
|
|
self.zk.stop()
|
|
|
|
self.zk.close()
|
|
|
|
|
|
|
|
def start(self):
|
|
|
|
self.zk.start()
|
2022-10-05 08:23:15 +02:00
|
|
|
if self.module.params['auth_credential']:
|
|
|
|
self.zk.add_auth(self.module.params['auth_scheme'], self.module.params['auth_credential'])
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
def wait(self):
|
|
|
|
return self._wait(self.module.params['name'], self.module.params['timeout'])
|
|
|
|
|
|
|
|
def _absent(self, znode):
|
|
|
|
if self.exists(znode):
|
|
|
|
self.zk.delete(znode, recursive=self.module.params['recursive'])
|
|
|
|
return True, {'changed': True, 'msg': 'The znode was deleted.'}
|
|
|
|
else:
|
|
|
|
return True, {'changed': False, 'msg': 'The znode does not exist.'}
|
|
|
|
|
|
|
|
def _get(self, path):
|
|
|
|
if self.exists(path):
|
|
|
|
value, zstat = self.zk.get(path)
|
|
|
|
stat_dict = {}
|
|
|
|
for i in dir(zstat):
|
|
|
|
if not i.startswith('_'):
|
|
|
|
attr = getattr(zstat, i)
|
|
|
|
if isinstance(attr, (int, str)):
|
|
|
|
stat_dict[i] = attr
|
|
|
|
result = True, {'msg': 'The node was retrieved.', 'znode': path, 'value': value,
|
|
|
|
'stat': stat_dict}
|
|
|
|
else:
|
|
|
|
result = False, {'msg': 'The requested node does not exist.'}
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
def _present(self, path, value):
|
|
|
|
if self.exists(path):
|
|
|
|
(current_value, zstat) = self.zk.get(path)
|
|
|
|
if value != current_value:
|
|
|
|
self.zk.set(path, to_bytes(value))
|
|
|
|
return True, {'changed': True, 'msg': 'Updated the znode value.', 'znode': path,
|
|
|
|
'value': value}
|
|
|
|
else:
|
|
|
|
return True, {'changed': False, 'msg': 'No changes were necessary.', 'znode': path, 'value': value}
|
|
|
|
else:
|
|
|
|
self.zk.create(path, to_bytes(value), makepath=True)
|
|
|
|
return True, {'changed': True, 'msg': 'Created a new znode.', 'znode': path, 'value': value}
|
|
|
|
|
|
|
|
def _wait(self, path, timeout, interval=5):
|
|
|
|
lim = time.time() + timeout
|
|
|
|
|
|
|
|
while time.time() < lim:
|
|
|
|
if self.exists(path):
|
|
|
|
return True, {'msg': 'The node appeared before the configured timeout.',
|
|
|
|
'znode': path, 'timeout': timeout}
|
|
|
|
else:
|
|
|
|
time.sleep(interval)
|
|
|
|
|
|
|
|
return False, {'msg': 'The node did not appear before the operation timed out.', 'timeout': timeout,
|
|
|
|
'znode': path}
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|