---

- name: 'apply setup to {{ host | default("all") }}'
  hosts: '{{ host | default("all") }}'
  serial: "{{ serial_number | default(10) }}"
  strategy: free
  vars:
    ansible_ssh_host: "{{ stage_server_domain }}"
  remote_user: root
  become: yes

  pre_tasks:
    - name: "Check if ansible version is at least 2.10.x"
      assert:
        that:
          - ansible_version.major >= 2
          - ansible_version.minor >= 10
        msg: "The ansible version has to be at least ({{ ansible_version.full }})"
      tags:
        - always

    - name: Remove outdated dependencies
      apt:
        name: [
          'docker',
          'docker-client',
          'docker-client-latest',
          'docker-common',
          'docker-latest',
          'docker-latest-logrotate',
          'docker-logrotate',
          'docker-engine',
          'smartmontools',
        ]
        state: 'absent'
      when: ansible_distribution == "Ubuntu"
      tags:
        - common
        - install

    - name: "Import autodiscover pre-tasks"
      import_tasks: tasks/autodiscover_pre_tasks.yml
      become: false
      tags:
        - always

  roles:
    - role: ansible-role-docker
      when:
        - docker_enabled

    - role: common
      tags:
        - common

    - role: node_exporter
      when:
        - node_exporter_enabled
      tags:
        - node-exporter

    - role: filebeat
      when:
        - docker_enabled
        - filebeat_enabled
      tags:
        - filebeat

    - role: metricbeat
      when:
        - docker_enabled
        - metricbeat_enabled
      tags:
        - metricbeat

    - role: traefik
      when:
        - docker_enabled
        - traefik_enabled
      tags:
        - traefik
