-
Notifications
You must be signed in to change notification settings - Fork 8
/
elasticsearch-rolling-upgrade.yml
160 lines (135 loc) · 4.83 KB
/
elasticsearch-rolling-upgrade.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
---
- name: Elasticsearch rolling upgrade
hosts: elasticsearch
gather_facts: no
serial: 1
vars:
es_disable_allocation: '{"transient":{"cluster.routing.allocation.enable":"none"}}'
es_enable_allocation: '{"transient":{"cluster.routing.allocation.enable": "all","cluster.routing.allocation.node_concurrent_recoveries": 5,"indices.recovery.max_bytes_per_sec": "500mb"}}'
es_http_port: 9200
es_transport_port: 9300
# s3_plugin_url: https://artifacts.elastic.co/downloads/elasticsearch-plugins/repository-s3/repository-s3-{{ elk_version }}.zip
tasks:
- name: Validate ELK Version
fail: msg="Invalid ELK Version"
when: elk_version is undefined or not elk_version is match("\d+\.\d+\.\d+")
- name: Set the es_host for the first host
set_fact:
es_host: "{{ groups.elasticsearch[1] }}"
when: "inventory_hostname == groups.elasticsearch[0]"
- name: Set the es_host for the remaining hosts
set_fact:
es_host: "{{ groups.elasticsearch[0] }}"
when: "inventory_hostname != groups.elasticsearch[0]"
- name: Ensure elasticsearch service is running
systemd:
name: elasticsearch
enabled: yes
state: started
register: response
- name: Wait for elasticsearch node to come back up if it was stopped
wait_for:
port: "{{ es_transport_port }}"
delay: 45
when: response.changed == true
- name: Check current version
uri:
url: http://localhost:{{ es_http_port }}
method: GET
register: version_found
retries: 10
delay: 10
- name: Display Current Elasticsearch Version
debug: var=version_found.json.version.number
- block:
- name: Enable shard allocation for the cluster
uri:
url: http://localhost:{{ es_http_port }}/_cluster/settings
method: PUT
body_format: json
body: "{{ es_enable_allocation }}"
- name: Wait for cluster health to return to green
uri:
url: http://localhost:{{ es_http_port }}/_cluster/health
method: GET
register: response
until: "response.json.status == 'green'"
retries: 500
delay: 15
- name: Disable shard allocation for the cluster
uri:
url: http://localhost:{{ es_http_port }}/_cluster/settings
method: PUT
body_format: json
body: "{{ es_disable_allocation }}"
- name: Perform a synced flush
uri:
url: http://localhost:{{ es_http_port }}/_flush/synced
method: POST
status_code: "200, 409"
- name: Shutdown elasticsearch node
systemd:
name: elasticsearch
state: stopped
- name: Update elasticsearch
yum:
name: elasticsearch-{{ elk_version }}
state: present
# - name: Download repository-s3 plugin
# get_url: url={{ s3_plugin_url }} dest=/tmp/repository-s3-{{ elk_version }}.zip
# - name: Remove repository-s3 plugin
# shell: /usr/share/elasticsearch/bin/elasticsearch-plugin remove -v repository-s3
# - name: Upgrade repository-s3 plugin
# shell: /usr/share/elasticsearch/bin/elasticsearch-plugin install -v -b file:///tmp/repository-s3-{{ elk_version }}.zip
# - name: Remove repository-s3 installer
# file: path=/tmp/repository-s3-{{ elk_version }}.zip state=absent
- name: Wait for all shards to be reallocated
uri:
url: http://{{ es_host }}:{{ es_http_port }}/_cluster/health
method: GET
register: response
until: "response.json.relocating_shards == 0"
retries: 20
delay: 15
- name: Start elasticsearch
systemd:
name: elasticsearch
state: restarted
enabled: yes
daemon_reload: yes
- name: Wait for elasticsearch node to come back up
wait_for:
port: "{{ es_transport_port }}"
delay: 35
- name: Wait for elasticsearch http to come back up
wait_for:
port: "{{ es_http_port }}"
delay: 5
- name: Wait for cluster health to return to yellow or green
uri:
url: http://localhost:{{ es_http_port }}/_cluster/health
method: GET
register: response
until: "response.json.status == 'yellow' or response.json.status == 'green'"
retries: 500
delay: 15
- name: Enable shard allocation for the cluster
uri:
url: http://localhost:{{ es_http_port }}/_cluster/settings
method: PUT
body_format: json
body: "{{ es_enable_allocation }}"
register: response
until: "response.json.acknowledged == true"
retries: 10
delay: 15
- name: Wait for the node to recover
uri:
url: http://localhost:{{ es_http_port }}/_cat/health
method: GET
return_content: yes
register: response
until: "'green' in response.content"
retries: 500
delay: 15
when: version_found.json.version.number is version_compare(elk_version, '<')