mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
apache2_module - multiple improvements (#3106)
* multiple improvements * added changelog fragment * comment and name in int test files * added notes to the documentation * removed the extraneous changelog frag * Update plugins/modules/web_infrastructure/apache2_module.py * adjusted doc text for sanity check * Update plugins/modules/web_infrastructure/apache2_module.py Co-authored-by: Felix Fontein <felix@fontein.de> * removed extraneous dependency in integration test Co-authored-by: Felix Fontein <felix@fontein.de>
This commit is contained in:
parent
d974ca32ae
commit
d9533c44aa
4 changed files with 71 additions and 64 deletions
2
changelogs/fragments/3106-apache2_module-review.yaml
Normal file
2
changelogs/fragments/3106-apache2_module-review.yaml
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
minor_changes:
|
||||||
|
- apache2_module - minor refactoring improving code quality, readability and speed (https://github.com/ansible-collections/community.general/pull/3106).
|
|
@ -49,6 +49,9 @@ options:
|
||||||
type: bool
|
type: bool
|
||||||
default: False
|
default: False
|
||||||
requirements: ["a2enmod","a2dismod"]
|
requirements: ["a2enmod","a2dismod"]
|
||||||
|
notes:
|
||||||
|
- This does not work on RedHat-based distributions. It does work on Debian- and SuSE-based distributions.
|
||||||
|
Whether it works on others depend on whether the C(a2enmod) and C(a2dismod) tools are available or not.
|
||||||
'''
|
'''
|
||||||
|
|
||||||
EXAMPLES = '''
|
EXAMPLES = '''
|
||||||
|
@ -109,13 +112,14 @@ import re
|
||||||
# import module snippets
|
# import module snippets
|
||||||
from ansible.module_utils.basic import AnsibleModule
|
from ansible.module_utils.basic import AnsibleModule
|
||||||
|
|
||||||
|
_re_threaded = re.compile(r'threaded: *yes')
|
||||||
|
|
||||||
|
|
||||||
def _run_threaded(module):
|
def _run_threaded(module):
|
||||||
control_binary = _get_ctl_binary(module)
|
control_binary = _get_ctl_binary(module)
|
||||||
|
result, stdout, stderr = module.run_command([control_binary, "-V"])
|
||||||
|
|
||||||
result, stdout, stderr = module.run_command("%s -V" % control_binary)
|
return bool(_re_threaded.search(stdout))
|
||||||
|
|
||||||
return bool(re.search(r'threaded:[ ]*yes', stdout))
|
|
||||||
|
|
||||||
|
|
||||||
def _get_ctl_binary(module):
|
def _get_ctl_binary(module):
|
||||||
|
@ -124,15 +128,12 @@ def _get_ctl_binary(module):
|
||||||
if ctl_binary is not None:
|
if ctl_binary is not None:
|
||||||
return ctl_binary
|
return ctl_binary
|
||||||
|
|
||||||
module.fail_json(
|
module.fail_json(msg="Neither of apache2ctl nor apachctl found. At least one apache control binary is necessary.")
|
||||||
msg="Neither of apache2ctl nor apachctl found."
|
|
||||||
" At least one apache control binary is necessary."
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def _module_is_enabled(module):
|
def _module_is_enabled(module):
|
||||||
control_binary = _get_ctl_binary(module)
|
control_binary = _get_ctl_binary(module)
|
||||||
result, stdout, stderr = module.run_command("%s -M" % control_binary)
|
result, stdout, stderr = module.run_command([control_binary, "-M"])
|
||||||
|
|
||||||
if result != 0:
|
if result != 0:
|
||||||
error_msg = "Error executing %s: %s" % (control_binary, stderr)
|
error_msg = "Error executing %s: %s" % (control_binary, stderr)
|
||||||
|
@ -168,7 +169,7 @@ def create_apache_identifier(name):
|
||||||
|
|
||||||
# re expressions to extract subparts of names
|
# re expressions to extract subparts of names
|
||||||
re_workarounds = [
|
re_workarounds = [
|
||||||
('php', r'^(php\d)\.'),
|
('php', re.compile(r'^(php\d)\.')),
|
||||||
]
|
]
|
||||||
|
|
||||||
for a2enmod_spelling, module_name in text_workarounds:
|
for a2enmod_spelling, module_name in text_workarounds:
|
||||||
|
@ -178,7 +179,7 @@ def create_apache_identifier(name):
|
||||||
for search, reexpr in re_workarounds:
|
for search, reexpr in re_workarounds:
|
||||||
if search in name:
|
if search in name:
|
||||||
try:
|
try:
|
||||||
rematch = re.search(reexpr, name)
|
rematch = reexpr.search(name)
|
||||||
return rematch.group(1) + '_module'
|
return rematch.group(1) + '_module'
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
pass
|
pass
|
||||||
|
@ -201,15 +202,15 @@ def _set_state(module, state):
|
||||||
result=success_msg,
|
result=success_msg,
|
||||||
warnings=module.warnings)
|
warnings=module.warnings)
|
||||||
|
|
||||||
a2mod_binary = module.get_bin_path(a2mod_binary)
|
a2mod_binary = [module.get_bin_path(a2mod_binary)]
|
||||||
if a2mod_binary is None:
|
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))
|
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:
|
if not want_enabled and force:
|
||||||
# force exists only for a2dismod on debian
|
# force exists only for a2dismod on debian
|
||||||
a2mod_binary += ' -f'
|
a2mod_binary.append('-f')
|
||||||
|
|
||||||
result, stdout, stderr = module.run_command("%s %s" % (a2mod_binary, name))
|
result, stdout, stderr = module.run_command(a2mod_binary + [name])
|
||||||
|
|
||||||
if _module_is_enabled(module) == want_enabled:
|
if _module_is_enabled(module) == want_enabled:
|
||||||
module.exit_json(changed=True,
|
module.exit_json(changed=True,
|
||||||
|
@ -241,10 +242,10 @@ def main():
|
||||||
module = AnsibleModule(
|
module = AnsibleModule(
|
||||||
argument_spec=dict(
|
argument_spec=dict(
|
||||||
name=dict(required=True),
|
name=dict(required=True),
|
||||||
identifier=dict(required=False, type='str'),
|
identifier=dict(type='str'),
|
||||||
force=dict(required=False, type='bool', default=False),
|
force=dict(type='bool', default=False),
|
||||||
state=dict(default='present', choices=['absent', 'present']),
|
state=dict(default='present', choices=['absent', 'present']),
|
||||||
ignore_configcheck=dict(required=False, type='bool', default=False),
|
ignore_configcheck=dict(type='bool', default=False),
|
||||||
),
|
),
|
||||||
supports_check_mode=True,
|
supports_check_mode=True,
|
||||||
)
|
)
|
||||||
|
@ -253,7 +254,7 @@ def main():
|
||||||
|
|
||||||
name = module.params['name']
|
name = module.params['name']
|
||||||
if name == 'cgi' and _run_threaded(module):
|
if name == 'cgi' and _run_threaded(module):
|
||||||
module.fail_json(msg="Your MPM seems to be threaded. No automatic actions on module %s possible." % name)
|
module.fail_json(msg="Your MPM seems to be threaded. No automatic actions on module cgi possible.")
|
||||||
|
|
||||||
if not module.params['identifier']:
|
if not module.params['identifier']:
|
||||||
module.params['identifier'] = create_apache_identifier(module.params['name'])
|
module.params['identifier'] = create_apache_identifier(module.params['name'])
|
||||||
|
|
|
@ -13,40 +13,25 @@
|
||||||
# You should have received a copy of the GNU General Public License
|
# You should have received a copy of the GNU General Public License
|
||||||
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
- name: install apache via apt
|
|
||||||
apt:
|
|
||||||
name: "{{item}}"
|
|
||||||
state: present
|
|
||||||
when: "ansible_os_family == 'Debian'"
|
|
||||||
with_items:
|
|
||||||
- apache2
|
|
||||||
- libapache2-mod-evasive
|
|
||||||
|
|
||||||
- name: install apache via zypper
|
|
||||||
community.general.zypper:
|
|
||||||
name: apache2
|
|
||||||
state: present
|
|
||||||
when: "ansible_os_family == 'Suse'"
|
|
||||||
|
|
||||||
- name: disable userdir module
|
- name: disable userdir module
|
||||||
apache2_module:
|
community.general.apache2_module:
|
||||||
name: userdir
|
name: userdir
|
||||||
state: absent
|
state: absent
|
||||||
register: userdir_first_disable
|
register: userdir_first_disable
|
||||||
|
|
||||||
- name: disable userdir module, second run
|
- name: disable userdir module, second run
|
||||||
apache2_module:
|
community.general.apache2_module:
|
||||||
name: userdir
|
name: userdir
|
||||||
state: absent
|
state: absent
|
||||||
register: disable
|
register: disable
|
||||||
|
|
||||||
- name: ensure apache2_module is idempotent
|
- name: ensure community.general.apache2_module is idempotent
|
||||||
assert:
|
assert:
|
||||||
that:
|
that:
|
||||||
- disable is not changed
|
- disable is not changed
|
||||||
|
|
||||||
- name: enable userdir module
|
- name: enable userdir module
|
||||||
apache2_module:
|
community.general.apache2_module:
|
||||||
name: userdir
|
name: userdir
|
||||||
state: present
|
state: present
|
||||||
register: enable
|
register: enable
|
||||||
|
@ -57,18 +42,18 @@
|
||||||
- enable is changed
|
- enable is changed
|
||||||
|
|
||||||
- name: enable userdir module, second run
|
- name: enable userdir module, second run
|
||||||
apache2_module:
|
community.general.apache2_module:
|
||||||
name: userdir
|
name: userdir
|
||||||
state: present
|
state: present
|
||||||
register: enabletwo
|
register: enabletwo
|
||||||
|
|
||||||
- name: ensure apache2_module is idempotent
|
- name: ensure community.general.apache2_module is idempotent
|
||||||
assert:
|
assert:
|
||||||
that:
|
that:
|
||||||
- 'not enabletwo.changed'
|
- 'not enabletwo.changed'
|
||||||
|
|
||||||
- name: disable userdir module, final run
|
- name: disable userdir module, final run
|
||||||
apache2_module:
|
community.general.apache2_module:
|
||||||
name: userdir
|
name: userdir
|
||||||
state: absent
|
state: absent
|
||||||
register: disablefinal
|
register: disablefinal
|
||||||
|
@ -79,13 +64,13 @@
|
||||||
- 'disablefinal.changed'
|
- 'disablefinal.changed'
|
||||||
|
|
||||||
- name: set userdir to original state
|
- name: set userdir to original state
|
||||||
apache2_module:
|
community.general.apache2_module:
|
||||||
name: userdir
|
name: userdir
|
||||||
state: present
|
state: present
|
||||||
when: userdir_first_disable is changed
|
when: userdir_first_disable is changed
|
||||||
|
|
||||||
- name: ensure autoindex enabled
|
- name: ensure autoindex enabled
|
||||||
apache2_module:
|
community.general.apache2_module:
|
||||||
name: autoindex
|
name: autoindex
|
||||||
state: present
|
state: present
|
||||||
|
|
||||||
|
@ -93,55 +78,56 @@
|
||||||
when: "ansible_os_family == 'Debian'"
|
when: "ansible_os_family == 'Debian'"
|
||||||
block:
|
block:
|
||||||
- name: force disable of autoindex # bug #2499
|
- name: force disable of autoindex # bug #2499
|
||||||
apache2_module:
|
community.general.apache2_module:
|
||||||
name: autoindex
|
name: autoindex
|
||||||
state: absent
|
state: absent
|
||||||
force: True
|
force: True
|
||||||
|
|
||||||
- name: reenable autoindex
|
- name: reenable autoindex
|
||||||
apache2_module:
|
community.general.apache2_module:
|
||||||
name: autoindex
|
name: autoindex
|
||||||
state: present
|
state: present
|
||||||
|
|
||||||
- name: enable evasive module, test https://github.com/ansible/ansible/issues/22635
|
# mod_evasive is enabled by default upon the installation, so disable first and enable second, to preserve the config
|
||||||
apache2_module:
|
|
||||||
name: evasive
|
|
||||||
state: present
|
|
||||||
|
|
||||||
- name: disable evasive module
|
- name: disable evasive module
|
||||||
apache2_module:
|
community.general.apache2_module:
|
||||||
name: evasive
|
name: evasive
|
||||||
state: absent
|
state: absent
|
||||||
|
|
||||||
|
- name: enable evasive module, test https://github.com/ansible/ansible/issues/22635
|
||||||
|
community.general.apache2_module:
|
||||||
|
name: evasive
|
||||||
|
state: present
|
||||||
|
|
||||||
- name: use identifier to enable module, fix for https://github.com/ansible/ansible/issues/33669
|
- name: use identifier to enable module, fix for https://github.com/ansible/ansible/issues/33669
|
||||||
apache2_module:
|
community.general.apache2_module:
|
||||||
name: dump_io
|
name: dump_io
|
||||||
state: present
|
state: present
|
||||||
ignore_errors: True
|
ignore_errors: True
|
||||||
register: enable_dumpio_wrong
|
register: enable_dumpio_wrong
|
||||||
|
|
||||||
- name: disable dump_io
|
- name: disable dump_io
|
||||||
apache2_module:
|
community.general.apache2_module:
|
||||||
name: dump_io
|
name: dump_io
|
||||||
identifier: dumpio_module
|
identifier: dumpio_module
|
||||||
state: absent
|
state: absent
|
||||||
|
|
||||||
- name: use identifier to enable module, fix for https://github.com/ansible/ansible/issues/33669
|
- name: use identifier to enable module, fix for https://github.com/ansible/ansible/issues/33669
|
||||||
apache2_module:
|
community.general.apache2_module:
|
||||||
name: dump_io
|
name: dump_io
|
||||||
identifier: dumpio_module
|
identifier: dumpio_module
|
||||||
state: present
|
state: present
|
||||||
register: enable_dumpio_correct_1
|
register: enable_dumpio_correct_1
|
||||||
|
|
||||||
- name: ensure idempotency with identifier
|
- name: ensure idempotency with identifier
|
||||||
apache2_module:
|
community.general.apache2_module:
|
||||||
name: dump_io
|
name: dump_io
|
||||||
identifier: dumpio_module
|
identifier: dumpio_module
|
||||||
state: present
|
state: present
|
||||||
register: enable_dumpio_correct_2
|
register: enable_dumpio_correct_2
|
||||||
|
|
||||||
- name: disable dump_io
|
- name: disable dump_io
|
||||||
apache2_module:
|
community.general.apache2_module:
|
||||||
name: dump_io
|
name: dump_io
|
||||||
identifier: dumpio_module
|
identifier: dumpio_module
|
||||||
state: absent
|
state: absent
|
||||||
|
@ -153,7 +139,7 @@
|
||||||
- enable_dumpio_correct_2 is not changed
|
- enable_dumpio_correct_2 is not changed
|
||||||
|
|
||||||
- name: disable mpm modules
|
- name: disable mpm modules
|
||||||
apache2_module:
|
community.general.apache2_module:
|
||||||
name: "{{ item }}"
|
name: "{{ item }}"
|
||||||
state: absent
|
state: absent
|
||||||
ignore_configcheck: True
|
ignore_configcheck: True
|
||||||
|
@ -163,7 +149,7 @@
|
||||||
- mpm_prefork
|
- mpm_prefork
|
||||||
|
|
||||||
- name: enabled mpm_event
|
- name: enabled mpm_event
|
||||||
apache2_module:
|
community.general.apache2_module:
|
||||||
name: mpm_event
|
name: mpm_event
|
||||||
state: present
|
state: present
|
||||||
ignore_configcheck: True
|
ignore_configcheck: True
|
||||||
|
@ -175,7 +161,7 @@
|
||||||
- 'enabledmpmevent.changed'
|
- 'enabledmpmevent.changed'
|
||||||
|
|
||||||
- name: switch between mpm_event and mpm_worker
|
- name: switch between mpm_event and mpm_worker
|
||||||
apache2_module:
|
community.general.apache2_module:
|
||||||
name: "{{ item.name }}"
|
name: "{{ item.name }}"
|
||||||
state: "{{ item.state }}"
|
state: "{{ item.state }}"
|
||||||
ignore_configcheck: True
|
ignore_configcheck: True
|
||||||
|
@ -186,7 +172,7 @@
|
||||||
state: present
|
state: present
|
||||||
|
|
||||||
- name: ensure mpm_worker is already enabled
|
- name: ensure mpm_worker is already enabled
|
||||||
apache2_module:
|
community.general.apache2_module:
|
||||||
name: mpm_worker
|
name: mpm_worker
|
||||||
state: present
|
state: present
|
||||||
register: enabledmpmworker
|
register: enabledmpmworker
|
||||||
|
@ -197,7 +183,7 @@
|
||||||
- 'not enabledmpmworker.changed'
|
- 'not enabledmpmworker.changed'
|
||||||
|
|
||||||
- name: try to disable all mpm modules with configcheck
|
- name: try to disable all mpm modules with configcheck
|
||||||
apache2_module:
|
community.general.apache2_module:
|
||||||
name: "{{item}}"
|
name: "{{item}}"
|
||||||
state: absent
|
state: absent
|
||||||
with_items:
|
with_items:
|
||||||
|
@ -214,7 +200,7 @@
|
||||||
with_items: "{{ remove_with_configcheck.results }}"
|
with_items: "{{ remove_with_configcheck.results }}"
|
||||||
|
|
||||||
- name: try to disable all mpm modules without configcheck
|
- name: try to disable all mpm modules without configcheck
|
||||||
apache2_module:
|
community.general.apache2_module:
|
||||||
name: "{{item}}"
|
name: "{{item}}"
|
||||||
state: absent
|
state: absent
|
||||||
ignore_configcheck: True
|
ignore_configcheck: True
|
||||||
|
@ -224,7 +210,7 @@
|
||||||
- mpm_prefork
|
- mpm_prefork
|
||||||
|
|
||||||
- name: enabled mpm_event to restore previous state
|
- name: enabled mpm_event to restore previous state
|
||||||
apache2_module:
|
community.general.apache2_module:
|
||||||
name: mpm_event
|
name: mpm_event
|
||||||
state: present
|
state: present
|
||||||
ignore_configcheck: True
|
ignore_configcheck: True
|
||||||
|
|
|
@ -5,8 +5,22 @@
|
||||||
####################################################################
|
####################################################################
|
||||||
|
|
||||||
|
|
||||||
|
- name: install apache via apt
|
||||||
|
apt:
|
||||||
|
name: "{{item}}"
|
||||||
|
state: present
|
||||||
|
when: "ansible_os_family == 'Debian'"
|
||||||
|
with_items:
|
||||||
|
- apache2
|
||||||
|
- libapache2-mod-evasive
|
||||||
|
|
||||||
- name:
|
- name: install apache via zypper
|
||||||
|
community.general.zypper:
|
||||||
|
name: apache2
|
||||||
|
state: present
|
||||||
|
when: "ansible_os_family == 'Suse'"
|
||||||
|
|
||||||
|
- name: test apache2_module
|
||||||
block:
|
block:
|
||||||
- name: get list of enabled modules
|
- name: get list of enabled modules
|
||||||
shell: apache2ctl -M | sort
|
shell: apache2ctl -M | sort
|
||||||
|
@ -17,8 +31,12 @@
|
||||||
- name: get list of enabled modules
|
- name: get list of enabled modules
|
||||||
shell: apache2ctl -M | sort
|
shell: apache2ctl -M | sort
|
||||||
register: modules_after
|
register: modules_after
|
||||||
- debug: var=modules_before
|
- name: modules_before
|
||||||
- debug: var=modules_after
|
debug:
|
||||||
|
var: modules_before
|
||||||
|
- name: modules_after
|
||||||
|
debug:
|
||||||
|
var: modules_after
|
||||||
- name: ensure that all test modules are disabled again
|
- name: ensure that all test modules are disabled again
|
||||||
assert:
|
assert:
|
||||||
that: modules_before.stdout == modules_after.stdout
|
that: modules_before.stdout == modules_after.stdout
|
||||||
|
|
Loading…
Reference in a new issue