Skip to content

Commit

Permalink
Fix bug ansible#5328 apache module loading
Browse files Browse the repository at this point in the history
Currently, the apache2_module module parses apache configs
for correctness when enabling or disabling apache2 modules.

This behavior introduced a conflict condition when transitioning
between certain modules, such as mpm_worker and mpm_event.

This change only parses apache's configs during check mode,
otherwise it parses the stdout results of attempted to enabled
or disable modules to determine change state.

Fixes ansible#5328
  • Loading branch information
Michael Gugino committed Nov 4, 2016
1 parent d2106f1 commit 5304dcb
Showing 1 changed file with 26 additions and 15 deletions.
41 changes: 26 additions & 15 deletions web_infrastructure/apache2_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
- apache2_module: state=absent name=wsgi
'''

import platform
import re

def _run_threaded(module):
Expand Down Expand Up @@ -103,31 +104,41 @@ def _set_state(module, state):
a2mod_binary = {'present': 'a2enmod', 'absent': 'a2dismod'}[state]
success_msg = "Module %s %s" % (name, state_string)

if _module_is_enabled(module) != want_enabled:
if module.check_mode:
module.exit_json(changed = True, result = success_msg)
if module.check_mode:
module.exit_json(changed = _module_is_enabled(module) != want_enabled, result = success_msg)

a2mod_binary = module.get_bin_path(a2mod_binary)
if a2mod_binary is None:
module.fail_json(msg="%s not found. Perhaps this system does not use %s to manage apache" % (a2mod_binary, a2mod_binary))
a2mod_binary = module.get_bin_path(a2mod_binary)
if a2mod_binary is None:
module.fail_json(msg="%s not found. Perhaps this system does not use %s to manage apache" % (a2mod_binary, a2mod_binary))

if not want_enabled and force:
# force exists only for a2dismod on debian
a2mod_binary += ' -f'
if not want_enabled and force:
# force exists only for a2dismod on debian
a2mod_binary += ' -f'

result, stdout, stderr = module.run_command("%s %s" % (a2mod_binary, name))
result, stdout, stderr = module.run_command("%s %s" % (a2mod_binary, name))

if _module_is_enabled(module) == want_enabled:
module.exit_json(changed = True, result = success_msg)
else:
module.fail_json(msg="Failed to set module %s to %s: %s" % (name, state_string, stdout), rc=result, stdout=stdout, stderr=stderr)
if result != 0:
module.fail_json(msg="Failed to set module %s to %s: %s" % (name, state_string, stdout), rc=result, stdout=stdout, stderr=stderr)
elif re.match(r'.*(already (disabled|enabled|present)|not present)', stdout, re.S):
changed = False
else:
module.exit_json(changed = False, result = success_msg)
# openSUSE always returns 0 for a2enmod/a2dismod, need to check
# module output to ensure it was actually loaded/unloaded.
if platform.linux_distribution(full_distribution_name=0)[0] = "SuSE":
if _module_is_enabled(module) != want_enabled:
module.fail_json(msg="Failed to set module %s to %s: %s" % (name, state_string, stdout), rc=result, stdout=stdout, stderr=stderr)
changed = True

if module.check_config:
_module_is_enabled(module)

module.exit_json(changed = changed, result = success_msg)

def main():
module = AnsibleModule(
argument_spec = dict(
name = dict(required=True),
check_config = dict(required=False, type='bool', default=False),
force = dict(required=False, type='bool', default=False),
state = dict(default='present', choices=['absent', 'present'])
),
Expand Down

0 comments on commit 5304dcb

Please sign in to comment.