---
# tasks file for ipabackup

- name: Create backup
  ansible.builtin.shell: >
    ipa-backup
    {{ "--gpg" if ipabackup_gpg | bool else "" }}
    {{ "--gpg-keyring=" + ipabackup_gpg_keyring if ipabackup_gpg_keyring is defined else "" }}
    {{ "--data" if ipabackup_data | bool else "" }}
    {{ "--logs" if ipabackup_logs | bool else "" }}
    {{ "--online" if ipabackup_online | bool else "" }}
    {{ "--disable-role-check" if ipabackup_disable_role_check | bool else "" }}
    {{ "--log-file=" + ipabackup_log_file if ipabackup_log_file is defined else "" }}
  register: result_ipabackup

- name: Handle backup
  when: ipabackup_to_controller
  block:
  - name: Get ipabackup_item from stderr or stdout output
    ansible.builtin.set_fact:
      ipabackup_item: "{{ item | regex_search('\n.*/([^\n]+)','\\1') | first }}"
    when: item.find("Backed up to "+ipabackup_dir+"/") > 0
    with_items:
    - "{{ result_ipabackup.stderr }}"
    - "{{ result_ipabackup.stdout }}"
    loop_control:
      label: ""

  - name: Fail on missing ipabackup_item
    ansible.builtin.fail:
      msg: "Failed to get ipabackup_item"
    when: ipabackup_item is not defined

  - name: Copy backup to controller
    ansible.builtin.include_tasks: "{{ role_path }}/tasks/copy_backup_from_server.yml"
    when: state|default("present") == "present"

  - name: Remove backup on server
    ansible.builtin.include_tasks: "{{ role_path }}/tasks/remove_backup_from_server.yml"
    when: not ipabackup_keep_on_server
