--- - name: Apache Pre-Check hosts: all become: true gather_facts: true vars: mw_apache_bin: 'httpd' mw_apache_pkg: 'httpd' # mw_apache_pkg: 'php' tasks: - name: Check if Middleware Apache package is present ansible.builtin.package: name: "{{ mw_apache_pkg }}" state: present check_mode: true changed_when: false register: mw_apache_pkg_check - name: Debug mw_apache_pkg_check ansible.builtin.debug: var: mw_apache_pkg_check - name: Verify Middleware Apache package and process presence block: - name: Assert that Middleware Apache package is present ansible.builtin.assert: that: mw_apache_pkg_check['results'] | length == 0 success_msg: Middleware Apache is installed on this host. fail_msg: Middleware Apache is NOT installed on this host. - name: Set Middleware Apache presence flag ansible.builtin.set_stats: data: mw_apache_is_installed: true rescue: - name: Set Middleware Apache presence flag ansible.builtin.set_stats: data: mw_apache_is_installed: false - name: Verify if Middleware Apache process is running block: - name: Check if any Middleware Apache processes are running community.general.pids: name: "{{ mw_apache_bin }}" register: mw_apache_pids - name: Assert that Middleware Apache process is running ansible.builtin.assert: that: mw_apache_pids['pids'] | length >= 2 success_msg: Middleware Apache is running on this host. fail_msg: Middleware Apache is NOT running on this host. - name: Set Middleware Apache process running flag ansible.builtin.set_stats: data: mw_apache_is_running: true rescue: - name: Set Middleware Apache process running flag ansible.builtin.set_stats: data: mw_apache_is_running: false