Skip to content

Commit 608aa6e

Browse files
authored
Review refactoring (#394)
* Add disclaimer to README about breaking changes * Rename variable `elasticstack_ca` to `elasticstack_ca_host` * Ensure the "global meta" role `elasticstack` is only called once per host
1 parent 6c87b8d commit 608aa6e

File tree

17 files changed

+112
-84
lines changed

17 files changed

+112
-84
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
![Test ElasticStack](https://github.com/NETWAYS/ansible-collection-elasticstack/actions/workflows/test_full_stack.yml/badge.svg)
44

5+
> [!CAUTION]
6+
> The current main branch has some breaking changes. Be careful using it! Everything after commit 3d6673f6d526afeb0fd7ba382d067d76bd10bbd6 is affected!
7+
58
This collection installs and manages the Elastic Stack. It provides roles for every component of the Stack. Furthermore, it is possible to differentiate between Enterprise or OSS releases.
69

710
Every role is documented with all variables, please refer to the documentation found in **[Getting-Started](./docs/getting-started.md)**
@@ -10,6 +13,9 @@ Every role is documented with all variables, please refer to the documentation f
1013

1114
## Roles documentation
1215

16+
> [!NOTE]
17+
> Some roles have fixed requirements that you must observe. Please have a look at the [requirements](docs/01-requirements.md) before using the collection. (There is a high probability that some of them will be refactored soon)
18+
1319
* [Beats](docs/role-beats.md)
1420
* [Elasticsearch](docs/role-elasticsearch.md)
1521
* [Kibana](docs/role-kibana.md)

docs/01-requirements.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Requirements
2+
3+
There are some requirements that the user have to fulfill while using the collection. Some of them will be refactored and disappear from the list soon.
4+
5+
**Inventory names**
6+
7+
The collection provides roles for several tools (elasticsearch, kibana, ...). In some tasks hosts from the specific inventory group are fetched. The group names are customizable via variables. But using different inventory names than provided inside the variables will result in errors.
8+
9+
**elasticstack_ca_host**
10+
11+
This is a mandatory variable. It is used to define the host that will be used as "CA host". Per default it is already defined (first host inside the group `elasticstack_elasticsearch_group_name`). In case you are using different group names, this variable wont be set with a defualt value.

docs/role-elasticsearch.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ The following variable was only integrated to speed up upgrades of non-productio
6060

6161
These variables are identical over all our elastic related roles, hence the different naming schemes.
6262

63-
* *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)
63+
* *elasticstack_ca_host*: 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)
6464
* *elasticstack_ca_name*: Distinguished name of the CA. (default: `CN=Elastic Certificate Tool Autogenerated CA`)
6565
* *elasticstack_ca_pass*: Password for Elasticsearch CA (default: `PleaseChangeMe`)
6666
* *elasticstack_ca_validity_period*: number of days that the generated CA are valid (default: 1095).

docs/role-kibana.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ These variables are identical over all our elastic related roles, hence the diff
3232
* *kibana_cert_will_expire_soon*: Set it to true to renew kibana certificate (default: `false`), Or run the playbook with `--tags renew_kibana_cert` to do that.
3333
* *elasticstack_kibana_host*: Hostname users use to connect to Kibana (default: FQDN of the host the role is executed on)
3434
* *elasticstack_kibana_port*: Port Kibana webinterface is listening on (default: `5601`)
35-
* *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)
35+
* *elasticstack_ca_host*: 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)
3636
* *elasticstack_ca_dir*: Directory where on the Elasticsearch CA host certificates are stored. This is only useful in connection with out other Elastic Stack related roles. (default: `/opt/es-ca`)
3737
* *elasticstack_ca_pass*: Password for Elasticsearch CA (default: `PleaseChangeMe`)
3838
* *elasticstack_initial_passwords*: Path to file with initical elasticsearch passwords (default: `/usr/share/elasticsearch/initial_passwords`)

roles/beats/tasks/beats-security.yml

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
---
2-
32
- name: Ensure beats certificate exists
43
ansible.builtin.stat:
54
path: "/etc/beats/certs/{{ inventory_hostname }}-beats.crt"
@@ -53,9 +52,9 @@
5352
state: absent
5453
when: beats_move_cert_directory.changed
5554

56-
- name: Backup beats certs on elasticstack_ca host then remove
55+
- name: Backup beats certs on elasticstack_ca_host then remove
5756
when: "'renew_beats_cert' in ansible_run_tags or 'renew_ca' in ansible_run_tags or beats_cert_will_expire_soon | bool"
58-
delegate_to: "{{ elasticstack_ca }}"
57+
delegate_to: "{{ elasticstack_ca_host }}"
5958
tags:
6059
- renew_ca
6160
- renew_beats_cert
@@ -134,7 +133,7 @@
134133
--pass {{ beats_tls_key_passphrase }}
135134
--pem
136135
--out {{ elasticstack_ca_dir }}/{{ ansible_hostname }}-beats.zip
137-
delegate_to: "{{ elasticstack_ca }}"
136+
delegate_to: "{{ elasticstack_ca_host }}"
138137
args:
139138
creates: "{{ elasticstack_ca_dir }}/{{ ansible_hostname }}-beats.zip"
140139
tags:
@@ -147,7 +146,7 @@
147146
src: "{{ elasticstack_ca_dir }}/{{ ansible_hostname }}-beats.zip"
148147
dest: "{{ lookup('config', 'DEFAULT_LOCAL_TMP') | dirname }}/{{ ansible_hostname }}-beats.zip"
149148
flat: yes
150-
delegate_to: "{{ elasticstack_ca }}"
149+
delegate_to: "{{ elasticstack_ca_host }}"
151150
tags:
152151
- certificates
153152
- renew_ca
@@ -204,7 +203,7 @@
204203
src: "{{ elasticstack_ca_dir }}/ca.crt"
205204
dest: "{{ lookup('config', 'DEFAULT_LOCAL_TMP') | dirname }}/ca.crt"
206205
flat: yes
207-
delegate_to: "{{ elasticstack_ca }}"
206+
delegate_to: "{{ elasticstack_ca_host }}"
208207
tags:
209208
- certificates
210209
- renew_ca
@@ -234,4 +233,4 @@
234233
register: beats_writer_password
235234
changed_when: false
236235
no_log: "{{ elasticstack_no_log }}"
237-
delegate_to: "{{ elasticstack_ca }}"
236+
delegate_to: "{{ elasticstack_ca_host }}"

roles/beats/tasks/main.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
- name: Include global role
44
ansible.builtin.import_role:
55
name: netways.elasticstack.elasticstack
6+
when: not hostvars[inventory_hostname]._elasticstack_role_imported | default(false)
67

78
- name: Update apt cache.
89
ansible.builtin.apt:

roles/elasticsearch/tasks/elasticsearch-security.yml

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,25 @@
44
ansible.builtin.stat:
55
path: "{{ elasticstack_ca_dir }}/elastic-stack-ca.p12"
66
register: elasticstack_ca_exists
7-
when: inventory_hostname == elasticstack_ca
7+
when: inventory_hostname == elasticstack_ca_host
88

99
- name: Get CA informations
1010
cert_info:
1111
path: "{{ elasticstack_ca_dir }}/elastic-stack-ca.p12"
1212
passphrase: "{{ elasticstack_ca_pass | default(omit, true) }}"
1313
register: elasticstack_ca_infos
14-
when: inventory_hostname == elasticstack_ca and elasticstack_ca_exists.stat.exists | bool
14+
when: inventory_hostname == elasticstack_ca_host and elasticstack_ca_exists.stat.exists | bool
1515

1616
- name: Set the ca expiration date in days
1717
ansible.builtin.set_fact:
1818
elasticstack_ca_expiration_days: "{{ ((elasticstack_ca_infos.not_valid_after | to_datetime()) - (ansible_date_time.date | to_datetime('%Y-%m-%d'))).days }}"
19-
when: inventory_hostname == elasticstack_ca and elasticstack_ca_infos.skipped is not defined
19+
when: inventory_hostname == elasticstack_ca_host and elasticstack_ca_infos.skipped is not defined
2020

2121
- name: Set ca will expire soon to true
2222
ansible.builtin.set_fact:
2323
elasticstack_ca_will_expire_soon: true
2424
when: >
25-
inventory_hostname == elasticstack_ca and
25+
inventory_hostname == elasticstack_ca_host and
2626
elasticstack_ca_expiration_days is defined and
2727
elasticstack_ca_expiration_days | int <= elasticstack_ca_expiration_buffer | int
2828
@@ -32,7 +32,7 @@
3232
Your ca will expire in {{ elasticstack_ca_expiration_days }} days.
3333
Ansible will renew it and all elastic stack certificates
3434
when: >
35-
inventory_hostname == elasticstack_ca and
35+
inventory_hostname == elasticstack_ca_host and
3636
elasticstack_ca_expiration_days is defined and
3737
elasticstack_ca_expiration_days | int <= elasticstack_ca_expiration_buffer | int
3838
@@ -48,7 +48,7 @@
4848
- groups[elasticstack_logstash_group_name] is defined
4949

5050
- name: Backup ca directory on elasticsearch ca host then remove
51-
when: (inventory_hostname == elasticstack_ca) and ('renew_ca' in "ansible_run_tags" or elasticstack_ca_will_expire_soon | bool)
51+
when: (inventory_hostname == elasticstack_ca_host) and ('renew_ca' in "ansible_run_tags" or elasticstack_ca_will_expire_soon | bool)
5252
tags:
5353
- renew_ca
5454
block:
@@ -149,19 +149,19 @@
149149
state: absent
150150
when: elasticsearch_move_cert_directory.changed
151151

152-
- name: Backup elasticsearch certs on elasticstack_ca host then remove
152+
- name: Backup elasticsearch certs on elasticstack_ca_host host then remove
153153
when: "'renew_es_cert' in ansible_run_tags or 'renew_ca' in ansible_run_tags or elasticsearch_cert_will_expire_soon | bool"
154-
delegate_to: "{{ elasticstack_ca }}"
154+
delegate_to: "{{ elasticstack_ca_host }}"
155155
tags:
156156
- renew_ca
157157
- renew_es_cert
158158
block:
159-
- name: Check if cert file exists on elasticstack_ca host
159+
- name: Check if cert file exists on elasticstack_ca_host host
160160
ansible.builtin.stat:
161161
path: "{{ elasticstack_ca_dir }}/{{ ansible_hostname }}.p12"
162162
register: elasticsearch_check_cert_file
163163

164-
- name: Move cert file on elasticstack_ca host
164+
- name: Move cert file on elasticstack_ca_host
165165
ansible.builtin.copy:
166166
src: "{{ elasticstack_ca_dir }}/{{ ansible_hostname }}.p12"
167167
dest: "{{ elasticstack_ca_dir }}/{{ ansible_hostname }}.p12_{{ ansible_date_time.iso8601_micro }}"
@@ -170,7 +170,7 @@
170170
when: elasticsearch_check_cert_file.stat.exists
171171
register: elasticsearch_move_cert_file
172172

173-
- name: Remove cert file on elasticstack_ca host
173+
- name: Remove cert file on elasticstack_ca_host
174174
ansible.builtin.file:
175175
path: "{{ elasticstack_ca_dir }}/{{ ansible_hostname }}.p12"
176176
state: absent
@@ -217,14 +217,14 @@
217217
- name: Import Tasks elasticsearch-keystore.yml
218218
ansible.builtin.import_tasks: elasticsearch-keystore.yml
219219

220-
- name: Create ca and certificates on elasticstack_ca host
221-
when: inventory_hostname == elasticstack_ca
220+
- name: Create ca and certificates on elasticstack_ca_host
221+
when: inventory_hostname == elasticstack_ca_host
222222
tags:
223223
- certificates
224224
- renew_ca
225225
- renew_es_cert
226226
block:
227-
- name: Configure ca on elasticstack_ca host
227+
- name: Configure ca on elasticstack_ca_host
228228
ansible.builtin.command: >
229229
/usr/share/elasticsearch/bin/elasticsearch-certutil ca
230230
--ca-dn {{ elasticstack_ca_name }}
@@ -236,7 +236,7 @@
236236
creates: "{{ elasticstack_ca_dir }}/elastic-stack-ca.p12"
237237
no_log: "{{ elasticstack_no_log }}"
238238

239-
- name: Create node certificates on elasticstack_ca host
239+
- name: Create node certificates on elasticstack_ca_host
240240
ansible.builtin.command: >
241241
/usr/share/elasticsearch/bin/elasticsearch-certutil cert
242242
--ca {{ elasticstack_ca_dir }}/elastic-stack-ca.p12
@@ -266,7 +266,7 @@
266266
src: "{{ elasticstack_ca_dir }}/ca.crt"
267267
dest: "{{ lookup('config', 'DEFAULT_LOCAL_TMP') | dirname }}/ca.crt"
268268
flat: yes
269-
when: inventory_hostname == elasticstack_ca
269+
when: inventory_hostname == elasticstack_ca_host
270270
tags:
271271
- certificates
272272
- renew_ca
@@ -277,7 +277,7 @@
277277
src: "{{ elasticstack_ca_dir }}/{{ ansible_hostname }}.p12"
278278
dest: "{{ lookup('config', 'DEFAULT_LOCAL_TMP') | dirname }}/{{ ansible_hostname }}.p12"
279279
flat: yes
280-
delegate_to: "{{ elasticstack_ca }}"
280+
delegate_to: "{{ elasticstack_ca_host }}"
281281
tags:
282282
- certificates
283283
- renew_ca
@@ -361,7 +361,7 @@
361361
- name: Check for passwords being set
362362
ansible.builtin.stat:
363363
path: "{{ elasticstack_initial_passwords }}"
364-
delegate_to: "{{ elasticstack_ca }}"
364+
delegate_to: "{{ elasticstack_ca_host }}"
365365
register: elasticsearch_passwords_file
366366

367367
- name: Setting elasticsearch_http_protocol
@@ -412,7 +412,7 @@
412412
register: elasticstack_password
413413
changed_when: false
414414
no_log: "{{ elasticstack_no_log }}"
415-
delegate_to: "{{ elasticstack_ca }}"
415+
delegate_to: "{{ elasticstack_ca_host }}"
416416
when: elasticsearch_passwords_file.stat.exists | bool
417417

418418
- name: Check for API availability with elastic password
@@ -499,7 +499,7 @@
499499
if test -n "$(ps -p $$ | grep bash)"; then set -o pipefail; fi;
500500
/usr/share/elasticsearch/bin/elasticsearch-setup-passwords auto -b >
501501
{{ elasticstack_initial_passwords }}
502-
when: inventory_hostname == elasticstack_ca
502+
when: inventory_hostname == elasticstack_ca_host
503503
no_log: "{{ elasticstack_no_log }}"
504504
args:
505505
creates: "{{ elasticstack_initial_passwords }}"
@@ -512,6 +512,6 @@
512512
owner: root
513513
group: root
514514
mode: 0600
515-
when: inventory_hostname == elasticstack_ca
515+
when: inventory_hostname == elasticstack_ca_host
516516

517517
# Maybe make sure that Elasticsearch is using the right protocol http(s) to connect, even in newly setup clusters

roles/elasticsearch/tasks/main.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
- name: Include global role
1111
ansible.builtin.import_role:
1212
name: netways.elasticstack.elasticstack
13+
when: not hostvars[inventory_hostname]._elasticstack_role_imported | default(false)
1314

1415
- name: Update apt cache.
1516
ansible.builtin.apt:
@@ -314,8 +315,8 @@
314315

315316
- name: Show hint about passwords
316317
ansible.builtin.debug:
317-
msg: "Remember, your temporary passwords can be found on {{ elasticstack_ca }} in {{ elasticstack_initial_passwords }}"
318+
msg: "Remember, your temporary passwords can be found on {{ elasticstack_ca_host }} in {{ elasticstack_initial_passwords }}"
318319
when:
319320
- elasticsearch_security | bool
320321
- elasticstack_variant == "elastic"
321-
- inventory_hostname == elasticstack_ca
322+
- inventory_hostname == elasticstack_ca_host

roles/elasticstack/defaults/main.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ elasticstack_logstash_group_name: logstash
55
elasticstack_kibana_group_name: kibana
66

77
elasticstack_beats_port: 5044
8+
elasticstack_ca_host: "{{ groups[elasticstack_elasticsearch_group_name][0] }}"
89
elasticstack_ca_dir: /opt/es-ca
910
elasticstack_ca_expiration_buffer: 30
1011
elasticstack_ca_name: "CN=Elastic Certificate Tool Autogenerated CA"

roles/elasticstack/tasks/elasticstack-passwords.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
- name: Check for passwords being set
44
ansible.builtin.stat:
55
path: "{{ elasticstack_initial_passwords }}"
6-
delegate_to: "{{ elasticstack_ca }}"
6+
delegate_to: "{{ elasticstack_ca_host }}"
77
register: elasticsearch_passwords_file
88

99
- name: Fetch Elastic password # noqa: risky-shell-pipe
@@ -14,5 +14,5 @@
1414
register: elasticstack_password
1515
changed_when: false
1616
no_log: "{{ elasticstack_no_log }}"
17-
delegate_to: "{{ elasticstack_ca }}"
17+
delegate_to: "{{ elasticstack_ca_host }}"
1818
when: elasticsearch_passwords_file.stat.exists | bool

0 commit comments

Comments
 (0)