From d7ced5de41e131889e1c052239910c999e33bdd0 Mon Sep 17 00:00:00 2001 From: AnsibleGuy Date: Fri, 10 Jun 2022 23:41:25 +0200 Subject: [PATCH] increased speed of module status-changes --- filter_plugins/utils.py | 21 +++++++++++++++++++++ tasks/debian/main.yml | 19 +++++++++---------- 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/filter_plugins/utils.py b/filter_plugins/utils.py index c957ca6..4bcba2f 100644 --- a/filter_plugins/utils.py +++ b/filter_plugins/utils.py @@ -8,6 +8,9 @@ class FilterModule(object): "safe_key": self.safe_key, "all_true": self.all_true, "prepare_letsencrypt": self.prepare_letsencrypt, + "ensure_list": self.ensure_list, + "enmod_list": self.enmod_list, + "mod_list": self.mod_list, } @staticmethod @@ -30,3 +33,21 @@ class FilterModule(object): 'state': site['state'], } } + + @classmethod + def enmod_list(cls, present: list, absent: list) -> str: + absent_list = cls.ensure_list(absent) + return ' '.join([mod for mod in cls.ensure_list(present) if mod not in absent_list]) + + @classmethod + def mod_list(cls, mods: (str, list)) -> str: + return ' '.join(cls.ensure_list(mods)) + + @staticmethod + def ensure_list(data: (str, list)) -> list: + # if user supplied a string instead of a list => convert it to match our expectations + if type(data) == list: + return data + + else: + return [data] diff --git a/tasks/debian/main.yml b/tasks/debian/main.yml index f1ddf08..058e883 100644 --- a/tasks/debian/main.yml +++ b/tasks/debian/main.yml @@ -26,22 +26,19 @@ - {reg: '^export APACHE_RUN_GROUP=', line: "export APACHE_RUN_GROUP={{ APACHE_CONFIG.group }}"} tags: [base, config] +# NOTE: a2enmod/a2dismod command saves time in comparison to the community 'apache2_module' module - name: Apache | Debian | Enabling apache modules - community.general.apache2_module: - state: present - name: "{{ item }}" - when: item not in APACHE_CONFIG.modules.absent + ansible.builtin.command: "a2enmod {{ APACHE_CONFIG.modules.present | enmod_list(APACHE_CONFIG.modules.absent) }}" register: apache_mods_enable_raw - loop: "{{ APACHE_CONFIG.modules.present }}" + changed_when: "'restart apache2' in apache_mods_enable_raw.stdout" + when: APACHE_CONFIG.modules.present | ensure_list | length > 0 tags: [base] - name: Apache | Debian | Disabling apache modules - community.general.apache2_module: - state: absent - name: "{{ APACHE_CONFIG.modules.absent }}" - force: true - ignore_configcheck: true + ansible.builtin.command: "a2dismod -f {{ APACHE_CONFIG.modules.absent | mod_list }}" register: apache_mods_disable_raw + changed_when: "'restart apache2' in apache_mods_disable_raw.stdout" + when: APACHE_CONFIG.modules.absent | ensure_list | length > 0 tags: [base] # todo: configure module settings @@ -128,6 +125,7 @@ ansible.builtin.systemd: name: 'apache2.service' state: reloaded + changed_when: false tags: [base, config, sites, certs] - name: Apache | Debian | Adding status page @@ -160,4 +158,5 @@ name: 'apache2.service' enabled: yes state: reloaded + changed_when: false tags: [base, config, sites, certs]