Skip to content

Discover and restore

Use case: capture the current values of the settings you manage into a snapshot before a risky change, and restore them afterwards. Restore is scoped: it applies snapshot values only for the application fields you declare, and it fails closed — with the field named — when nothing is declared, when the snapshot is missing a declared field, or when the snapshot holds null for one (the original cannot be proven). See Discovery, snapshot, and restore for the full semantics.

# Capture:
ansible-playbook -i localhost, examples/discover-and-restore.yml --tags discover
# Restore:
ansible-playbook -i localhost, examples/discover-and-restore.yml --tags restore

Source: examples/discover-and-restore.yml

---
# Capture the current values of the settings you manage into a snapshot, then
# restore them later (for example, before and after a risky change).
#
#   # Capture:
#   ansible-playbook -i localhost, examples/discover-and-restore.yml --tags discover
#   # Restore:
#   ansible-playbook -i localhost, examples/discover-and-restore.yml --tags restore
#
# Restore only applies snapshot values for the fields you DECLARE in desired
# state — declare the fields you want restored.
- name: Discover and restore GitLab settings
  hosts: localhost
  gather_facts: false
  connection: local
  vars:
    gitlab_settings_target:
      url: https://gitlab.example.com
      auth:
        type: personal_access_token
        token: "{{ vault_gitlab_admin_token }}"
    snapshot_file: /var/backups/gitlab-settings.yml
  tasks:
    - name: Capture current settings into a snapshot
      ansible.builtin.include_role:
        name: primetheus.gitlab_settings
        tasks_from: discover
        apply:
          tags: [discover]
      vars:
        gitlab_settings_snapshot_file: "{{ snapshot_file }}"
      tags: [discover]

    - name: Restore the captured settings
      ansible.builtin.include_role:
        name: primetheus.gitlab_settings
        tasks_from: restore
        apply:
          tags: [restore]
      vars:
        gitlab_settings_restore_file: "{{ snapshot_file }}"
        # Restore reconciles only the fields named in desired state, taking
        # their values from the snapshot instead of the declared value.
        gitlab_settings_instance:
          application:
            account:
              signup_enabled: true
      tags: [restore]