Skip to content
Snippets Groups Projects
main.yml 6.12 KiB
Newer Older
Chris Croome's avatar
Chris Croome committed
# Copyright 2018-2023 Chris Croome
#
# This file is part of the Webarchitects Apache Ansible role.
#
# The Webarchitects Apache Ansible role is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
#
# The Webarchitects Apache Ansible role is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with the Webarchitects Apache Ansible role. If not, see <https://www.gnu.org/licenses/>.
Chris Croome's avatar
Chris Croome committed
---
- name: Apache role skipped
  ansible.builtin.debug:
Chris Croome's avatar
Chris Croome committed
    msg: "The tasks in the Apache role are not being run since the apache variable is not true."
  when: not apache | bool
  tags:
    - apache

Chris Croome's avatar
Chris Croome committed
- name: Install and configure Apache
Chris Croome's avatar
Chris Croome committed
    - name: Verify variables that start with apache_
      ansible.builtin.include_tasks: verify.yml
      when:
        - apache_verify is defined
        - apache_verify | bool

    - name: Include apt role local fact tasks
      ansible.builtin.include_role:
        name: apt
        tasks_from: local_facts.yml
      when: >-
        ( ansible_local.dpkg.arch is not defined ) or
        ( ansible_local.gpg.version is not defined ) or
        ( ansible_local.bash.path is not defined )

    - name: Packages present and absent
      block:

        - name: Apache packages absent
          ansible.builtin.apt:
Chris Croome's avatar
Chris Croome committed
            pkg: "{{ apache_pkgs_absent }}"
            state: absent
Chris Croome's avatar
Chris Croome committed
            update_cache: true
Chris Croome's avatar
Chris Croome committed
            cache_valid_time: 60
Chris Croome's avatar
Chris Croome committed
            - apache_pkgs_absent is defined
            - apache_pkgs_absent != []
            - ansible_local.dpkg.installed is defined
Chris Croome's avatar
Chris Croome committed
            - apache_pkgs_absent | ansible.builtin.intersect(ansible_local.dpkg.installed) != []
          notify: Restart Apache

        - name: Apache present from buster-backports
          ansible.builtin.apt:
            pkg:
              - apache2
            state: latest
            install_recommends: true
            default_release: buster-backports
            update_cache: true
Chris Croome's avatar
Chris Croome committed
            cache_valid_time: 60
Chris Croome's avatar
Chris Croome committed
            - apache_pkgs_present is defined
            - ( "apache2" in apache_pkgs_present )
            - ansible_distribution_release == "buster"
          notify: Restart Apache

        - name: Apache packages present
          ansible.builtin.apt:
Chris Croome's avatar
Chris Croome committed
            pkg: "{{ apache_pkgs_present }}"
            state: present
            install_recommends: true
Chris Croome's avatar
Chris Croome committed
            update_cache: true
            cache_valid_time: 60
Chris Croome's avatar
Chris Croome committed
            - apache_pkgs_present is defined
            - apache_pkgs_present != []
            - ansible_local.dpkg.installed is defined
Chris Croome's avatar
Chris Croome committed
            - apache_pkgs_present | ansible.builtin.difference(ansible_local.dpkg.installed) != []
          notify: Restart Apache
Chris Croome's avatar
Chris Croome committed
        ( ( apache_pkgs_absent is defined ) and ( apache_pkgs_absent != [] ) ) or
        ( ( apache_pkgs_present is defined ) and ( apache_pkgs_present != [] ) )
Chris Croome's avatar
Chris Croome committed

    - name: Include local facts tasks
Chris Croome's avatar
Chris Croome committed
      ansible.builtin.include_tasks: local_facts.yml
      when: ansible_local.a2query is not defined
Chris Croome's avatar
Chris Croome committed
    - name: Include Apache checks
Chris Croome's avatar
Chris Croome committed
      ansible.builtin.include_tasks: checks.yml
Chris Croome's avatar
Chris Croome committed

    - name: Apache DocumentRoot present
Chris Croome's avatar
Chris Croome committed
      ansible.builtin.file:
        path: "{{ apache_document_root }}"
        state: directory
        mode: "0755"
        owner: root
        group: root

    - name: Include MPM switching tasks
      ansible.builtin.include_tasks: mpm.yml
      when: ansible_local.a2query.mpm not in apache_mods_enabled

    - name: Apache DH parameters file present
Chris Croome's avatar
Chris Croome committed
      community.crypto.openssl_dhparam:
        path: "{{ apache_dhparam_path }}"
        size: "{{ apache_dhparam_size }}"
      notify: Restart Apache
Chris Croome's avatar
Chris Croome committed
    - name: Include suEXEC tasks
Chris Croome's avatar
Chris Croome committed
      ansible.builtin.include_tasks: suexec.yml
      when:
        - apache_suexec is defined
        - apache_suexec | bool
Chris Croome's avatar
Chris Croome committed

Chris Croome's avatar
Chris Croome committed
    - name: Install specific version of mod_md
Chris Croome's avatar
Chris Croome committed
      ansible.builtin.include_tasks: md.yml
      when:
        - ( "md" in apache_mods_enabled )
        - apache_md_version is defined
        - apache_md_version != "default"
Chris Croome's avatar
Chris Croome committed
    - name: Apache config available
Chris Croome's avatar
Chris Croome committed
      ansible.builtin.include_tasks: config.yml
Chris Croome's avatar
Chris Croome committed

Chris Croome's avatar
Chris Croome committed
    - name: Apache envars in place
Chris Croome's avatar
Chris Croome committed
      ansible.builtin.template:
Chris Croome's avatar
Chris Croome committed
        src: templates/envvars.j2
        dest: /etc/apache2/envvars
        mode: "0755"
Chris Croome's avatar
Chris Croome committed
        owner: root
        group: root
      notify: Restart Apache
Chris Croome's avatar
Chris Croome committed

Chris Croome's avatar
Chris Croome committed
    - name: Include Apache check config tasks
      ansible.builtin.include_tasks: check_config.yml

Chris Croome's avatar
Chris Croome committed
    - name: Apache modules disabled and enabled
Chris Croome's avatar
Chris Croome committed
      ansible.builtin.include_tasks: a2mod.yml
Chris Croome's avatar
Chris Croome committed
      when: ( apache_mods_disabled is defined ) or ( apache_mods_enabled is defined )

    - name: Apache conf disabled and enabled
Chris Croome's avatar
Chris Croome committed
      ansible.builtin.include_tasks: a2conf.yml
Chris Croome's avatar
Chris Croome committed
      when: ( apache_sites_disabled is defined ) or ( apache_conf_enabled is defined )

    - name: Apache sites disabled and enabled
Chris Croome's avatar
Chris Croome committed
      ansible.builtin.include_tasks: a2site.yml
Chris Croome's avatar
Chris Croome committed
      when: ( apache_sites_disabled is defined ) or ( apache_sites_enabled is defined )

    - name: Apache enabled for systemd servers
Chris Croome's avatar
Chris Croome committed
      ansible.builtin.systemd:
Chris Croome's avatar
Chris Croome committed
        name: apache2
        enabled: true
Chris Croome's avatar
Chris Croome committed
      when: ansible_distribution_release != "stretch"
      notify: Restart Apache
Chris Croome's avatar
Chris Croome committed

    - name: Apache configtest
Chris Croome's avatar
Chris Croome committed
      ansible.builtin.command: apache2ctl configtest
      check_mode: false
      changed_when: false
Chris Croome's avatar
Chris Croome committed
      register: apache_configtest
Chris Croome's avatar
Chris Croome committed
      failed_when: apache_configtest.rc is not regex('^0|1$')
Chris Croome's avatar
Chris Croome committed

    - name: Print the apache2ctl configtest standard error
Chris Croome's avatar
Chris Croome committed
      ansible.builtin.debug:
Chris Croome's avatar
Chris Croome committed
        msg: "{{ apache_configtest.stderr }}"
        verbosity: 1

    - name: Fail if Apache configtest is not OK
Chris Croome's avatar
Chris Croome committed
      ansible.builtin.fail:
Chris Croome's avatar
Chris Croome committed
        msg: "{{ apache_configtest.stdout_lines }}"
      when: ( "Syntax OK" not in apache_configtest.stderr ) or ( apache_configtest.rc == 1 )
Chris Croome's avatar
Chris Croome committed

  when: apache | bool