---
- setup:

- name: Check if all hosts are reachable
  assert:
    that:
      - ansible_play_hosts == ansible_play_hosts_all

- block:
    - name: Get current kernel version
      shell: "uname -r"
      register: linux_kernel_version
    - set_fact:
        ansible_kernel: "{{ linux_kernel_version.stdout }}"
  when: ansible_kernel is undefined

- name: Include OS specific variables
  include_vars: "{{ ansible_distribution }}.yml"

- name: Include OS specific tasks
  include_tasks: "{{ ansible_distribution }}.yml"

- name: Install python packages
  pip:
    name: "{{ python_packages }}"

- name: Create system users
  include_tasks: user_groups.yml

- name: Create required directories
  include_tasks: dirs.yml

- name: Install docker additional tools
  include_tasks: docker.yml

- name: Install docker prefetch script
  include_tasks: docker_prefetch.yml

- name: Setup monitoring
  include_tasks: monitoring.yml

- name: Setup haproxy config
  include_tasks: haproxy.yml
  when: layerops_instance_type == "orchestrator"

- name: Setup wireguard config templates
  include_tasks: wireguard.yml

- name: Setup job update templates
  include_tasks: updates.yml
  when: layerops_instance_type == "client"

- name: Install consul and consul_template
  include_tasks: consul.yml

- name: Install nomad
  include_tasks: nomad.yml

- name: Install Vault
  include_tasks: vault.yml
  when: layerops_instance_type == "orchestrator"

- name: create logrotate config
  template:
    src: "{{ item }}/logrotate.conf"
    dest: "/etc/logrotate.d/{{ item }}"
  with_items:
    - consul
    - docker
    - haproxy
    - nomad
    - vault

- name: Setup dns config
  include_tasks: systemd_resolved.yml

- name: Create {{ layerops_config_path }} directory
  file:
    path: "{{ layerops_config_path }}"
    state: directory

- name: Create api access config template file
  template:
    src: "init/api-access.json.{{ layerops_instance_type }}"
    dest: "{{ layerops_api_access_file }}.tpl"

- block:
    - name: Create empty layerops config file
      copy:
        content: ""
        dest: "{{ layerops_config_file }}"
        force: no
        group: root
        owner: root
        mode: 0600

    - name: Create common layerops init scripts
      template:
        src: "scripts/{{ item }}"
        dest: "{{ layerops_bin_path }}/{{ item }}"
        mode: 0755
      with_items:
        - launch.sh
        - 01-wireguard.sh
        - 02-consul.sh
        - 03-nomad.sh
        - 04-volumes.sh
        - 05-instance-watch.sh

    - name: Create layerops init scripts specific for {{ layerops_instance_type }}
      template:
        src: "scripts/_common_{{ layerops_instance_type }}.sh"
        dest: "{{ layerops_bin_path }}/_common.sh"
        mode: 0755
  when: layerops_instance_type != "orchestrator"

- name: Create additional init scripts for orchestrator instances
  template:
    src: "orchestrator_scripts/{{ item }}"
    dest: "{{ layerops_bin_path }}/{{ item }}"
    mode: 0755
  with_items:
    - launch.sh
    - ly_wireguard.sh
    - ly_reset_consul.sh
    - ly_consul.sh
    - ly_reset_vault.sh
    - ly_vault.sh
    - ly_reset_nomad.sh
    - ly_nomad.sh
    - ly_nomad_restore_keyring.sh
    - ly_haproxy.sh
  when: layerops_instance_type == "orchestrator"

- name: Create layerops tls management scripts
  template:
    src: "tls/{{ item }}"
    dest: "{{ layerops_bin_path }}/{{ item }}"
    mode: 0755
  with_items:
    - ly_tls_check.sh
    - ly_tls_renew.sh

- name: Create layerops tls management system timers
  template:
    src: "tls/{{ item }}"
    dest: "/etc/systemd/system/{{ item }}"
  with_items:
    - ly-tls-check.service
    - ly-tls-check.timer

- name: Create {{ layerops_config_path }}/image_version.json file
  template:
    src: "init/image_version.json"
    dest: "{{ layerops_config_path }}/image_version.json"

- name: Create {{ layerops_config_path }}/.{{ role_name }} file
  shell: "date +%Y-%m-%dT%TZ > {{ layerops_config_path }}/.{{ role_name }}"
