commit ac2d974e4be8af926e182c13302654f0ab853727 Author: chale Date: Tue May 20 16:31:56 2025 -0600 Initial Commit diff --git a/collections/requirements.yml b/collections/requirements.yml new file mode 100644 index 0000000..08e2610 --- /dev/null +++ b/collections/requirements.yml @@ -0,0 +1,3 @@ +--- +collections: +- community.general diff --git a/hosts.yml b/hosts.yml new file mode 100644 index 0000000..85ec053 --- /dev/null +++ b/hosts.yml @@ -0,0 +1,26 @@ +--- +# file: hosts.yml + +sanoid: + hosts: + Server1: + ansible_host: 10.1.25.67 + sanoid_config: + pools: + - name: rpool + template: production + recursive: zfs + - name: rpool/zfs-share + template: production + recursive: zfs + templates: + - name: production + hourly: 24 + daily: 30 + montly: 3 + yearly: 0 + autosnap: yes + autoprune: yes + vars: + ansible_user: root + ansible_ssh_common_args: '-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no' diff --git a/roles/sanoid/handlers/main.yml b/roles/sanoid/handlers/main.yml new file mode 100644 index 0000000..6692550 --- /dev/null +++ b/roles/sanoid/handlers/main.yml @@ -0,0 +1,9 @@ +--- +# file: roles/sanoid/handlers/main.yml + +- name: Reboot Host + reboot: + +- name: Daemon Reload + systemd: + daemon_reload: yes diff --git a/roles/sanoid/tasks/main.yml b/roles/sanoid/tasks/main.yml new file mode 100644 index 0000000..3ca12fc --- /dev/null +++ b/roles/sanoid/tasks/main.yml @@ -0,0 +1,95 @@ +--- +# file: roles/zfs/tasks/main.yml + +- name: Enable Code Ready Builder + community.general.dnf_config_manager: + name: crb + state: enabled + +- name: Install EPEL + package: + name: epel-release + state: present + +- name: Install Dependencies + package: + name: "{{ item }}" + state: present + loop: + - "git" + - "perl-Config-IniFiles" + - "perl-Data-Dumper" + - "perl-Capture-Tiny" + - "perl-Getopt-Long" + - "lzop" + - "mbuffer" + - "mhash" + - "pv" + +- name: Clone Sanoid Repository + git: + repo: "https://github.com/jimsalterjrs/sanoid.git" + dest: "/opt/repos/sanoid" + +- name: Place Executables + copy: + remote_src: true + src: "/opt/repos/sanoid/{{ item }}" + dest: "/usr/local/sbin" + mode: "preserve" + loop: + - "sanoid" + - "syncoid" + - "findoid" + - "sleepymutex" + +- name: Create Sanoid Config Directory + file: + path: /etc/sanoid + state: directory + +- name: Place Default Config + copy: + remote_src: true + src: "/opt/repos/sanoid/sanoid.defaults.conf" + dest: "/etc/sanoid/sanoid.defaults.conf" + +- name: Create Config + template: + src: "sanoid.conf.j2" + dest: "/etc/sanoid/sanoid.conf" + +- name: Create Sanoid Service + template: + src: "sanoid.service.j2" + dest: "/etc/systemd/system/sanoid.service" + become: true + notify: Daemon Reload + +- name: Create Sanoid Pruning Service + template: + src: "sanoid-prune.service.j2" + dest: "/etc/systemd/system/sanoid-prune.service" + become: true + notify: Daemon Reload + +- name: Create Sanoid Timer + template: + src: "sanoid.timer.j2" + dest: "/etc/systemd/system/sanoid.timer" + become: true + notify: Daemon Reload + +- name: Flush handlers + meta: flush_handlers + +- name: Enable Prune Service + service: + name: sanoid-prune.service + enabled: true + +- name: Start and Enable Timer + service: + name: sanoid.timer + state: started + enabled: true diff --git a/roles/sanoid/templates/sanoid-prune.service.j2 b/roles/sanoid/templates/sanoid-prune.service.j2 new file mode 100644 index 0000000..0df55da --- /dev/null +++ b/roles/sanoid/templates/sanoid-prune.service.j2 @@ -0,0 +1,13 @@ +[Unit] +Description=Cleanup ZFS Pool +Requires=zfs.target +After=zfs.target sanoid.service +ConditionFileNotEmpty=/etc/sanoid/sanoid.conf + +[Service] +Environment=TZ=UTC +Type=oneshot +ExecStart=/usr/local/sbin/sanoid --prune-snapshots --verbose + +[Install] +WantedBy=sanoid.service diff --git a/roles/sanoid/templates/sanoid.conf.j2 b/roles/sanoid/templates/sanoid.conf.j2 new file mode 100644 index 0000000..aa954d4 --- /dev/null +++ b/roles/sanoid/templates/sanoid.conf.j2 @@ -0,0 +1,55 @@ +{% for pool in sanoid_config.pools %} +[{{ pool.name }}] +{% if pool.template != '' %} + use_template={{pool.template}} +{% endif %} +{% if pool.recursive|default('') != '' %} + recursive={{pool.recursive}} +{% endif %} +{% if pool.hourly|default('') != '' %} + hourly = {{ pool.hourly }} +{% endif %} +{% if pool.daily|default('') != '' %} + daily = {{ pool.daily }} +{% endif %} +{% if pool.monthly|default('') != '' %} + monthly = {{ pool.montly }} +{% endif %} +{% if pool.yearly|default('') != '' %} + yearly = {{ pool.yearly }} +{% endif %} +{% if pool.autosnap|default('') != '' %} + autosnap = {{ pool.autosnap }} +{% endif %} +{% if pool.autoprune|default('') != '' %} + autoprune = {{ pool.autoprune }} +{% endif %} + +{% endfor %} +########### TEMPLATES ########### + +{% for template in sanoid_config.templates %} +[template_{{ template.name }}] +{% if template.hourly|default('') != '' %} + hourly = {{ template.hourly }} +{% endif %} +{% if template.daily|default('') != '' %} + daily = {{ template.daily }} +{% endif %} +{% if template.monthly|default('') != '' %} + monthly = {{ template.montly }} +{% endif %} +{% if template.yearly|default('') != '' %} + yearly = {{ template.yearly }} +{% endif %} +{% if template.autosnap|default('') != '' %} + autosnap = {{ template.autosnap }} +{% endif %} +{% if template.autoprune|default('') != '' %} + autoprune = {{ template.autoprune }} +{% endif %} +{% if template.recursive|default('') != '' %} + recursive = {{ template.recursive }} +{% endif %} + +{% endfor %} diff --git a/roles/sanoid/templates/sanoid.service.j2 b/roles/sanoid/templates/sanoid.service.j2 new file mode 100644 index 0000000..c68f5c6 --- /dev/null +++ b/roles/sanoid/templates/sanoid.service.j2 @@ -0,0 +1,12 @@ +[Unit] +Description=Snapshot ZFS Pool +Requires=zfs.target +After=zfs.target +Wants=sanoid-prune.service +Before=sanoid-prune.service +ConditionFileNotEmpty=/etc/sanoid/sanoid.conf + +[Service] +Environment=TZ=UTC +Type=oneshot +ExecStart=/usr/local/sbin/sanoid --take-snapshots --verbose diff --git a/roles/sanoid/templates/sanoid.timer.j2 b/roles/sanoid/templates/sanoid.timer.j2 new file mode 100644 index 0000000..c7b4356 --- /dev/null +++ b/roles/sanoid/templates/sanoid.timer.j2 @@ -0,0 +1,10 @@ +[Unit] +Description=Run Sanoid Every 15 Minutes +Requires=sanoid.service + +[Timer] +OnCalendar=*:0/15 +Persistent=true + +[Install] +WantedBy=timers.target diff --git a/sanoid.yml b/sanoid.yml new file mode 100644 index 0000000..4addf5b --- /dev/null +++ b/sanoid.yml @@ -0,0 +1,7 @@ +--- +# file: sanoid.yml + +- hosts: sanoid + become: true + roles: + - sanoid diff --git a/site.yml b/site.yml new file mode 100644 index 0000000..368539e --- /dev/null +++ b/site.yml @@ -0,0 +1,4 @@ +--- +## This playbook deploys the whole application stack in this site. + +- import_playbook: sanoid.yml