---
- hosts: all
  become: yes

  vars_files:
    - vars/main.yml
    - ../default.config.yml

  pre_tasks:
    - include: tasks/config.yml
      tags: ['always']

    - include: tasks/backwards-compatibility.yml
      tags: ['always']
      static: no

    - include: "tasks/init-{{ ansible_os_family }}.yml"
      static: no

    - name: Run configured pre-provision shell scripts.
      script: "{{ item }}"
      with_items: "{{ pre_provision_scripts|default([]) }}"

    - name: Run configured pre-provision ansible task files.
      include: "{{ outer_item }}"
      loop_control:
        loop_var: outer_item
      with_fileglob: "{{ pre_provision_tasks_dir|default(omit) }}"

    - include: tasks/php.yml

  roles:
    # Essential roles.
    - { role: geerlingguy.repo-remi, when: ansible_os_family == 'RedHat', tags: ['webserver', 'php'] }
    - { role: drupalvm.hostname, when: hostname_configure }
    - { role: geerlingguy.firewall, when: firewall_enabled }
    - { role: geerlingguy.git }
    - { role: geerlingguy.postfix }
    - { role: geerlingguy.apache, when: drupalvm_webserver == 'apache', tags: ['webserver']}
    - { role: geerlingguy.apache-php-fpm, when: drupalvm_webserver == 'apache', tags: ['webserver'] }
    - { role: geerlingguy.nginx, when: drupalvm_webserver == 'nginx', tags: ['webserver'] }
    - { role: geerlingguy.php-versions, when: php_version != '', tags: ['php', 'xdebug', 'database'] }
    - { role: geerlingguy.php, tags: ['php'] }
    - { role: geerlingguy.php-pecl, tags: ['php'] }
    - { role: geerlingguy.composer }
    - { role: geerlingguy.mysql, when: drupal_db_backend == 'mysql', tags: ['database'] }
    - { role: geerlingguy.php-mysql, when: drupal_db_backend == 'mysql', tags: ['php', 'database'] }
    - { role: geerlingguy.postgresql, when: drupal_db_backend == 'pgsql', tags: ['database'] }
    - { role: geerlingguy.php-pgsql, when: drupal_db_backend == 'pgsql', tags: ['php', 'database'] }

    # Conditionally-installed roles.
    - { role: geerlingguy.drupal-console, when: 'drupal_major_version > 7 and "drupalconsole" in installed_extras' }
    - { role: geerlingguy.drush, when: '"drush" in installed_extras' }
    - { role: geerlingguy.memcached, when: '"memcached" in installed_extras' }
    - { role: geerlingguy.php-memcached, when: '"memcached" in installed_extras', tags: ['php'] }

    - role: geerlingguy.php-tideways
      workspace: "/root/php{{ php_version }}"
      when: '"tideways" in installed_extras'
      tags: ['php']

    - role: geerlingguy.php-xdebug
      workspace: "/root/php{{ php_version }}"
      when: '"xdebug" in installed_extras'
      tags: ['php', 'xdebug']

    - role: geerlingguy.php-xhprof
      workspace: "/root/php{{ php_version }}"
      when: '"xhprof" in installed_extras'
      tags: ['php']

    - role: thom8.php-upload-progress
      workspace: "/root/php{{ php_version }}"
      when: '"upload-progress" in installed_extras'
      tags: ['php']

    - { role: geerlingguy.blackfire, when: '"blackfire" in installed_extras' }
    - { role: geerlingguy.adminer, when: '"adminer" in installed_extras', tags: ['database'] }
    - { role: geerlingguy.pimpmylog, when: '"pimpmylog" in installed_extras' }
    - { role: geerlingguy.daemonize, when: '"mailhog" in installed_extras' }
    - { role: geerlingguy.mailhog, when: '"mailhog" in installed_extras' }
    - { role: franklinkim.newrelic, when: '"newrelic" in installed_extras' }
    - { role: geerlingguy.nodejs, when: '"nodejs" in installed_extras' }
    - { role: geerlingguy.redis, when: '"redis" in installed_extras' }
    - { role: geerlingguy.php-redis, when: '"redis" in installed_extras', tags: ['php'] }
    - { role: geerlingguy.ruby, when: '"ruby" in installed_extras' }

    - role: geerlingguy.java
      when: >
        "java" in installed_extras or
        "solr" in installed_extras or
        "selenium" in installed_extras or
        "elasticsearch" in installed_extras

    - { role: arknoll.selenium, when: '"selenium" in installed_extras' }
    - { role: geerlingguy.solr, when: '"solr" in installed_extras' }
    - { role: geerlingguy.elasticsearch, when: '"elasticsearch" in installed_extras' }
    - { role: geerlingguy.varnish, when: '"varnish" in installed_extras' }
    - { role: drupalvm.www, tags: ['webserver'] }
    - { role: geerlingguy.drupal, tags: ['drupal'] }

    # Roles for security and stability on production.
    - { role: geerlingguy.security, when: extra_security_enabled }

  tasks:
    - include: tasks/sshd.yml
    - include: tasks/extras.yml

    - include: tasks/apparmor.yml
      when: ansible_os_family == 'Debian' and drupal_db_backend == 'mysql'
      tags: ['database']

    - include: tasks/drush-aliases.yml

    - include: tasks/cron.yml
      tags: ['cron']

    - include: tasks/dashboard.yml
      when: dashboard_install_dir is defined and dashboard_install_dir != ''
      tags: ['webserver', 'database', 'php']

    - name: Run configured post-provision shell scripts.
      script: "{{ item }}"
      with_items: "{{ post_provision_scripts|default([]) }}"

    - name: Run configured post-provision ansible task files.
      include: "{{ outer_item }}"
      loop_control:
        loop_var: outer_item
      with_fileglob: "{{ post_provision_tasks_dir|default(omit) }}"
