Skip to content

Commit ba93212

Browse files
authored
Fix Python module installation (#336)
There are several ways of installing Python modules on a system. We can't make safe assumptions about the system where the collection is used. Possible problems: * Modules available via package manager can be too old * Python could be entirely managed via package manager and pip wouldn't break that * Pip could be installed in different ways This change will do the following: * Install `pip` via package manager if a new variable is set * Try to install modules via pip * If that fails because Python is managed via package manager, try installing them via package manager * One of the above methods should be successful but you still might end up with modules which are too old to work * If that's the case you can force the installation via pip and break the managed Python installation fixes #334
1 parent cc26a73 commit ba93212

File tree

3 files changed

+47
-2
lines changed

3 files changed

+47
-2
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ Every role is documented with all variables, please refer to the documentation f
2121
* [elasticsearch_role](docs/module-elasticsearch_role.md)
2222
* [elasticsearch_user](docs/module-elasticsearch_user.md)
2323

24+
## Global variables
25+
26+
* `elasticstack_force_pip`: Will force installation of required Python modules via `pip`. This is useful if your package manager doesn't provide current versions of modules. (Default: `false`) See [PEP668](https://peps.python.org/pep-0668/) for more details.
27+
* `elasticstack_manage_pip`: Will install `pip` on your system. (Default: `false`)
28+
2429
## Installation
2530

2631
You can easily install the collection with the `ansible-galaxy` command.

roles/elasticstack/defaults/main.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ elasticstack_repo_key: https://artifacts.elastic.co/GPG-KEY-elasticsearch
2222
elasticstack_rpm_workaround: false
2323
elasticstack_security: true
2424
elasticstack_variant: elastic
25+
elasticstack_force_pip: false
26+
elasticstack_manage_pip: false
2527

2628
# for debugging only
2729
elasticstack_no_log: true

roles/elasticstack/tasks/packages.yml

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,51 @@
2020
- renew_es_cert
2121
- renew_logstash_cert
2222

23+
# We don't know how the user made `pip` available and we don't want
24+
# to mess with the installation.
25+
# But we need a way to install our dependencies
26+
2327
- name: Install packages for module dependencies
2428
ansible.builtin.package:
2529
name:
26-
- python3
2730
- python3-pip
31+
when:
32+
- elasticstack_manage_pip | bool
33+
34+
# The following block will try to install modules via pip
35+
# If that fails it will try to install them via package manager
36+
# Packets are sometimes too old as a dependency, so pip is
37+
# preferred
38+
# If that's the case the next task will allow breaking
39+
# systems Python and just install it via pip no matter what
40+
41+
- name: Install Python Modules as dependencies
42+
when:
43+
- not elasticstack_force_pip | bool
44+
block:
45+
- name: Install Python Modules via pip
46+
ansible.builtin.pip:
47+
name:
48+
- elasticsearch
49+
- cryptography
50+
ignore_errors: true
51+
register: elasticstack_pip_installation
52+
53+
- name: Install Python Module via package manager
54+
ansible.builtin.package:
55+
name:
56+
- python3-elasticsearch
57+
- python3-cryptography
58+
when:
59+
- elasticstack_pip_installation
2860

29-
- name: Install Elasticsearch Python Module
61+
- name: Install Python Modules via pip - forced
3062
ansible.builtin.pip:
3163
name:
3264
- elasticsearch
65+
- cryptography
66+
break_system_packages: true
67+
ignore_errors: true
68+
when:
69+
- elasticstack_force_pip | bool
70+
register: elasticstack_pip_installation_forced

0 commit comments

Comments
 (0)