- name: Install required packages
  package: name={{item}} state=present
  with_items:
    - borgbackup
    - heketi-client

- name: Create scripts directory
  file: path="{{ ands_script_path }}" state=directory

- name: Populate backup script
  template: src=backup.sh.j2 dest="{{ ands_script_path }}/ands_backup.sh" owner=root group=root mode=0755

- name: Populate cron job
  template: src=backup.cron.j2 dest="/etc/cron.d/9ands_backup" owner=root group=root mode=0644


- name: Check if backup volume is mounted
  command: mountpoint -q "{{ ands_backup_volume }}"


- block:
  - name: Check if borg is already initialized
    stat: path="{{ ands_borg_path }}/config"
    register: borg_stat_res

  - name: Initialize borg repository
    shell: "borg init {{ ands_borg_path }} --encryption=none"
    when: not borg_stat_res.stat.exists
  run_once: true