-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.yml
260 lines (226 loc) · 7.87 KB
/
main.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
---
- name: Update apt cache.
ansible.builtin.apt:
update_cache: yes
cache_valid_time: 600
changed_when: false
when: ansible_os_family == 'Debian'
- name: Check-set-parameters
ansible.builtin.include_tasks: elasticsearch-parameters.yml
- name: Include OS specific vars
ansible.builtin.include_vars: '{{ item }}'
with_first_found:
- '{{ ansible_os_family }}_{{ ansible_distribution_major_version }}.yml'
- '{{ ansible_os_family }}.yml'
- name: Set common password for common certificates
ansible.builtin.set_fact:
elasticsearch_tls_key_passphrase: "{{ elasticstack_cert_pass }}"
when:
- elasticstack_cert_pass is defined
tags:
- certificates
- renew_ca
- renew_es_cert
- name: Check if cluster is already set up
ansible.builtin.stat:
path: "{{ elasticsearch_initialized_file }}"
register: cluster_setup_check
failed_when: false
- name: Set var that cluster is set up
ansible.builtin.set_fact:
elaticsearch_cluster_set_up: true
when:
- cluster_setup_check.stat.exists | bool
- cluster_setup_check is defined
- name: Set var that cluster is not set up
ansible.builtin.set_fact:
elaticsearch_cluster_set_up: false
when:
- cluster_setup_check is undefined or
not cluster_setup_check.stat.exists | bool
- name: Check if master node count is correct
when:
- elasticsearch_node_types is defined
block:
# the following is a way to simplify handling information
# about nodes. For some tasks it's only important if a node
# has a specific roles and the others are irrelevant
#
- name: Set node role variable to master
ansible.builtin.set_fact:
elasticsearch_role: "master"
when:
- "'master' in elasticsearch_node_types"
- name: Set node role variable to data
ansible.builtin.set_fact:
elasticsearch_role: "data"
when:
- "'master' not in elasticsearch_node_types"
- "'data' in elasticsearch_node_types"
- name: Set node role variable to other
ansible.builtin.set_fact:
elasticsearch_role: "other"
when:
- "'master' not in elasticsearch_node_types"
- "'data' not in elasticsearch_node_types"
- name: Create groups of nodes
ansible.builtin.group_by:
key: "elasticsearch_role_{{ elasticsearch_role }}"
changed_when: false
- name: Count master nodes
ansible.builtin.set_fact:
count_of_master_nodes: "{{ groups['elasticsearch_role_master'] | length }}"
- name: Check count of master nodes
ansible.builtin.fail:
msg: "There must be an odd count of master nodes. You have {{ count_of_master_nodes }}"
when:
- count_of_master_nodes | int % 2 == 0
- name: End play in checks
ansible.builtin.meta: end_host
when: elasticsearch_check_calculation | bool
- name: Install openssl if security is activated
ansible.builtin.package:
name: openssl
when: elasticsearch_security | bool
# the following should be done by the rpm but failed with 7.4
- name: Set ulimits for Elasticsearch
community.general.pam_limits:
limit_item: nofile
domain: elasticsearch
value: 65535
limit_type: hard
when:
- elasticsearch_pamlimits | bool
- name: Construct exact name of Elasticsearch package
ansible.builtin.set_fact:
elasticsearch_package: >
{{
'elasticsearch' +
('-oss' if elasticstack_variant == 'oss' else '') +
(elasticstack_versionseparator +
elasticstack_version |
string if elasticstack_version is defined else '') |
replace(' ', '')
}}
- name: Install Elasticsearch - rpm
ansible.builtin.package:
name: "{{ elasticsearch_package }}"
enablerepo:
- 'elastic-{% if elasticstack_variant == "oss" %}oss-{% endif %}{{ elasticstack_release }}.x'
when:
- ansible_os_family == "RedHat"
- name: Install Elasticsearch - deb
ansible.builtin.package:
name: "{{ elasticsearch_package }}"
when:
- ansible_os_family == "Debian"
- name: Configure Elasticsearch
ansible.builtin.template:
src: elasticsearch.yml.j2
dest: /etc/elasticsearch/elasticsearch.yml
owner: root
group: root
mode: 0644
backup: "{{ elasticsearch_config_backup }}"
notify:
- Restart Elasticsearch
when: elasticsearch_manage_yaml | bool
- name: Create Elasticsearch directory
ansible.builtin.file:
path: "{{ item.path }}"
state: directory
owner: elasticsearch
group: elasticsearch
mode: "2750"
when: item.create | bool
loop:
- {create: "{{ elasticsearch_create_logpath }}", path: "{{ elasticsearch_logpath }}" }
- {create: "{{ elasticsearch_create_datapath }}", path: "{{ elasticsearch_datapath }}" }
- name: Activate JNA workaround (see README.md)
ansible.builtin.lineinfile:
path: "{{ elasticsearch_sysconfig_file }}"
regexp: 'ES_JAVA_OPTS='
line: 'ES_JAVA_OPTS="-Djna.tmpdir={{ elasticsearch_datapath }}/tmp"'
notify:
- Restart Elasticsearch
when: elasticsearch_jna_workaround | bool
- name: Copy jvm.options File
become: yes
ansible.builtin.template:
src: "{{ elasticsearch_config_jvm }}"
dest: "{{ elasticsearch_conf_dir }}/jvm.options"
owner: root
group: "{{ elasticsearch_group }}"
mode: "660"
force: yes
notify: Restart Elasticsearch
# On containerized Debian 10 systemd will not recognize elasticsearch service
- name: Force systemd to reread configs on container
ansible.builtin.systemd:
daemon_reload: true
when: ansible_virtualization_type == "container" or ansible_virtualization_type == "docker"
# Free up some space to let elsticsearch allocate replica in GitHub Action
- name: Remove cache
ansible.builtin.command: >
rm -rf /var/cache/*
changed_when: false
when: ansible_virtualization_type == "container" or ansible_virtualization_type == "docker"
- name: Import Tasks elasticsearch-security.yml
ansible.builtin.import_tasks: elasticsearch-security.yml
when:
- elasticsearch_security | bool
- elasticstack_variant == "elastic"
tags:
- certificates
- renew_ca
- renew_es_cert
- name: Start Elasticsearch
ansible.builtin.service:
name: elasticsearch
state: started
enabled: yes
register: elasticsearch_freshstart
- name: Handle cluster setup without security
when: not elasticsearch_security | bool
block:
- name: Check for cluster status without security
ansible.builtin.uri:
url: "http://localhost:{{ elasticstack_elasticsearch_http_port }}/_cluster/health?pretty"
register: elasticsearch_cluster_status
ignore_errors: "{{ ansible_check_mode }}"
until: elasticsearch_cluster_status.json.status == "green"
retries: 5
delay: 10
no_log: "{{ elasticstack_no_log }}"
- name: Leave a file showing that the cluster is set up
ansible.builtin.template:
dest: "{{ elasticsearch_initialized_file }}"
src: elasticsearch_initialized.j2
owner: root
group: root
mode: "0600"
- name: Set var that cluster is set up
ansible.builtin.set_fact:
elaticsearch_cluster_set_up: true
# See https://github.com/NETWAYS/ansible-collection-elasticstack/issues/137
# for details why we have this task again here
#
- name: Configure Elasticsearch
ansible.builtin.template:
src: elasticsearch.yml.j2
dest: /etc/elasticsearch/elasticsearch.yml
owner: root
group: root
mode: 0644
backup: "{{ elasticsearch_config_backup }}"
when: elasticsearch_manage_yaml | bool
- name: Show Info about heap
ansible.builtin.debug:
msg: "Using {{ elasticsearch_heap | int * 1024 }} of {{ ansible_memtotal_mb }} MB as heap for Elasticsearch"
- name: Show hint about passwords
ansible.builtin.debug:
msg: "Remember, your temporary passwords can be found on {{ elasticstack_ca }} in {{ elasticstack_initial_passwords }}"
when:
- elasticsearch_security | bool
- elasticstack_variant == "elastic"
- inventory_hostname == elasticstack_ca