- name: install duplicity apt: name: duplicity state: present become: yes - name: generate commands set_fact: backup_command: |- {% set source = item.0 %} {% set target = item.1 %} {% if source.source_host is defined %} mount_path="$(mktemp --tmpdir --directory ansible-duplicity-sshfs-XXXXXXXX)"; sshfs "{{source.source_host}}:{{source.source_path}}" $mount_path; {%- endif %} duplicity --no-print-statistics --verbosity warning {%- if target.encrypt_key is defined %} --encrypt-key "{{target.encrypt_key}}" {%- else %} --no-encryption {%- endif %} {%- for selector in source.selectors|default([]) %} {% if selector['option'] in ['exclude', 'include'] %} --{{selector['option']}} {%- if source.source_host is defined %} "{{ selector['shell_pattern'] | replace(source.source_path, '$mount_path') }}" {%- else %} "{{selector['shell_pattern']}}" {%- endif %} {%- endif %} {% endfor %} {%- if source.source_host is defined %} --allow-source-mismatch "$mount_path" {%- else %} "{{source.source_path}}" {%- endif %} "{{target.url}}"; {%- if source.source_host is defined %} fusermount -u "$mount_path" && rm -r "$mount_path"; {%- endif %} with_subelements: - '{{duplicity_backups}}' - targets register: duplicity_backup_commands - name: setup cron jobs blockinfile: dest: '/etc/crontab' marker: '# ANSIBLE DUPLICITY BACKUPS {mark}' block: |- {% for backup in duplicity_backup_commands.results %} {% set source = backup.item.0 %} {% set target = backup.item.1 %} {% for backup_job in target.backup_cron_jobs %} {{backup_job.minute}} {{backup_job.hour}} * * * {{source.local_user}} {{backup.ansible_facts.backup_command}} {% endfor %} {% endfor %} become: yes