Skip to content

Instantly share code, notes, and snippets.

@holysoros
Last active November 27, 2018 15:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save holysoros/ef4049a424aac347d04ddd905eb6e79f to your computer and use it in GitHub Desktop.
Save holysoros/ef4049a424aac347d04ddd905eb6e79f to your computer and use it in GitHub Desktop.
Elasticsearch rolling restart ansible playbook
# Ansible
# Rolling Upgrade of Elasticsearch
# author: Jeff Steinmetz, @jeffsteinmetz; Bin Li, @holysoros
# tested with Ansible 2.4
---
- name: Elasticsearch rolling upgrade
hosts: es
serial: 1
vars:
es_disable_allocation: '{ "persistent": { "cluster.routing.allocation.enable": "none" } }'
es_enable_allocation: '{ "persistent": { "cluster.routing.allocation.enable": null } }'
es_http_port: 9200
es_transport_port: 9300
tasks:
# this first step is a overkill, but here
# in case the rolling restart was cancelled by user mid playbook run
- name: make sure elasticsearch service is running
service: name=elasticsearch enabled=yes state=started
register: response
become: true
- name: Wait for elasticsearch node to come back up if it was stopped
wait_for: port={{ es_transport_port }} delay=45
# this step is key!!! Don't restart more nodes
# until all shards have completed recovery
- name: Wait for cluster health to return to green
uri: url=http://localhost:{{ es_http_port }}/_cluster/health method=GET
register: response
retries: 50
delay: 30
- name: Disable shard allocation for the cluster
uri: url=http://localhost:{{ es_http_port }}/_cluster/settings method=PUT body='{{ es_disable_allocation }}' body_format=json
- name: Shutdown elasticsearch service
service: name=elasticsearch enabled=yes state=restarted
become: true
- name: Wait for elasticsearch node to come back up
wait_for: port={{ es_transport_port }} delay=35
- name: Confirm the node joins the cluster
shell: "curl -s -m 2 'localhost:9200/_cat/nodes?h=name' | grep -E '^{{ansible_hostname}}$'"
register: result
until: result.rc == 0
retries: 200
delay: 3
- name: Enable shard allocation for the cluster
uri: url=http://localhost:{{ es_http_port }}/_cluster/settings method=PUT body='{{ es_enable_allocation }}' body_format=json
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
- 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: 5
delay: 30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment