---
- name: Stop and backup services
  hosts: serguzim_net
  vars:
    host_services: "{{ all_services | services_for_host(inventory_hostname) }}"
  tasks:
    - name: Get unused services
      ansible.builtin.include_tasks:
        file: tasks/get-unused.yml

    - name: Set unused services backups
      ansible.builtin.set_fact:
        unused_services_backups: "{{ all_services | services_get_backups(unused_services) }}"
    - name: Debug unused services backups
      ansible.builtin.debug:
        msg: "{{ unused_services_backups }}"

    - name: Stop unused services
      ansible.builtin.include_tasks:
        file: tasks/stop-unused.yml

    - name: Backup unused services
      ansible.builtin.command:
        cmd: sudo autorestic -c .autorestic.all.yml backup -l {{ item.name }}
        chdir: "{{ (services_path, 'backup') | path_join }}"
      loop: "{{ unused_services_backups }}"
      changed_when: true
      become: true
      register: cmd_result_backup
      until: "cmd_result_backup is not failed"
      retries: 10
      delay: 10