Skip to content

Instantly share code, notes, and snippets.

@nbari
Created August 22, 2017 16:14
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save nbari/1e9d5f6fa355fe1bd3ecb36d373998bc to your computer and use it in GitHub Desktop.
ansible rolling restart elasticsearch node
# Elasticsearch Rolling restart using Ansible
#
# Perform a rolling restart of the elasticsearch nodes and wait for the cluster
# to stabilize before continuing processing nodes.
#
# The handlers are chained together using notify to perform the following process:
#
# 1. Disable shard allocation on the cluster
# 2. Restart the elasticsearch node process
# 3. Wait for the node to rejoin the cluster
# 4. Enable shard allocation on the cluster
# 5. Wait for the cluster state to become green
#
# This keeps the cluster from entering a red state.
#
# Edit/modify this playbook to match your node.name, in this case
# node.name in elasticsearch.yml match {{ ansible_hostname }}
#
# Copy this file to an elk node and run it like this:
#
# ansible-playbook restart-elk.yml -i localhost,
#
# Install ansible:
# apt-add-repository ppa:ansible/ansible -y
# apt-get update && sudo apt-get install ansible -y
---
- hosts: all
connection: local
tasks:
- name: restart elasticsearch node
debug: msg="Rolling restart of node {{ ansible_hostname }} initiated"
changed_when: true
notify:
- routing_allocation_enable_none
handlers:
# Turn off shard allocation
- name: routing_allocation_enable_none
local_action: "shell curl -XPUT localhost:9200/_cluster/settings -d '{\"transient\" : {\"cluster.routing.allocation.enable\" : \"none\" }}'"
register: result
until: result.stdout.find('"acknowledged"') != -1
retries: 200
delay: 3
changed_when: result.stdout.find('"acknowledged":true') != -1
notify:
- restart elasticsearch
# restart elasticsearch process
- name: restart elasticsearch
service: name=elasticsearch state=restarted
notify:
- wait node
# Wait for Elasticsearch node to come back into cluster
- name: wait node
local_action: "shell curl -s 'localhost:9200/_cat/nodes?h=name' | tr -d ' ' | grep -E '^{{ ansible_hostname }}$'"
register: result
until: result.rc == 0
retries: 200
delay: 3
notify:
- cluster routing all
# Turn on shard allocation
- name: cluster routing all
local_action: "shell curl -s -XPUT localhost:9200/_cluster/settings -d '{\"transient\" : {\"cluster.routing.allocation.enable\" : \"all\" }}'"
register: result
until: result.stdout.find("acknowledged") != -1
retries: 200
delay: 3
changed_when: result.stdout.find('"acknowledged":true') != -1
notify:
- wait green
# Wait until cluster status is green
- name: wait green
local_action: shell curl -s localhost:9200/_cat/health | cut -d ' ' -f 4
register: result
until: result.stdout.find("green") != -1
retries: 200
delay: 3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment