-
Notifications
You must be signed in to change notification settings - Fork 46
/
kafka_topic.py
251 lines (227 loc) · 7.51 KB
/
kafka_topic.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Ansible module for topic configuration management
"""
from __future__ import absolute_import, division, print_function
__metaclass__ = type
# Init logging
import logging
import sys
# XXX: fix kafka-python import broken for Python 3.12
import ansible.module_utils.kafka_fix_import # noqa
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.kafka_lib_topic import process_module_topic
from ansible.module_utils.kafka_lib_commons import (
module_commons, module_zookeeper_commons, module_topic_commons,
DOCUMENTATION_COMMON
)
# Default logging
# TODO: refactor all this logging logic
log = logging.getLogger('kafka')
log.addHandler(logging.StreamHandler(sys.stdout))
log.setLevel(logging.INFO)
log = logging.getLogger('kazoo.client')
log.addHandler(logging.StreamHandler(sys.stdout))
log.setLevel(logging.INFO)
ANSIBLE_METADATA = {'metadata_version': '1.0'}
DOCUMENTATION = '''
---
module: kafka_topic
short_description: Manage Kafka topic
description:
- Configure Kafka topic.
- Not compatible avec Kafka version < 0.11.0.
author:
- Stephen SORRIAUX
options:
name:
description:
- 'when resource = topic, name of the topic.'
- 'when resource = acl, name of the `acl_resource_type` or * for'
- 'all resources of type `acl_resource_type`.'
required: True
partition:
description:
- 'when resource = topic, number of partitions for this resource.'
replica_factor:
description:
- 'when resource = topic, number of replica for the partitions of '
- 'this resource.'
force_reassign:
description:
- 'force reassign topic/partition between all the brokers.'
default: False
preserve_leader:
description:
- 'when reassign topic/partition try to preserve topic/partition'
- 'leader to limit downtime.'
default: False
state:
description:
- 'state of the managed resource.'
default: present
choices: [present, absent]
options:
description:
- 'a dict with all options wanted for the managed resource'
- 'Example: retention.ms: 7594038'
type: dict
zookeeper:
description:
- 'the zookeeper connection.'
zookeeper_auth_scheme:
description:
- 'when zookeeper is configured to use authentication, schema used to '
- 'connect to zookeeper.'
default: 'digest'
choices: [digest, sasl]
zookeeper_auth_value:
description:
- 'when zookeeper is configured to use authentication, value used to '
- 'connect.'
zookeeper_use_ssl:
description:
- 'force using ssl for zookeeper connection.'
default: False
zookeeper_ssl_check_hostname:
description:
- 'when using ssl for zookeeper, check if certificate for hostname is '
- 'correct.'
default: True
zookeeper_ssl_cafile:
description:
- 'when using ssl for zookeeper, content of ca cert file or path to '
- 'ca cert file.'
zookeeper_ssl_certfile:
description:
- 'when using ssl for zookeeper, content of cert file or path to '
- 'server cert file.'
zookeeper_ssl_keyfile:
description:
- 'when using ssl for zookeeper, content of keyfile or path to '
- 'server cert key file.'
zookeeper_ssl_password:
description:
- 'when using ssl for zookeeper, password for ssl_keyfile.'
zookeeper_sleep_time:
description:
- 'when updating number of partitions and while checking for'
- 'the ZK node, the time to sleep (in seconds) between'
- 'each checks.'
default: 5
zookeeper_max_retries:
description:
- 'when updating number of partitions and while checking for'
- 'the ZK node, maximum of try to do before failing'
default: 5
kafka_sleep_time:
description:
- 'when updating number of partitions and while checking for'
- 'kafka to applied, the time to sleep (in seconds) between'
- 'each checks.'
default: 5
kafka_max_retries:
description:
- 'when updating number of partitions and while checking for'
- 'kafka to applied, maximum of try to do before failing'
default: 5
''' + DOCUMENTATION_COMMON
EXAMPLES = '''
# creates a topic 'test' with provided configuation for plaintext
# configured Kafka and Zookeeper
- name: create topic
kafka_topic:
api_version: "1.0.1"
name: 'test'
partitions: 2
replica_factor: 1
options:
retention.ms: 574930
flush.ms: 12345
state: 'present'
zookeeper: >
"{{ hostvars['zk']['ansible_eth0']['ipv4']['address'] }}:2181"
bootstrap_servers: >
"{{ hostvars['kafka1']['ansible_eth0']['ipv4']['address'] }}:9092,
{{ hostvars['kafka2']['ansible_eth0']['ipv4']['address'] }}:9092"
# creates a topic for a sasl_ssl configured Kafka and plaintext Zookeeper
- name: create topic
kafka_topic:
api_version: "1.0.1"
name: 'test'
partitions: 2
replica_factor: 1
options:
retention.ms: 574930
flush.ms: 12345
state: 'present'
zookeeper: >
"{{ hostvars['zk']['ansible_eth0']['ipv4']['address'] }}:2181"
bootstrap_servers: >
"{{ hostvars['kafka1']['ansible_eth0']['ipv4']['address'] }}:9092,
{{ hostvars['kafka2']['ansible_eth0']['ipv4']['address'] }}:9092"
security_protocol: 'SASL_SSL'
sasl_plain_username: 'username'
sasl_plain_password: 'password'
ssl_cafile: '{{ content_of_ca_cert_file_or_path_to_ca_cert_file }}'
# creates a topic for a plaintext configured Kafka and a digest
# authentication Zookeeper
- name: create topic
kafka_topic:
api_version: "1.0.1"
name: 'test'
partitions: 2
replica_factor: 1
options:
retention.ms: 574930
flush.ms: 12345
state: 'present'
zookeeper: >
"{{ hostvars['zk']['ansible_eth0']['ipv4']['address'] }}:2181"
zookeeper_auth_scheme: "digest"
zookeeper_auth_value: "username:password"
bootstrap_servers: >
"{{ hostvars['kafka1']['ansible_eth0']['ipv4']['address'] }}:9092,
{{ hostvars['kafka2']['ansible_eth0']['ipv4']['address'] }}:9092"
# deletes a topic
- name: delete topic
kafka_topic:
api_version: "1.0.1"
name: 'test'
state: 'absent'
zookeeper: >
"{{ hostvars['zk']['ansible_eth0']['ipv4']['address'] }}:2181"
bootstrap_servers: >
"{{ hostvars['kafka1']['ansible_eth0']['ipv4']['address'] }}:9092,
{{ hostvars['kafka2']['ansible_eth0']['ipv4']['address'] }}:9092"
# deletes a topic using automatic api_version discovery
- name: delete topic
kafka_topic:
name: 'test'
state: 'absent'
zookeeper: >
"{{ hostvars['zk']['ansible_eth0']['ipv4']['address'] }}:2181"
bootstrap_servers: >
"{{ hostvars['kafka1']['ansible_eth0']['ipv4']['address'] }}:9092,
{{ hostvars['kafka2']['ansible_eth0']['ipv4']['address'] }}:9092"
'''
def main():
"""
Module usage
"""
spec = dict(
# resource name
name=dict(type='str', required=True),
state=dict(choices=['present', 'absent'], default='present'),
**module_commons
)
spec.update(module_topic_commons)
spec.update(module_zookeeper_commons)
module = AnsibleModule(
argument_spec=spec,
supports_check_mode=True
)
process_module_topic(module)
if __name__ == '__main__':
main()