Skip to content

Commit

Permalink
upgrading support
Browse files Browse the repository at this point in the history
  • Loading branch information
ShaunBoughey committed Aug 13, 2022
1 parent a611574 commit 8a81ff2
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 35 deletions.
57 changes: 44 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,50 @@ No requirements for this role.
Role Variables
--------------

A description of the settable variables for this role should go here, including any variables that are in defaults/main.yml, vars/main.yml, and any variables that can/should be set via parameters to the role. Any variables that are read from other roles and/or the global scope (ie. hostvars, group vars, etc.) should be mentioned here as well.

Dependencies
------------

No dependencies for this role.
Available variables are listed below, along with default values (see defaults/main.yml):

```yaml
es_major_version Default value: 7
es_minor_version: 16.1-1
```
Setting the major and minor version of Elasticsearch you are wanting to install. This will also ensure the correct yum repository is added to your instances.
```yaml
es_upgrade: false
es_upgrade_version: 7.16.2-1 # Need to specify tag upgrade for this to do anything.
```
Only runs upgrade tasks and will upgrade the cluster to the specified version. <strong>Ensure you have Serial: 1 set in your playbook for upgrading</strong>. Within the upgrade, shard allocation will be limited to primary shards only to ensure a quick upgrade. This follows Elasticsearch best pracise on rolling upgrades. Ensure you set the upgrade version in es_minor_version once complete.
```yaml
es_cluster_name: dev-cluster
es_data_path: /data/elasticsearch
es_log_path: /var/log/elasticsearch
es_user: elasticsearch
es_group: elasticsearch
```
Basic cluster information including the path for logd and data.
```yaml
es_heap_size: 1g
```
Sets the JVM heap size to the value. This will set both xms and xmx to the same value. This should be half of your memory on the instance. Don't set this above 32GB or you will see performance issue, instead add more instances.
```yaml
es_start_service: true
es_restart_on_change: true
```
Both of these settings are if you don't want the service to start on install. EG, you have extra plugins to configure first. The restart_on_change will restart any time you ammend the elasticsearch.yml.
```yaml
es_generate_certs: false
```
Still WiP. This will be used for xpack security and internode TLS.
Example Playbook
----------------

Including an example of how to use your role (for instance, with variables passed in as parameters) is always nice for users too:

- hosts: elastic
roles:
- pictowolf.elasticsearch

```yaml
---
- hosts: elastic
roles:
- pictowolf.elasticsearch
```
7 changes: 5 additions & 2 deletions defaults/main.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
---
# defaults file for elasticsearch
es_major_version: 7
es_minor_version: 15.2-1
es_minor_version: 17.5-1
es_upgrade: false
es_upgrade_version: 7.17.5-1 # es_upgrade must be set to true for this to upgrade.

es_cluster_name: dev-cluster
es_data_path: /data/elasticsearch
Expand All @@ -11,4 +13,5 @@ es_group: elasticsearch

es_heap_size: 1g
es_start_service: true
es_restart_on_change: true
es_restart_on_change: true
es_generate_certs: false
6 changes: 4 additions & 2 deletions handlers/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
# handlers file for elasticsearch

- name: restart elasticsearch
become: yes
ansible.builtin.service: name=elasticsearch state=restarted enabled=yes
ansible.builtin.service:
name: elasticsearch
state: restarted
enabled: true
when:
- es_restart_on_change
- es_start_service
Expand Down
1 change: 1 addition & 0 deletions molecule/default/molecule.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ platforms:
- /sys/fs/cgroup:/sys/fs/cgroup:ro
privileged: true
pre_build_image: true
command: "/usr/sbin/init"
groups:
- master
provisioner:
Expand Down
14 changes: 13 additions & 1 deletion tasks/main.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
---
- name: Include install tasks
ansible.builtin.include_tasks: install.yml
when: es_upgrade == false

- name: Include configuration tasks
ansible.builtin.include_tasks: configure.yml
ansible.builtin.include_tasks: configure.yml
when: es_upgrade == false

- name: Include SSL tasks
ansible.builtin.include_tasks: ssl.yml
when:
- es_generate_certs == true
- es_upgrade == false

- name: Include upgrade tasks
ansible.builtin.include_tasks: upgrade.yml
when: es_upgrade == true
1 change: 1 addition & 0 deletions tasks/ssl.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
---
48 changes: 31 additions & 17 deletions tasks/upgrade.yml
Original file line number Diff line number Diff line change
@@ -1,23 +1,30 @@
---
- name: Ensure elasticsearch already present
- name: Ensure Elastic is present
ansible.builtin.service:
name: name=elasticsearch state=started
name: elasticsearch
state: started

- name: ensure elasticsearch is up and available
- name: Ensure Elastic is running
ansible.builtin.wait_for:
host: "{{ hostvars[inventory_hostname]['ansible_default_ipv4']['address'] }}"
port: 9200
delay: 5

- name: Perform upgrade process as root
- name: Check if upgraded version is already installed
ansible.builtin.package:
list: "elasticsearch-{{ es_upgrade_version }}.x86_64"
register: installed_es_version


- name: Perform upgrade process
block:
- name: Disable shard allocation
ansible.builtin.uri:
url: "http://{{ hostvars[inventory_hostname]['ansible_default_ipv4']['address'] }}:9200/_cluster/settings"
body: '{"persistent":{"cluster.routing.allocation.enable":"primaries"}}' # specify no shard allocation
body_format: json
method: PUT

- name: Stop non essential indexing to speed up shard recovery
ansible.builtin.uri:
url: "http://{{ hostvars[inventory_hostname]['ansible_default_ipv4']['address'] }}:9200/_flush/synced"
Expand All @@ -29,45 +36,52 @@
url: "http://{{ hostvars[inventory_hostname]['ansible_default_ipv4']['address'] }}:9200"
register: pre_upgrade_cluster_info

- name: Shut down node
- name: Stop Elastic
ansible.builtin.service:
name: name=elasticsearch state=stopped
name: elasticsearch
state: stopped
when: es_upgrade_version

- name: Upgrade node
- name: Upgrade Elastic
ansible.builtin.package:
name: "elasticsearch-{{ es_upgrade_version }}.x86_64"
state: present

- name: bring up node
- name: Start Elastic
ansible.builtin.service:
name: name=elasticsearch state=started
notify: wait for elasticsearch to start
name: elasticsearch
state: started

- meta: flush_handlers
- name: Ensure Elastic instance is working
ansible.builtin.uri:
url: "http://{{ hostvars[inventory_hostname]['ansible_default_ipv4']['address'] }}:9200"
status_code: 200
retries: 3
delay: 10

- name: validate it joins cluster
- name: Validate instance joins the cluster
ansible.builtin.uri:
url: "http://{{ hostvars[inventory_hostname]['ansible_default_ipv4']['address'] }}:9200"
register: post_upgrade_cluster_info
until: pre_upgrade_cluster_info.json.cluster_uuid == post_upgrade_cluster_info.json.cluster_uuid
retries: 3
delay: 10

- name: reenable shard allocation
- name: Enable shard allocation
ansible.builtin.uri:
url: "http://{{ hostvars[inventory_hostname]['ansible_default_ipv4']['address'] }}:9200/_cluster/settings"
body: '{"persistent":{"cluster.routing.allocation.enable":null}}'
body_format: json
method: PUT

- name: Wait for elasticsearch to recover
- name: Wait for Elastic to recover
ansible.builtin.uri:
url: "http://{{ hostvars[inventory_hostname]['ansible_default_ipv4']['address'] }}:9200/_cat/health"
method: GET
return_content: yes
status_code: 200
body_format: json
register: result
until: result | search('green')
until: result | regex_search('green')
retries: 10
delay: 30
when: installed_es_version.results | selectattr("yumstate", "equalto", "installed") | list | length == 0

0 comments on commit 8a81ff2

Please sign in to comment.