From 790dae3ef4ea7681b806245c2101eb597ea9aa2f Mon Sep 17 00:00:00 2001 From: Matthias Leutenegger Date: Fri, 13 Nov 2020 10:01:05 +0100 Subject: [PATCH] change: backups can be defined as dicts (#28) * convert * use loop * clean * combine * change: reformat dict * fix * fix2 * update readme --- README.md | 11 +++++--- molecule/default/playbook.yml | 25 +++++++++++++++++++ .../test_restic/tasks/test_backup_files.yml | 1 + tasks/distribution/Linux.yml | 8 +++++- 4 files changed, 41 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 5c205ae..0e416e7 100644 --- a/README.md +++ b/README.md @@ -65,12 +65,12 @@ ansible-galaxy install arillso.restic | Name | Default | Description | | ---------------------- | -------------------- | --------------------------------------------------------------------------- | | `restic_url` | `undefined` | The URL to download restic from. Use this variable to overwrite the default | -| `restic_version` | `'0.11.0'` | The version of Restic to install | +| `restic_version` | `'0.11.0'` | The version of Restic to install | | `restic_download_path` | `'/opt/restic'` | Download location for the restic binary | | `restic_install_path` | `'/usr/local/bin'` | Install location for the restic binary | | `restic_script_dir` | `'~/restic'` | Location of the generated backup scripts | | `restic_repos` | `{}` | A dictionary of repositories where snapshots are stored | -| `restic_backups` | `[]` | A list of dictionaries specifying the files and directories to be backed up | +| `restic_backups` | `{}` (or `[]`) | A list of dictionaries specifying the files and directories to be backed up | | `restic_create_cron` | `false` | Should a cronjob be created for each backup | | `restic_dir_owner` | `'{{ansible_user}}'` | The owner of all created dirs | | `restic_dir_group` | `'{{ansible_user}}'` | The group of all created dirs | @@ -146,13 +146,18 @@ Available variables: Example: ```yaml restic_backups: - - name: data + data: + name: data repo: remove src: /path/to/data scheduled: true schedule_hour: 3 ``` +> You can also specify restic_backups as an array, which is a legacy feature and +> might be deprecated in the future. currently, the name key is used for +> namint the access and backup files + #### Exclude the `exclude` key on a backup allows you to specify multiple files to exclude or files to look for filenames to be excluded. You can specify the following keys: diff --git a/molecule/default/playbook.yml b/molecule/default/playbook.yml index 4096d59..a3db77a 100644 --- a/molecule/default/playbook.yml +++ b/molecule/default/playbook.yml @@ -32,3 +32,28 @@ keep_daily: 7 keep_monthly: 12 keep_yearly: 5 + +- name: Converge with dict + hosts: all + roles: + - role: ansible.restic + pre_tasks: + - name: install bzip2 + package: + name: bzip2 + state: present + vars: + restic_download_path: ~/restic + restic_install_path: ~/restic + restic_repos: + local: + location: /backup + password: securepassword1 + init: true + restic_backups: + dicttest: + name: dicttest + src: /home + repo: local + keep_last: 4 + keep_tag: deployment diff --git a/molecule/default/test_restic/tasks/test_backup_files.yml b/molecule/default/test_restic/tasks/test_backup_files.yml index b3061f4..fc22c65 100644 --- a/molecule/default/test_restic/tasks/test_backup_files.yml +++ b/molecule/default/test_restic/tasks/test_backup_files.yml @@ -22,5 +22,6 @@ args: chdir: ~/restic/ with_items: + - backup-dicttest.sh - backup-test.sh - backup-test_stdin.sh diff --git a/tasks/distribution/Linux.yml b/tasks/distribution/Linux.yml index 3356f0f..167e5aa 100644 --- a/tasks/distribution/Linux.yml +++ b/tasks/distribution/Linux.yml @@ -1,6 +1,12 @@ --- # tasks file for skeleton +- 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 @@ -42,8 +48,8 @@ state: present become: true no_log: true + with_items: '{{ restic_backups }}' when: - restic_create_cron - item.name is defined - item.scheduled | default(false) - with_items: '{{ restic_backups }}'