Skip to content

Instantly share code, notes, and snippets.

@samdoran
Last active December 23, 2021 00:00
Show Gist options
  • Save samdoran/bd996dc0a3018ff6ba8e to your computer and use it in GitHub Desktop.
Save samdoran/bd996dc0a3018ff6ba8e to your computer and use it in GitHub Desktop.
Elasticsearch Playbooks
---
- name: Rolling elasticsearch cluster restart
hosts: es
serial: 1
sudo: yes
vars:
uribody_true: '{"transient":{"cluster.routing.allocation.enable":"none"}}'
uribody_false: '{"transient":{"cluster.routing.allocation.enable":"all"}}'
es_http_port: 9200
es_transport_port: 9300
pre_tasks:
- name: Disable shard allocation for the cluster
uri: url=http://localhost:{{ es_http_port }}/_cluster/settings method=PUT body='{{ uribody_true }}'
tasks:
- name: Restart elasticsearch node
service: name=elasticsearch state=restarted
- name: Wait for elasticsearch to come back up
wait_for: port={{ es_transport_port }} delay=35
post_tasks:
- name: Enable shard allocation for the cluster
uri: url=http://localhost:{{ es_http_port }}/_cluster/settings method=PUT body='{{ uribody_false }}'
delay: 3
tags: [ "elasticsearch" , "esconfig" ]
- name: Wait for cluster health to return to yellow
uri: url=http://localhost:{{ es_http_port }}/_cluster/health method=GET
register: response
until: "response.json.status == 'yellow'"
retries: 5
delay: 30
---
- name: Elasticsearch rolling upgrade
hosts: es
serial: 1
sudo: yes
vars:
uribody_true: '{"transient":{"cluster.routing.allocation.enable":"none"}}'
uribody_false: '{"transient":{"cluster.routing.allocation.enable":"all"}}'
es_http_port: 9200
es_transport_port: 9300
pre_tasks:
- name: Disable shard allocation for the cluster
uri: url=http://localhost:{{ es_http_port }}/_cluster/settings method=PUT body='{{ uribody_true }}'
tasks:
- name: Shutdown elasticsearch node
uri: url=http://localhost:{{ es_http_port }}/_cluster/nodes/_local/_shutdown method=POST
- name: Wait for all shards to be reallocated
uri: url=http://localhost:{{ es_http_port }}/_cluster/health method=GET
register: response
until: "response.json.relocating_shards == 0"
retries: 5
delay: 30
- name: Update elasticsearch
yum: name=elasticsearch state=latest
- name: Start elasticsearch
service: name=elasticsearch enabled=yes state=started
- name: Wait for elasticsearch node to come back up
wait_for: port={{ es_transport_port }} delay=35
- name: Wait for cluster health to return to yellow
uri: url=http://localhost:{{ es_http_port }}/_cluster/health method=GET
register: response
until: "response.json.status == 'yellow'"
retries: 5
delay: 30
post_tasks:
- name: Enable shard allocation for the cluster
uri: url=http://localhost:{{ es_http_port }}/_cluster/settings method=PUT body='{{ uribody_false }}'
delay: 3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment