- name: Ensure that Apache ({{ apache_service }}) is not running -- we may need port swap
  systemd:
    name: "{{ apache_service }}"    # apache2 or httpd, per /opt/iiab/iiab/vars/<OS>.yml
    state: stopped
  ignore_errors: yes
  # 'when: apache_installed is defined' might also work, as mysql's php no longer installs apache2 (but 'ignore_errors: yes' remains safer!)

- name: Install /etc/{{ apache_service }}/ports.conf from template 1 of 2 (ports.conf.j2) if nginx_enabled, to enable Apache port {{ apache_port }} localhost only
  template:
    src: ports.conf.j2
    dest: "/etc/{{ apache_service }}/ports.conf"
  when: nginx_enabled

- name: Install /etc/{{ apache_service }}/ports.conf from template 2 of 2 (stock-apache-ports.conf) if not nginx_enabled, to disable Apache port {{ apache_port }} localhost only
  template:
    src: stock-apache-ports.conf
    dest: "/etc/{{ apache_service }}/ports.conf"
  when: not nginx_enabled

- name: Enable & Restart '{{ apache_service }}' if Apache is installed and enabled, since we stopped it
  systemd:
    name: "{{ apache_service }}"    # apache2 or httpd, per /opt/iiab/iiab/vars/<OS>.yml
    daemon_reload: yes
    state: restarted
    enabled: true
  when: apache_installed is defined and apache_enabled    # or not nginx_enabled


- name: Enable & (Re)Start 'nginx' systemd service, if nginx_enabled
  systemd:
    name: nginx
    daemon_reload: yes
    enabled: yes
    state: restarted
  when: nginx_enabled

- name: Disable & Stop 'nginx' systemd service, if not nginx_enabled
  systemd:
    name: nginx
    enabled: no
    state: stopped
  when: not nginx_enabled
