Skip to content

Commit b9c8111

Browse files
widhalmtxtruthx
andauthored
Add code for automatic updates (#250)
Adding playbook contributed by @xtruthx I have to admit, I kept this playbook for waaaaay too long. @xtruthx sent it to me a long while back and I always planned to integrate it into the codebase of this collection. Now I had to face that I took too long and so I put it up publicly. Now I worked it over so that it will fit into the `elasticsearch` role just well. This PR also introduces a new role where all "global" variables can be found. This will help determining the correct version of a package to install. Some tasks that were repeated in every role get moved into this role called `elasticstack` to streamline execution. This PR now reads the current version of Elasticsearch installed on the CA host and uses it as new installation target for all further installations. This not only helps with upgrading, it will also make sure that you won't have a version mismatch if you install components after the initial install. Special thanks to @hryamzik for the workaround to run a task file like with `serial: 1`. Found in ansible/ansible#12170 (comment) Very special thanks to CID GmbH for sponsoring parts of the the development of this PR. fixes #216 fixes #286 fixes #236 fixes #205 --------- Co-authored-by: Daniel Neuberger <[email protected]>
1 parent 0528dec commit b9c8111

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+447
-236
lines changed

NOTICE.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44

55
Here's a list of sponsors who contributed by having the collection improved via outsourcing to NETWAYS.
66

7-
* CID GmbH : Thank you so much for sponsoring. Especially the feature to have different types of Elasticsearch nodes in the cluster.
7+
* CID GmbH : Thank you so much for sponsoring. Especially the feature to have different types of Elasticsearch nodes in the cluster and the ingetration of rolling upgrades.

README.md

+12-4
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,13 @@ You will want to have reliable DNS resolution or enter all hosts of the stack in
7979

8080
The variable `elasticstack_no_log` can be set to `false` if you want to see the output of all tasks. It defaults to `true` because some tasks could reveal passwords in production.
8181

82-
### Versioning
82+
### Versions and upgrades
8383

84-
*elasticstack_version*: Version number of tools to install. Only set if you don't want the latest. (default: none).
84+
*elasticstack_version*: Version number of tools to install. Only set if you don't want the latest on new setups. (default: none). If you already have an installation of Elastic Stack, this collection will query the version of Elasticsearch on the CA host and use it for all further installations in the same setup. (Only if you run the `elasticsearch` role before all others) Example: `7.17.2`
8585

86-
*elasticstack_release*: Major release version of Elastic stack to configure. (default: `7`)
86+
*elasticstack_release*: Major release version of Elastic stack to configure. (default: `7`) Make sure it corresponds to `elasticstack_version` if you set both.
8787

88-
For OSS version see `elasticstack_variant` below. **IMPORTANT** Do not change the version once you have set up the stack. There are unpredictable effects to be expected when using this for upgrades. And upgrade mechanism is already on it's way. (default: none. Example: `7.17.2`)
88+
For OSS version see `elasticstack_variant` below.
8989

9090
*elasticstack_variant*: Variant of the stack to install. Valid values: `elastic` or `oss`. (default: `elastic`)
9191

@@ -99,6 +99,14 @@ roles:
9999
elasticstack_version: 8.8.1
100100
```
101101
102+
#### Upgrades ####
103+
104+
Set `elasticstack_version` to the version you want to upgrade to. Positively do read and understand Elastics changelog and "breaking changes" of your target version and all between your current and the target version. Do not use unless you have a valid backup.
105+
106+
If an upgrade fails, you can try re-running the collection with the same settings. There are several tasks that can provide "self-healing". Please do not rely on these mechanisms, they are more of a "convenience recovery" for easier steps.
107+
108+
The collection will make sure to upgrade Elasticsearch nodes one by one.
109+
102110
### Default Passwords
103111

104112
Default passwords can be seen during generation, or found later in `/usr/share/elasticsearch/initial_passwords`

docs/role-elasticsearch.md

+5
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Role Variables
1313
--------------
1414

1515
* *elasticsearch_node_types*: List of types of this very node. Please refer to [official docs](https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-node.html) for details. (default: not set. allowed value: array of types)
16+
+ *elasticsearch_nodename*': Node name of the Elasticsearch node. (default: value of `ansible_hostname`)
1617
* *elasticsearch_clustername*: Name the Elasticsearch Cluster (default: `elasticsearch`)
1718
* *elasticsearch_heap*: Heapsize for Elasticsearch. (Half of free memory on host. Maximum 30GB. (default: Half of hosts memory. Min 1GB, Max 30GB)
1819
* *elasticsearch_tls_key_passphrase*: Passphrase for elasticsearch certificates (default: `PleaseChangeMeIndividually`)
@@ -53,6 +54,10 @@ This variable activates a workaround to start on systems that have certain harde
5354
* *elasticsearch_seed_hosts*: Set elasticsearch seed hosts
5455
* *elasticsearch_security_enrollment*: Controls enrollment (of nodes and Kibana) to a local node that’s been autoconfigured for security.
5556

57+
The following variable was only integrated to speed up upgrades of non-production clusters. Use with caution and at your own risk:
58+
59+
* *elasticsearch_unsafe_upgrade_restart*: This will still perform rolling upgrades, but will first update the package and then restart the service. In contrast the default behaviour is to stop the service, do the upgrade and then start again. (default: `false`)
60+
5661
These variables are identical over all our elastic related roles, hence the different naming schemes.
5762

5863
* *elasticstack_ca*: Set to the inventory hostname of the host that should house the CA for certificates for inter-node communication. (default: First node in the `elasticsearch` host group)

molecule/elasticstack_default/verify.yml

-1
Original file line numberDiff line numberDiff line change
@@ -136,4 +136,3 @@
136136
success_msg: "'{{ item }}' was found in nodes.content"
137137
with_inventory_hostnames: all
138138
when: groups[elasticstack_elasticsearch_group_name] | length > 1
139-

roles/beats/defaults/main.yml

-18
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ beats_auditbeat: false
66
beats_metricbeat: false
77
beats_target_hosts:
88
- localhost
9-
elasticstack_beats_port: 5044
109
beats_logging: file
1110
beats_logpath: /var/log/beats
1211
beats_loglevel: info
@@ -58,23 +57,6 @@ beats_metricbeat_modules:
5857
- system
5958
beats_metricbeat_loadbalance: true
6059

61-
elasticstack_release: 8
62-
elasticstack_full_stack: true
63-
elasticstack_variant: elastic
64-
elasticstack_security: true
65-
66-
elasticstack_elasticsearch_group_name: elasticsearch
67-
elasticstack_logstash_group_name: logstash
68-
69-
elasticstack_ca_dir: /opt/es-ca
70-
elasticstack_ca_pass: PleaseChangeMe
71-
elasticstack_initial_passwords: /usr/share/elasticsearch/initial_passwords
72-
elasticstack_elasticsearch_http_port: 9200
73-
elasticstack_no_log: true
7460
beats_cert_validity_period: 1095
7561
beats_cert_expiration_buffer: "+30d"
7662
beats_cert_will_expire_soon: false
77-
78-
# Variables for debugging and development
79-
80-
elasticstack_override_beats_tls: false

roles/beats/tasks/auditbeat.yml

+6
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,26 @@
1616
name: "{{ beats_auditbeat_package }}"
1717
enablerepo:
1818
- 'elastic-{{ elasticstack_release }}.x'
19+
notify:
20+
- Restart Auditbeat
1921
when:
2022
- ansible_os_family == "RedHat"
2123
- elasticstack_full_stack | bool
2224

2325
- name: Install Auditbeat - rpm - standalone
2426
ansible.builtin.package:
2527
name: "{{ beats_auditbeat_package }}"
28+
notify:
29+
- Restart Auditbeat
2630
when:
2731
- ansible_os_family == "RedHat"
2832
- not elasticstack_full_stack | bool
2933

3034
- name: Install Auditbeat - deb
3135
ansible.builtin.package:
3236
name: "{{ beats_auditbeat_package }}"
37+
notify:
38+
- Restart Auditbeat
3339
when:
3440
- ansible_os_family == "Debian"
3541

roles/beats/tasks/beats-security.yml

-12
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,5 @@
11
---
22

3-
- name: Install packages for security tasks
4-
ansible.builtin.package:
5-
name:
6-
- unzip
7-
- python3-cryptography
8-
- openssl
9-
tags:
10-
- certificates
11-
- renew_ca
12-
- renew_kibana_cert
13-
- renew_beats_cert
14-
153
- name: Ensure beats certificate exists
164
ansible.builtin.stat:
175
path: "/etc/beats/certs/{{ inventory_hostname }}-beats.crt"

roles/beats/tasks/filebeat.yml

+6
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,26 @@
1515
name: "{{ beats_filebeat_package }}"
1616
enablerepo:
1717
- 'elastic-{{ elasticstack_release }}.x'
18+
notify:
19+
- Restart Filebeat
1820
when:
1921
- ansible_os_family == "RedHat"
2022
- elasticstack_full_stack | bool
2123

2224
- name: Install Filebeat - rpm - standalone
2325
ansible.builtin.package:
2426
name: "{{ beats_filebeat_package }}"
27+
notify:
28+
- Restart Filebeat
2529
when:
2630
- ansible_os_family == "RedHat"
2731
- not elasticstack_full_stack | bool
2832

2933
- name: Install Filebeat - deb
3034
ansible.builtin.package:
3135
name: "{{ beats_filebeat_package }}"
36+
notify:
37+
- Restart Filebeat
3238
when:
3339
- ansible_os_family == "Debian"
3440

roles/beats/tasks/main.yml

+3-17
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
---
22

3-
- name: Include OS specific vars
4-
ansible.builtin.include_vars: '{{ item }}'
5-
with_first_found:
6-
- '{{ ansible_os_family }}_{{ ansible_distribution_major_version }}.yml'
7-
- '{{ ansible_os_family }}.yml'
3+
- name: Include global role
4+
ansible.builtin.import_role:
5+
name: netways.elasticstack.elasticstack
86

97
- name: Update apt cache.
108
ansible.builtin.apt:
@@ -25,18 +23,6 @@
2523
- elasticstack_variant != "oss"
2624
- not elasticstack_override_beats_tls | bool
2725

28-
- name: Set elasticstack_ca variable if not already done by user
29-
ansible.builtin.set_fact:
30-
elasticstack_ca: "{{ groups[elasticstack_elasticsearch_group_name][0] }}"
31-
when:
32-
- beats_security | bool
33-
- elasticstack_ca is undefined
34-
- groups[elasticstack_elasticsearch_group_name] is defined
35-
tags:
36-
- certificates
37-
- renew_ca
38-
- renew_beats_cert
39-
4026
- name: Set beats_ca_dir if whole stack is used
4127
ansible.builtin.set_fact:
4228
beats_ca_dir: "/etc/beats/certs"

roles/beats/tasks/metricbeat.yml

+6
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,26 @@
1616
name: "{{ beats_metricbeat_package }}"
1717
enablerepo:
1818
- 'elastic-{{ elasticstack_release }}.x'
19+
notify:
20+
- Restart Metricbeat
1921
when:
2022
- ansible_os_family == "RedHat"
2123
- elasticstack_full_stack | bool
2224

2325
- name: Install Metricbeat - rpm - standalone
2426
ansible.builtin.package:
2527
name: "{{ beats_metricbeat_package }}"
28+
notify:
29+
- Restart Metricbeat
2630
when:
2731
- ansible_os_family == "RedHat"
2832
- not elasticstack_full_stack | bool
2933

3034
- name: Install Metricbeat - deb
3135
ansible.builtin.package:
3236
name: "{{ beats_metricbeat_package }}"
37+
notify:
38+
- Restart Metricbeat
3339
when:
3440
- ansible_os_family == "Debian"
3541

roles/beats/vars/Debian.yml

-4
This file was deleted.

roles/beats/vars/RedHat.yml

-4
This file was deleted.

roles/beats/vars/main.yml

-2
This file was deleted.

roles/elasticsearch/defaults/main.yml

+3-24
Original file line numberDiff line numberDiff line change
@@ -31,39 +31,18 @@ elasticsearch_heap_dump_path: "/var/lib/elasticsearch"
3131

3232
elasticsearch_jna_workaround: false
3333

34-
# The following variables are to be used when activating security
35-
# They follow a different naming scheme to show that they are global
36-
# to our set of Elastic Stack related Ansible roles
37-
38-
# elasticstack_ca: First host in the `elasticsearch` group
39-
elasticstack_ca_dir: /opt/es-ca
40-
elasticstack_initial_passwords: /usr/share/elasticsearch/initial_passwords
4134
elasticsearch_initialized_file: "{{ elasticstack_initial_passwords | dirname }}/cluster_initialized"
42-
elasticstack_ca_name: "CN=Elastic Certificate Tool Autogenerated CA"
43-
elasticstack_ca_pass: PleaseChangeMe
44-
elasticstack_ca_validity_period: 1095
4535
elasticsearch_tls_key_passphrase: PleaseChangeMeIndividually
4636
elasticsearch_cert_validity_period: 1095
47-
elasticstack_ca_expiration_buffer: 30
4837
elasticsearch_cert_expiration_buffer: 30
49-
elasticstack_ca_will_expire_soon: false
5038
elasticsearch_cert_will_expire_soon: false
5139
elasticsearch_ssl_verification_mode: full
5240

41+
# use this only for non-prod environments and at your own risk!
42+
elasticsearch_unsafe_upgrade_restart: false
43+
5344
# only used internally
5445
elasticsearch_freshstart:
5546
changed: false
5647
elasticsearch_freshstart_security:
5748
changed: false
58-
59-
# "global" variables for all roles
60-
61-
elasticstack_release: 8
62-
elasticstack_full_stack: true
63-
elasticstack_variant: elastic
64-
elasticstack_elasticsearch_http_port: 9200
65-
elasticstack_no_log: true
66-
67-
elasticstack_elasticsearch_group_name: elasticsearch
68-
elasticstack_logstash_group_name: logstash
69-
elasticstack_kibana_group_name: kibana

0 commit comments

Comments
 (0)