---
- name: reformat dict if necessary
  set_fact:
    restic_backups: "{{ restic_backups|dict2items|json_query('[*].value') }}"
  when:
    - restic_backups | type_debug == "dict"

- name: Create backup credentials
  template:
    src: restic_access_Linux.j2
    dest: "{{ restic_script_dir }}/access-{{ item.name | replace(' ', '') }}.sh"
    mode: '0700'
    owner: '{{ restic_dir_owner }}'
    group: '{{ restic_dir_group }}'
  no_log: true
  with_items: '{{ restic_backups }}'
  when:
    - item.name is defined
    - item.src is defined or item.stdin is defined
    - item.src is defined or item.stdin and item.stdin_cmd is defined
    - item.repo in restic_repos

- name: Create backup script
  template:
    src: restic_script_Linux.j2
    dest: "{{ restic_script_dir }}/backup-{{ item.name | replace(' ', '') }}.sh"
    mode: '0700'
    owner: '{{ restic_dir_owner }}'
    group: '{{ restic_dir_group }}'
  no_log: true
  with_items: '{{ restic_backups }}'
  when:
    - item.name is defined
    - item.src is defined or item.stdin is defined
    - item.src is defined or item.stdin and item.stdin_cmd is defined
    - item.repo in restic_repos