|
| 1 | +# Ansible |
| 2 | +# |
| 3 | +# Rolling restart of Elasticsearch with security on |
| 4 | +# Source from: author: Jeff Steinmetz, @jeffsteinmetz; Bin Li, @holysoros |
| 5 | +# Modifications: author: Daniel Neuberger @netways.de |
| 6 | +# More modifications: NETWAYS Professional Services GmbH |
| 7 | +# latest tested with Ansible 2.9 and later |
| 8 | + |
| 9 | +--- |
| 10 | + |
| 11 | +- name: Set connection protocol to https |
| 12 | + ansible.builtin.set_fact: |
| 13 | + elasticsearch_http_protocol: "https" |
| 14 | + |
| 15 | +- name: Check for running Elasticsearch service |
| 16 | + ansible.builtin.systemd: |
| 17 | + name: elasticsearch |
| 18 | + register: elasticsearch_running |
| 19 | + |
| 20 | +- name: Be careful about upgrade when Elasticsearch is running |
| 21 | + when: |
| 22 | + - elasticsearch_running.status.ActiveState == "active" |
| 23 | + block: |
| 24 | + |
| 25 | + # this step is key!!! Don't restart more nodes |
| 26 | + # until all shards have completed recovery |
| 27 | + - name: Wait for cluster health to return to green |
| 28 | + ansible.builtin.uri: |
| 29 | + url: "{{ elasticsearch_http_protocol }}://localhost:{{ elasticstack_elasticsearch_http_port }}/_cluster/health" |
| 30 | + method: GET |
| 31 | + user: elastic |
| 32 | + password: "{{ elasticstack_password.stdout }}" |
| 33 | + validate_certs: no |
| 34 | + register: response |
| 35 | + until: "response.json.status == 'green'" |
| 36 | + retries: 50 |
| 37 | + delay: 30 |
| 38 | + |
| 39 | + - name: Disable shard allocation for the cluster |
| 40 | + ansible.builtin.uri: |
| 41 | + url: "{{ elasticsearch_http_protocol }}://localhost:{{ elasticstack_elasticsearch_http_port }}/_cluster/settings" |
| 42 | + method: PUT |
| 43 | + body: '{ "persistent": { "cluster.routing.allocation.enable": "none" }}' |
| 44 | + body_format: json |
| 45 | + user: elastic |
| 46 | + password: "{{ elasticstack_password.stdout }}" |
| 47 | + validate_certs: no |
| 48 | + |
| 49 | + - name: Stop non essential indexing to speed up shard recovery |
| 50 | + ansible.builtin.uri: |
| 51 | + url: "{{ elasticsearch_http_protocol }}://localhost:{{ elasticstack_elasticsearch_http_port }}/_flush" |
| 52 | + method: POST |
| 53 | + user: elastic |
| 54 | + password: "{{ elasticstack_password.stdout }}" |
| 55 | + validate_certs: no |
| 56 | + failed_when: false |
| 57 | + |
| 58 | + - name: Shutdown elasticsearch service |
| 59 | + ansible.builtin.service: |
| 60 | + name: elasticsearch |
| 61 | + enabled: yes |
| 62 | + daemon_reload: yes |
| 63 | + state: restarted |
| 64 | + |
| 65 | + - name: Wait for elasticsearch node to come back up if it was stopped |
| 66 | + ansible.builtin.wait_for: |
| 67 | + host: "localhost" |
| 68 | + port: "{{ elasticstack_elasticsearch_http_port }}" |
| 69 | + delay: 30 |
| 70 | + |
| 71 | + - name: Confirm the node joins the cluster # noqa: risky-shell-pipe |
| 72 | + ansible.builtin.shell: > |
| 73 | + if test -n "$(ps -p $$ | grep bash)"; then set -o pipefail; fi; |
| 74 | + curl |
| 75 | + -k |
| 76 | + -u elastic:{{ elasticstack_password.stdout }} |
| 77 | + -s |
| 78 | + -m 2 |
| 79 | + '{{ elasticsearch_http_protocol }}://localhost:{{ elasticstack_elasticsearch_http_port }}/_cat/nodes?h=name' |
| 80 | + | grep |
| 81 | + -E |
| 82 | + '^{{ elasticsearch_nodename }}$' |
| 83 | + register: result |
| 84 | + until: result.rc == 0 |
| 85 | + retries: 200 |
| 86 | + delay: 3 |
| 87 | + changed_when: false |
| 88 | + |
| 89 | + - name: Enable shard allocation for the cluster |
| 90 | + ansible.builtin.uri: |
| 91 | + url: "{{ elasticsearch_http_protocol }}://localhost:{{ elasticstack_elasticsearch_http_port }}/_cluster/settings" |
| 92 | + method: PUT |
| 93 | + body: '{ "persistent": { "cluster.routing.allocation.enable": null }}' |
| 94 | + body_format: json |
| 95 | + user: elastic |
| 96 | + password: "{{ elasticstack_password.stdout }}" |
| 97 | + validate_certs: no |
| 98 | + register: response |
| 99 | + # next line is boolean not string, so no quotes around true |
| 100 | + # use python truthiness |
| 101 | + until: "response.json.acknowledged == true" |
| 102 | + retries: 5 |
| 103 | + delay: 30 |
| 104 | + |
| 105 | + - name: Wait for cluster health to return to yellow or green |
| 106 | + ansible.builtin.uri: |
| 107 | + url: "{{ elasticsearch_http_protocol }}://localhost:{{ elasticstack_elasticsearch_http_port }}/_cluster/health" |
| 108 | + method: GET |
| 109 | + user: elastic |
| 110 | + password: "{{ elasticstack_password.stdout }}" |
| 111 | + validate_certs: no |
| 112 | + register: response |
| 113 | + until: "response.json.status == 'yellow' or response.json.status == 'green'" |
| 114 | + retries: 5 |
| 115 | + delay: 30 |
0 commit comments