Skip to content

Instantly share code, notes, and snippets.

@zahodi
Created November 8, 2017 17:06
Show Gist options
  • Save zahodi/e85665ce9353acb1c20a59817db5ff3a to your computer and use it in GitHub Desktop.
Save zahodi/e85665ce9353acb1c20a59817db5ff3a to your computer and use it in GitHub Desktop.
Ansible task to upgrade elasticsearch cluster.
---
- name: get current elasticsearch version
uri:
url: http://localhost:9200
method: GET
headers:
Content-Type: application/json
register: current_elasticsearch_version
- block:
- set_fact:
es_home: "/usr/share/elasticsearch"
- name: get node status
uri:
url: http://localhost:9200/_cluster/health?pretty
method: GET
headers:
Content-Type: application/json
register: pre_upgrade_cluster_state
- name: print elastic_response
debug:
msg: "{{ pre_upgrade_cluster_state }}"
- name: disable shard allocation
uri:
url: http://localhost:9200/_cluster/settings
method: PUT
body: '{ "transient": { "cluster.routing.allocation.enable": "none" } }'
headers:
Content-Type: application/json
register: elastic_response
- name: Stop non-essential indexing and perform a synced flush
uri:
url: http://localhost:9200/_flush/synced?pretty
method: POST
ignore_errors: yes
register: elastic_response2
- name: stop elasticsearch service
service:
name: elasticsearch
state: stopped
- name: remove aws cloud plugin
command: "{{ es_home }}/bin/elasticsearch-plugin remove discovery-ec2"
- name: upgrade elasticsearch
apt:
pkg: "elasticsearch={{ elasticsearch_version }}"
state: present
update_cache: yes
- name: install aws cloud plugin
command: "{{ es_home }}/bin/elasticsearch-plugin install discovery-ec2"
- name: start elasticsearch service
service:
name: elasticsearch
state: started
- name: wait for node to come up
wait_for:
host: localhost
port: 9200
delay: 10
- name: Verify the node has joined the cluster
uri:
url: http://localhost:9200/_cluster/health?pretty
method: GET
headers:
Content-Type: application/json
register: post_upgrade_cluster_state
until: pre_upgrade_cluster_state.json.number_of_nodes == post_upgrade_cluster_state.json.number_of_nodes
retries: 10
delay: 10
- name: enable shard allocation
uri:
url: http://localhost:9200/_cluster/settings
method: PUT
body: '{ "transient": { "cluster.routing.allocation.enable": "all" } }'
headers:
Content-Type: application/json
- name: Ensure cluster is back to green
uri:
url: http://localhost:9200/_cluster/health?pretty
method: GET
headers:
Content-Type: application/json
register: final_cluster_health
until: final_cluster_health.json.status == "green"
retries: 1000
delay: 30
- name: print post upgrade cluster state
debug:
msg: "{{ final_cluster_health }}"
when: current_elasticsearch_version.json.version.number != elasticsearch_version
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment