---
# file: roles/drupal/tasks/main.yml

- name: Drupal Role
  set_fact:
    role_drupal_started: yes
  tags:
    - always

- block:

    - name: Install Certs
      include_tasks: ../../letsencrypt/tasks/cert.yml
      with_subelements:
        - '{{ drupal_settings|default([]) }}'
        - domains
      loop_control:
        loop_var: domain
      when: domain.1.protocol|default("https") == "https" and domain.1.letsencrypt|default(true) and (limit_site is not defined or limit_site == 'no' or domain.0.id is not defined or limit_site == domain.0.id)
      tags:
        - ApacheConfig

  when: (not excluded_roles or "letsencrypt" not in excluded_roles) and groups.proxyserver is not defined

- block:

    - name: Install Drupal
      include_tasks: install.yml
      with_items: '{{ drupal_settings|default([]) }}'
      loop_control:
        loop_var: drupal
      when: limit_site is not defined or limit_site == 'no' or drupal.id is not defined or limit_site == drupal.id
      tags:
        - ApacheConfig
        - cron
        - deploy
        - SetPermissions
        - mysql

    - name: Install HAProxy redirects collector
      include_tasks: haproxy_redirects_collector.yml
      tags:
        - haproxy_redirects_collector

  when: (not excluded_roles or "drupal" not in excluded_roles) and drupal_install_drupal and (collect_config is not defined or not collect_config)

- block:

    - name: Collect Drupal Configuration
      include_tasks: collect_config/main.yml
      with_items: '{{ drupal_settings|default([]) }}'
      loop_control:
        loop_var: drupal
      when: limit_site is not defined or limit_site == 'no' or drupal.id is not defined or limit_site == drupal.id
      tags:
        - deploy

  when: (not excluded_roles or "drupal" not in excluded_roles) and drupal_install_drupal and collect_config is defined and collect_config

- block:

    - name: Set collect task for db-dump
      set_fact:
        collectTask: 'db'
      tags:
        - collect_db_dump
    - name: Set collect task for asset-archive
      set_fact:
        collectTask: 'assets'
      tags:
        - collect_asset_archive
    - name: Collect and Download
      include_tasks: collect_download/main.yml
      with_items: '{{ drupal_settings|default([]) }}'
      loop_control:
        loop_var: drupal
      when: limit_site is not defined or limit_site == 'no' or drupal.id is not defined or limit_site == drupal.id
      tags:
        - collect_db_dump
        - collect_asset_archive

  when: (not excluded_roles or "drupal" not in excluded_roles) and drupal_install_drupal and collect_download is defined and collect_download