| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright (c) 2013-2014, Christian Berendt <[email protected]> |
| 4 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 6 |
|
| 7 |
from __future__ import annotations |
| 8 |
|
| 9 |
DOCUMENTATION = r""" |
| 10 |
module: apache2_module |
| 11 |
author: |
| 12 |
- Christian Berendt (@berendt) |
| 13 |
- Ralf Hertel (@n0trax) |
| 14 |
- Robin Roth (@robinro) |
| 15 |
short_description: Enables/disables a module of the Apache2 webserver |
| 16 |
description: |
| 17 |
- Enables or disables a specified module of the Apache2 webserver. |
| 18 |
extends_documentation_fragment: |
| 19 |
- community.general._attributes |
| 20 |
attributes: |
| 21 |
check_mode: |
| 22 |
support: full |
| 23 |
diff_mode: |
| 24 |
support: none |
| 25 |
options: |
| 26 |
name: |
| 27 |
type: str |
| 28 |
description: |
| 29 |
- Name of the module to enable/disable as given to C(a2enmod)/C(a2dismod). |
| 30 |
required: true |
| 31 |
identifier: |
| 32 |
type: str |
| 33 |
description: |
| 34 |
- Identifier of the module as listed by C(apache2ctl -M). This is optional and usually determined automatically by the |
| 35 |
common convention of appending V(_module) to O(name) as well as custom exception for popular modules. |
| 36 |
force: |
| 37 |
description: |
| 38 |
- Force disabling of default modules and override Debian warnings. |
| 39 |
type: bool |
| 40 |
default: false |
| 41 |
state: |
| 42 |
type: str |
| 43 |
description: |
| 44 |
- Desired state of the module. |
| 45 |
choices: ['present', 'absent'] |
| 46 |
default: present |
| 47 |
ignore_configcheck: |
| 48 |
description: |
| 49 |
- Ignore configuration checks about inconsistent module configuration. Especially for mpm_* modules. |
| 50 |
type: bool |
| 51 |
default: false |
| 52 |
warn_mpm_absent: |
| 53 |
description: |
| 54 |
- Control the behavior of the warning process for MPM modules. |
| 55 |
type: bool |
| 56 |
default: true |
| 57 |
version_added: 6.3.0 |
| 58 |
requirements: ["a2enmod", "a2dismod"] |
| 59 |
notes: |
| 60 |
- This does not work on RedHat-based distributions. It does work on Debian- and SuSE-based distributions. Whether it works |
| 61 |
on others depend on whether the C(a2enmod) and C(a2dismod) tools are available or not. |
| 62 |
""" |
| 63 |
|
| 64 |
EXAMPLES = r""" |
| 65 |
- name: Enable the Apache2 module wsgi |
| 66 |
community.general.apache2_module: |
| 67 |
state: present |
| 68 |
name: wsgi |
| 69 |
|
| 70 |
- name: Disables the Apache2 module wsgi |
| 71 |
community.general.apache2_module: |
| 72 |
state: absent |
| 73 |
name: wsgi |
| 74 |
|
| 75 |
- name: Disable default modules for Debian |
| 76 |
community.general.apache2_module: |
| 77 |
state: absent |
| 78 |
name: autoindex |
| 79 |
force: true |
| 80 |
|
| 81 |
- name: Disable mpm_worker and ignore warnings about missing mpm module |
| 82 |
community.general.apache2_module: |
| 83 |
state: absent |
| 84 |
name: mpm_worker |
| 85 |
ignore_configcheck: true |
| 86 |
|
| 87 |
- name: Disable mpm_event, enable mpm_prefork and ignore warnings about missing mpm module |
| 88 |
community.general.apache2_module: |
| 89 |
name: "{{ item.module }}" |
| 90 |
state: "{{ item.state }}" |
| 91 |
warn_mpm_absent: false |
| 92 |
ignore_configcheck: true |
| 93 |
loop: |
| 94 |
- module: mpm_event |
| 95 |
state: absent |
| 96 |
- module: mpm_prefork |
| 97 |
state: present |
| 98 |
|
| 99 |
- name: Enable dump_io module, which is identified as dumpio_module inside apache2 |
| 100 |
community.general.apache2_module: |
| 101 |
state: present |
| 102 |
name: dump_io |
| 103 |
identifier: dumpio_module |
| 104 |
""" |
| 105 |
|
| 106 |
RETURN = r""" |
| 107 |
result: |
| 108 |
description: Message about action taken. |
| 109 |
returned: always |
| 110 |
type: str |
| 111 |
""" |
| 112 |
|
| 113 |
import re |
| 114 |
|
| 115 |
# import module snippets |
| 116 |
from ansible.module_utils.basic import AnsibleModule |
| 117 |
|
| 118 |
_re_threaded = re.compile(r"threaded: *yes") |
| 119 |
|
| 120 |
|
| 121 |
def _run_threaded(module): |
| 122 |
control_binary = _get_ctl_binary(module) |
| 123 |
result, stdout, stderr = module.run_command([control_binary, "-V"]) |
| 124 |
|
| 125 |
return bool(_re_threaded.search(stdout)) |
| 126 |
|
| 127 |
|
| 128 |
def _get_ctl_binary(module): |
| 129 |
for command in ["apache2ctl", "apachectl"]: |
| 130 |
ctl_binary = module.get_bin_path(command) |
| 131 |
if ctl_binary is not None: |
| 132 |
return ctl_binary |
| 133 |
|
| 134 |
module.fail_json(msg="Neither of apache2ctl nor apachectl found. At least one apache control binary is necessary.") |
| 135 |
|
| 136 |
|
| 137 |
def _module_is_enabled(module): |
| 138 |
control_binary = _get_ctl_binary(module) |
| 139 |
result, stdout, stderr = module.run_command([control_binary, "-M"]) |
| 140 |
|
| 141 |
if result != 0: |
| 142 |
error_msg = f"Error executing {control_binary}: {stderr}" |
| 143 |
if module.params["ignore_configcheck"]: |
| 144 |
if "AH00534" in stderr and "mpm_" in module.params["name"]: |
| 145 |
if module.params["warn_mpm_absent"]: |
| 146 |
module.warn( |
| 147 |
"No MPM module loaded! apache2 reload AND other module actions" |
| 148 |
" will fail if no MPM module is loaded immediately." |
| 149 |
) |
| 150 |
else: |
| 151 |
module.warn(error_msg) |
| 152 |
return False |
| 153 |
else: |
| 154 |
module.fail_json(msg=error_msg) |
| 155 |
|
| 156 |
searchstring = f" {module.params['identifier']}" |
| 157 |
return searchstring in stdout |
| 158 |
|
| 159 |
|
| 160 |
def create_apache_identifier(name): |
| 161 |
""" |
| 162 |
By convention if a module is loaded via name, it appears in apache2ctl -M as |
| 163 |
name_module. |
| 164 |
|
| 165 |
Some modules don't follow this convention and we use replacements for those.""" |
| 166 |
|
| 167 |
# a2enmod name replacement to apache2ctl -M names |
| 168 |
text_workarounds = [ |
| 169 |
("shib", "mod_shib"), |
| 170 |
("shib2", "mod_shib"), |
| 171 |
("evasive", "evasive20_module"), |
| 172 |
] |
| 173 |
|
| 174 |
# re expressions to extract subparts of names |
| 175 |
re_workarounds = [ |
| 176 |
("php8", re.compile(r"^(php)[\d\.]+")), |
| 177 |
("php", re.compile(r"^(php\d)\.")), |
| 178 |
] |
| 179 |
|
| 180 |
for a2enmod_spelling, module_name in text_workarounds: |
| 181 |
if a2enmod_spelling in name: |
| 182 |
return module_name |
| 183 |
|
| 184 |
for search, reexpr in re_workarounds: |
| 185 |
if search in name: |
| 186 |
try: |
| 187 |
rematch = reexpr.search(name) |
| 188 |
return f"{rematch.group(1)}_module" |
| 189 |
except AttributeError: |
| 190 |
pass |
| 191 |
|
| 192 |
return f"{name}_module" |
| 193 |
|
| 194 |
|
| 195 |
def _set_state(module, state): |
| 196 |
name = module.params["name"] |
| 197 |
force = module.params["force"] |
| 198 |
|
| 199 |
want_enabled = state == "present" |
| 200 |
state_string = {"present": "enabled", "absent": "disabled"}[state] |
| 201 |
a2mod_binary = {"present": "a2enmod", "absent": "a2dismod"}[state] |
| 202 |
success_msg = f"Module {name} {state_string}" |
| 203 |
|
| 204 |
if _module_is_enabled(module) != want_enabled: |
| 205 |
if module.check_mode: |
| 206 |
module.exit_json(changed=True, result=success_msg) |
| 207 |
|
| 208 |
a2mod_binary_path = module.get_bin_path(a2mod_binary) |
| 209 |
if a2mod_binary_path is None: |
| 210 |
module.fail_json( |
| 211 |
msg=f"{a2mod_binary} not found. Perhaps this system does not use {a2mod_binary} to manage apache" |
| 212 |
) |
| 213 |
|
| 214 |
a2mod_binary_cmd = [a2mod_binary_path] |
| 215 |
|
| 216 |
if not want_enabled and force: |
| 217 |
# force exists only for a2dismod on debian |
| 218 |
a2mod_binary_cmd.append("-f") |
| 219 |
|
| 220 |
result, stdout, stderr = module.run_command(a2mod_binary_cmd + [name]) |
| 221 |
|
| 222 |
if _module_is_enabled(module) == want_enabled: |
| 223 |
module.exit_json(changed=True, result=success_msg) |
| 224 |
else: |
| 225 |
msg = ( |
| 226 |
f"Failed to set module {name} to {state_string}:\n" |
| 227 |
f"{stdout}\n" |
| 228 |
f"Maybe the module identifier ({module.params['identifier']}) was guessed incorrectly." |
| 229 |
'Consider setting the "identifier" option.' |
| 230 |
) |
| 231 |
module.fail_json(msg=msg, rc=result, stdout=stdout, stderr=stderr) |
| 232 |
else: |
| 233 |
module.exit_json(changed=False, result=success_msg) |
| 234 |
|
| 235 |
|
| 236 |
def main(): |
| 237 |
module = AnsibleModule( |
| 238 |
argument_spec=dict( |
| 239 |
name=dict(required=True), |
| 240 |
identifier=dict(type="str"), |
| 241 |
force=dict(type="bool", default=False), |
| 242 |
state=dict(default="present", choices=["absent", "present"]), |
| 243 |
ignore_configcheck=dict(type="bool", default=False), |
| 244 |
warn_mpm_absent=dict(type="bool", default=True), |
| 245 |
), |
| 246 |
supports_check_mode=True, |
| 247 |
) |
| 248 |
module.run_command_environ_update = {"LANGUAGE": "C", "LC_ALL": "C"} |
| 249 |
|
| 250 |
name = module.params["name"] |
| 251 |
if name == "cgi" and module.params["state"] == "present" and _run_threaded(module): |
| 252 |
module.fail_json(msg="Your MPM seems to be threaded, therefore enabling cgi module is not allowed.") |
| 253 |
|
| 254 |
if not module.params["identifier"]: |
| 255 |
module.params["identifier"] = create_apache_identifier(module.params["name"]) |
| 256 |
|
| 257 |
if module.params["state"] in ["present", "absent"]: |
| 258 |
_set_state(module, module.params["state"]) |
| 259 |
|
| 260 |
|
| 261 |
if __name__ == "__main__": |
| 262 |
main() |