How to install/upgrade elasticsearch cluster via ansible?

First, we have a playbook:

# es.yml
- hosts: es
  become: yes
    - elasticsearch
  serial: "50%"

serial: "50%" means ansbile will run tasks in 50% of hosts (or less).

And, we should add a rule elasticsearch:

# roles/elasticsearch/tasks/main.yml
- name: disable shard allocation for the cluster
  uri: url=http://localhost:9200/_cluster/settings method=PUT body='{{ es_allocation.disable }}' body_format=json
  ignore_errors: yes

- name: download ealsticsearch deb pacakge
    module: get_url
    url: "{{ elasticsearch_url }}"
    dest: "cache/elasticsearch-{{ elasticsearch_version }}.deb"
    checksum: "sha256:{{ elasticsearch_sha256 }}"
  become: no

- name: check if elasticsearch is installed
  command: dpkg-query --showformat='${Version}' -W elasticsearch
  register: elasticsearch_version_check
  failed_when: elasticsearch_version_check.rc > 1
  changed_when: elasticsearch_version_check.rc == 1

- block:
    - name: copy elasticsearch package
      copy: src="cache/elasticsearch-{{ elasticsearch_version }}.deb" dest=/tmp/elasticsearch.deb
    - name: install elasticsearch
      apt: deb=/tmp/elasticsearch.deb
    - name: delete elasticsearch package
      file: path=/tmp/elasticsearch.deb state=absent
  when: elasticsearch_version_check.stdout != "{{ elasticsearch_version }}"

# configure elasticsearch
# install plugins

- name: restart elasticsearch
  service: name=elasticsearch state=restarted

- name: wait for elasticsearch node to come back up
  wait_for: port=9200 delay=15

- name: enable shard allocation for the cluster
  uri: url=http://localhost:9200/_cluster/settings method=PUT body='{{ es_allocation.enable }}'  body_format=json

- name: wait for cluster health to return to green
  uri: url=http://localhost:9200/_cluster/health method=GET
  register: response
  until: "response.json.status == 'green'"
  retries: 50
  delay: 5
  1. we try to disable allocation for cluster upgrade
  2. download elasticsearch’s deb package to local cache
  3. install and configure, restart elasticsearch
  4. wait 9200 port is available, enable allocation, wait for cluster become green.

some vars:

# roles/elasticsearch/vars/main.yml
elasticsearch_version: 2.3.2
elasticsearch_url: "{{ elasticsearch_version }}/elasticsearch-{{ elasticsearch_version }}.deb"
elasticsearch_sha256: 3d474b0123ec8ad4ebfa089f8cde607033e6cbef28a6a0df318bdc3d2a546cd8

  disable: '{"transient":{"cluster.routing.allocation.enable":"none"}}'
  enable: '{"transient":{"cluster.routing.allocation.enable": "all"}}'

something to notice:

  • Do not forget configure unique in elasticsearch.yml.
  • configure to ${HOSTNAME} in elasticsearch.yml, needn’t to generate nodename.
  • generate host list from inventory list:
    {% for host in groups['es'] %}
      - {{ host }}
    {% endfor -%}
  • When master is upgrading, master electing will cause cluster unavailable for write.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.