-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathelasticsearch-wait-for-cluster-health.yml
66 lines (60 loc) · 2.92 KB
/
elasticsearch-wait-for-cluster-health.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
---
# Make sure shard allocation is enabled
- name: Enable shard allocation for the cluster
ansible.builtin.uri:
url: "{{ elasticsearch_http_protocol }}://{{ elasticsearch_api_host }}:{{ elasticstack_elasticsearch_http_port }}/_cluster/settings"
method: PUT
body: '{ "persistent": { "cluster.routing.allocation.enable": null }}'
body_format: json
user: elastic
password: "{{ elasticstack_password.stdout }}"
validate_certs: no
register: response
# next line is boolean not string, so no quotes around true
# use python truthiness
until: "response.json.acknowledged == true"
retries: 5
delay: 30
# From https://www.elastic.co/guide/en/elastic-stack/8.17/upgrading-elasticsearch.html
## During a rolling upgrade, primary shards assigned to a node running the new version cannot have their replicas assigned to a node with the old version. The new version might have a different data format that is not understood by the old version.
##
## If it is not possible to assign the replica shards to another node (there is only one upgraded node in the cluster), the replica shards remain unassigned and status stays yellow.
##
## In this case, you can proceed once there are no initializing or relocating shards (check the init and relo columns).
- name: Check cluster health
block:
- name: Wait for cluster health to return to green
ansible.builtin.uri:
url: "{{ elasticsearch_http_protocol }}://{{ elasticsearch_api_host }}:{{ elasticstack_elasticsearch_http_port }}/_cluster/health"
method: GET
user: elastic
password: "{{ elasticstack_password.stdout }}"
validate_certs: no
register: response
until: "response.json.status == 'green'"
retries: 50
delay: 30
# Timed out while waiting for green cluster
# Check if we can continue with a yellow cluster
rescue:
- name: "Rescue: Check if cluster health is yellow"
ansible.builtin.uri:
url: "{{ elasticsearch_http_protocol }}://{{ elasticsearch_api_host }}:{{ elasticstack_elasticsearch_http_port }}/_cluster/health"
method: GET
user: elastic
password: "{{ elasticstack_password.stdout }}"
validate_certs: no
register: response
failed_when: "response.json.status != 'yellow' or response.json.relocating_shards != 0 or response.json.initializing_shards != 0"
- name: "Rescure: Wait before verifying status"
ansible.builtin.pause:
seconds: 10
- name: "Rescue: Verify we can safely continue with yellow cluster"
ansible.builtin.uri:
url: "{{ elasticsearch_http_protocol }}://{{ elasticsearch_api_host }}:{{ elasticstack_elasticsearch_http_port }}/_cluster/health"
method: GET
user: elastic
password: "{{ elasticstack_password.stdout }}"
validate_certs: no
register: response
failed_when: "response.json.status != 'yellow' or response.json.relocating_shards != 0 or response.json.initializing_shards != 0"