---
- name: Create mandatory directories
  file:
    path: "{{ item }}"
    state: directory
  with_items:
    - "{{ layerops_config_path }}"
    - "{{ layerops_data_path }}"
    - "{{ layerops_template_path }}"
    - "{{ layerops_bin_path }}"
    - "{{ local_bin_path }}"
    - "{{ consul_config_template_path }}"
    - "{{ nomad_config_template_path }}"
    - "{{ updates_template_path }}"
    - "{{ wireguard_config_path }}"
    - "{{ wireguard_template_path }}"
    - "{{ consul_log_path }}"
    - "{{ docker_log_path }}"
    - "{{ nomad_log_path }}"
    - "{{ vault_log_path }}"
    - "{{ haproxy_log_path }}"

- name: Create layerops certificate directory
  file:
    path: "{{ layerops_tls_path }}"
    state: directory
    owner: root
    group: "{{ hashistack_group }}"
    mode: 0750

- name: Create Consul Configuration and data directories
  file:
    dest: "{{ item }}"
    state: directory
    owner: "{{ consul_user }}"
    group: "{{ hashistack_group }}"
    mode: 0700
  with_items:
    - "{{ consul_config_path }}"
    - "{{ consul_data_path }}"

- name: Create Consul Run directory
  file:
    dest: "{{ consul_run_path }}"
    state: directory
    owner: "{{ consul_user }}"
    group: "{{ hashistack_group }}"
    mode: 0750

- name: Create Nomad Configuration and data directories
  file:
    dest: "{{ item }}"
    state: directory
    owner: "{{ nomad_user }}"
    group: "{{ hashistack_group }}"
    mode: 0700
  with_items:
    - "{{ nomad_config_path }}"
    - "{{ nomad_data_path }}"

- name: Create Nomad Run directory
  file:
    dest: "{{ nomad_run_path }}"
    state: directory
    owner: "{{ nomad_user }}"
    group: "{{ hashistack_group }}"
    mode: 0750

- name: Create Layerops ".ssh" directory
  file:
    dest: "{{ layerops_home_dir }}/.ssh"
    state: directory
    owner: "{{ layerops_user }}"
    group: "{{ layerops_group }}"
    mode: 0700

- name: Create Layerops "authorised_keys" file
  file:
    dest: "{{ layerops_authorized_keys_file }}"
    state: touch
    owner: "{{ layerops_user }}"
    group: "{{ layerops_group }}"
    mode: 0600

- name: Create monitoring directories
  file:
    dest: "{{ item }}"
    state: directory
    owner: "{{ monitoring_user }}"
    group: "{{ monitoring_group }}"
  with_items:
    - "{{ monitoring_data_path }}"
    - "{{ monitoring_data_path }}/alertmanager"
    - "{{ monitoring_data_path }}/grafana/dashboards/json"
    - "{{ monitoring_data_path }}/grafana/data"
    - "{{ monitoring_data_path }}/loki"
    - "{{ monitoring_data_path }}/mimir"
    - "{{ monitoring_data_path }}/mimir/rules"
    - "{{ monitoring_data_path }}/prometheus"
  when: layerops_instance_type == "orchestrator"
