forked from confluentinc/cp-ansible
-
Notifications
You must be signed in to change notification settings - Fork 0
/
upgrade_zookeeper.yml
216 lines (183 loc) · 6.25 KB
/
upgrade_zookeeper.yml
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
- name: Zookeeper Upgrade - Prep
hosts: zookeeper
# Running serial=1 because add_host has problems
# https://stackoverflow.com/questions/42106527/ansible-how-to-call-module-add-host-for-all-hosts-of-the-play
serial: 1
tags:
- prep
tasks:
- import_role:
name: confluent.variables_handlers
- name: Zookeeper is Running
systemd:
name: "{{zookeeper_service_name}}"
state: started
- name: Install NetCat - RedHat
yum:
name: nc
state: present
when: ansible_os_family == "RedHat"
- name: Install NetCat - Debian
apt:
name: netcat-openbsd
state: present
when: ansible_os_family == "Debian"
- name: Wait for Zookeeper Status
shell: echo srvr | nc localhost 2181
register: srvr
until: srvr.rc == 0
retries: 30
delay: 5
- name: Wait for Zookeeper Quorum
shell: echo srvr | nc localhost 2181
register: srvr
until: '"Latency min/avg/max:" in srvr.stdout'
retries: 30
delay: 5
- name: Get Package Facts
package_facts:
manager: auto
- set_fact:
confluent_server_enabled: true
when: ansible_facts.packages['confluent-server'] is defined
- set_fact:
confluent_server_enabled: false
when: ansible_facts.packages['confluent-kafka-2.12'] is defined
- name: Set Current Package Version
set_fact:
zookeeper_current_version: "{{ ansible_facts.packages['confluent-server'][0]['version'] }}"
when: confluent_server_enabled|bool
- name: Set Current Package Version
set_fact:
zookeeper_current_version: "{{ ansible_facts.packages['confluent-kafka-2.12'][0]['version'] }}"
when: not confluent_server_enabled|bool
- debug:
msg: "Current version: {{zookeeper_current_version}} Upgrade to version: {{confluent_package_version}}"
- name: Get Leader/Follower
shell: echo srvr | nc localhost 2181 | grep Mode
register: leader_query
- name: Add host to Follower Group
add_host:
name: "{{ inventory_hostname }}"
group: upgrade_zookeeper_followers
delegate_to: localhost
when:
- '"follower" in leader_query.stdout'
- zookeeper_current_version != confluent_full_package_version
- zookeeper_current_version != confluent_package_version
- name: Add host to Leader Group
add_host:
name: "{{ inventory_hostname }}"
group: upgrade_zookeeper_leader
delegate_to: localhost
when:
- '"leader" in leader_query.stdout'
- zookeeper_current_version != confluent_full_package_version
- zookeeper_current_version != confluent_package_version
- debug:
msg: "Leader: {{inventory_hostname}}"
when: '"leader" in leader_query.stdout'
- name: Zookeeper Upgrade
# Putting leader group last here with serial=1 so the leader runs last
hosts: upgrade_zookeeper_followers,upgrade_zookeeper_leader
serial: 1
tags:
- upgrade
tasks:
- import_role:
name: confluent.variables_handlers
- name: Create Backup Directory
file:
path: "/tmp/upgrade/{{ zookeeper_service_name }}"
state: directory
mode: 0640
- set_fact:
timestamp: "{{ lookup('pipe', 'date +%Y%m%d%H%M%S') }}"
- name: Backup Configuration files
copy:
src: "{{ item }}"
remote_src: yes
dest: "/tmp/upgrade/{{ zookeeper_service_name }}/{{ item | basename }}-{{timestamp}}"
loop:
- "{{ zookeeper.config_file }}"
- "{{ zookeeper.systemd_override }}"
- "{{ kafka_broker.config_file }}"
- name: Stop Service
systemd:
name: "{{ zookeeper_service_name }}"
state: stopped
- name: Wait for Zookeeper Status on Another Zookeeper Node
shell: echo srvr | nc localhost 2181
register: srvr
until: srvr.rc == 0
retries: 30
delay: 5
delegate_to: "{{ groups['zookeeper'] | difference([inventory_hostname]) | first }}"
- name: Wait for Zookeeper Quorum on Another Zookeeper Node
shell: echo srvr | nc localhost 2181
register: srvr
until: '"Latency min/avg/max:" in srvr.stdout'
retries: 30
delay: 5
delegate_to: "{{ groups['zookeeper'] | difference([inventory_hostname]) | first }}"
- name: Configure Repositories
import_role:
name: confluent.common
vars:
install_java: false
- name: Install the Packages - Red Hat
yum:
name: "{{item}}{{confluent_package_redhat_suffix}}"
state: latest
loop: "{{ zookeeper_packages }}"
when: ansible_os_family == "RedHat"
- name: Install the Packages - Debian
apt:
name: "{{item}}{{confluent_package_debian_suffix}}"
update_cache: yes
loop: "{{ zookeeper_packages }}"
when: ansible_os_family == "Debian"
- name: Put back configuration
copy:
dest: "{{ item }}"
remote_src: yes
src: "/tmp/upgrade/{{ zookeeper_service_name }}/{{ item | basename }}-{{timestamp}}"
loop:
- "{{ zookeeper.config_file }}"
- "{{ kafka_broker.config_file }}"
- name: Search for existing snapshot files
find:
paths: "{{zookeeper_data_dir}}/version-2"
patterns: "snapshot.*"
register: snapshot_files
- name: Copy empty snapshot file
copy:
src: files/snapshot.0
dest: "{{zookeeper_data_dir}}/version-2/snapshot.0"
owner: "{{zookeeper.user}}"
group: "{{zookeeper.group}}"
mode: 0644
when: "snapshot_files.files | length == 0"
- name: Add Disable Admin Server Property
lineinfile:
name: "{{zookeeper.config_file}}"
line: admin.enableServer=false
regexp: admin.enableServer=.*
- name: reload systemd
command: systemctl daemon-reload
- name: Restart Service
systemd:
name: "{{ zookeeper_service_name }}"
state: restarted
- name: Wait for Zookeeper Status
shell: echo srvr | nc localhost 2181
register: srvr
until: srvr.rc == 0
retries: 30
delay: 5
- name: Wait for Zookeeper Quorum
shell: echo srvr | nc localhost 2181
register: srvr
until: '"Latency min/avg/max:" in srvr.stdout'
retries: 30
delay: 5