dbrennand / ansible-role-autorestic

Ansible role to configure backups using autorestic.
https://galaxy.ansible.com/dbrennand/autorestic
MIT License
10 stars 3 forks source link

v2 templating config #14

Closed PleaseStopAsking closed 2 months ago

PleaseStopAsking commented 3 months ago

I am in the process of updating my usage of this role and with the recent switch from string to yaml for the config, I am running into an issue and have been unable to workaround it myself so far.

I am leveraging Jinja2 loop to handle some locations and backends but with the new YAML change, those loops are causing issues.

I am hoping this is something simple I am missing.

Example variable:

autorestic_docker_volumes:
  - influxdb
  - scrutiny-web

Current autorestic config that worked before the yaml change

autorestic_config:
  version: 2

  locations:

  {% for volume in autorestic_docker_volumes %}
    docker-{{ volume }}:
      from: /var/lib/docker/volumes/{{ volume }}
      to: data-docker-{{ volume }}
      cron: "0 {{ loop.index }} * * 6" # every saturday at {{ loop.index }}:00

  {% endfor %}
dbrennand commented 3 months ago

Hi @PleaseStopAsking

Thanks for raising this issue. Similar issues to what you're seeing have been discussed in #12. Are you able to try using the lookup("template") plugin as suggested in https://github.com/dbrennand/ansible-role-autorestic/issues/12#issuecomment-2040325858 and see if that works for you?

PleaseStopAsking commented 3 months ago

@dbrennand I did indeed give that a shot but the generated config file was pretty rough and it wasn't clear how to solve the formatting.

"version: 2\n\nlocations:\n  docker-influxdb:\n    from: /var/lib/docker/volumes/influxdb\n
  \   to: data-docker-influxdb\n    cron: \"0 1 * * 6\" # every saturday at 1:00\n
  \   <<: *shared\n\n  docker-scrutiny-web:\n    from: /var/lib/docker/volumes/scrutiny-web\n
  \   to: data-docker-scrutiny-web\n    cron: \"0 2 * * 6\" # every saturday at 2:00\n
  \   <<: *shared\n\n"

template for reference:

version: 2

locations:
{% for volume in autorestic_docker_volumes %}
  docker-{{ volume }}:
    from: /var/lib/docker/volumes/{{ volume }}
    to: data-docker-{{ volume }}
    cron: "0 {{ loop.index }} * * 6" # every saturday at {{ loop.index }}:00
    <<: *shared

{% endfor %}
dbrennand commented 3 months ago

Hi @PleaseStopAsking - I've pushed the following commit https://github.com/dbrennand/ansible-role-autorestic/commit/2027064eaf4618d09416be440985db4d498a763e

Are you able to test this version of the role on branch issue/#12?

PleaseStopAsking commented 3 months ago

@dbrennand This change does appear to solve the problem.

Below is the usage just for reference.

  roles:
    - role: dbrennand.autorestic
      vars:
        autorestic_version: "{{ autorestic_version_override }}"
        autorestic_restic_version: "{{ autorestic_restic_version_override }}"
        autorestic_info: true
        autorestic_check: true
        autorestic_cron: true
        autorestic_cron_verbose: true
        autorestic_state: present
        autorestic_config_template: ../templates/autorestic.j2
dbrennand commented 2 months ago

@dbrennand This change does appear to solve the problem.

Below is the usage just for reference.

  roles:
    - role: dbrennand.autorestic
      vars:
        autorestic_version: "{{ autorestic_version_override }}"
        autorestic_restic_version: "{{ autorestic_restic_version_override }}"
        autorestic_info: true
        autorestic_check: true
        autorestic_cron: true
        autorestic_cron_verbose: true
        autorestic_state: present
        autorestic_config_template: ../templates/autorestic.j2

That's great news. Thank you for testing @PleaseStopAsking 🙂

I think we can move forward with this implementation because the user can either override the template themselves, as you've done here, or they can continue using autorestic_config and it will be templated in the end anyways.