Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.