summaryrefslogtreecommitdiff
path: root/roles/storage/zfs/syncoid/tasks/main.yml
blob: 939dc11005f2d3d2c43775997364c3ce29f094e2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
---
- name: install sanoid package
  apt:
    name: sanoid
    state: present

- name: create syncoid directory
  file:
    path: /var/lib/syncoid
    state: directory
    mode: 0700

- name: genarate ssh keypair for syncoid
  openssh_keypair:
    path: /var/lib/syncoid/id_ssh_ed25519
    type: ed25519
    comment: ZFS Backup syncoid@{{ host_name }}

- name: generate syncoid pull script
  template:
    src: syncoid_pull.j2
    dest: /var/lib/syncoid/syncoid_pull
    mode: 0755

- name: check if lvm.conf does exist
  stat:
    path: /etc/lvm/lvm.conf
  register: zfs_syncoid_lvm_conf

- name: configure lvm to ignore zfs volumes
  when: zfs_syncoid_lvm_conf.stat.exists
  lineinfile:
    path: /etc/lvm/lvm.conf
    backrefs: yes
    regexp: '^\s*#?\s*global_filter\s*='
    line: '        global_filter = [ "r|/dev/zd[0-9]+|" ]'

- name: create target datasets
  loop: "{{ zfs_syncoid_sources | dict2items }}"
  loop_control:
    loop_var: source
    label: "{{ source.key }}"
  include_tasks: datasets.yml

- name: create systemd units
  loop: "{{ zfs_syncoid_sources | dict2items }}"
  loop_control:
    label: "{{ item.key }}"
  template:
    src: pull.service.j2
    dest: "/etc/systemd/system/syncoid-pull-{{ item.key }}.service"

- name: create systemd timer units for periodic backups
  loop: "{{ zfs_syncoid_sources | dict2items }}"
  loop_control:
    label: "{{ item.key }}"
  when: "'periodic' in item.value"
  template:
    src: pull.timer.j2
    dest: "/etc/systemd/system/syncoid-pull-{{ item.key }}.timer"

- name: make sure systemd timer units for periodic backups are enabled and started
  loop: "{{ zfs_syncoid_sources | dict2items }}"
  loop_control:
    label: "{{ item.key }}"
  when: "'periodic' in item.value"
  systemd:
    daemon_reload: yes
    name: "syncoid-pull-{{ item.key }}.timer"
    state: started
    enabled: yes

- name: enable autosuspend
  when: zfs_syncoid_autosuspend is defined
  block:
  - name: install hdparm package
    when: zfs_syncoid_autosuspend == 'disks'
    apt:
      name: hdparm
      state: present

  - name: create systemd override directory for sanoid timer
    when: zfs_syncoid_autosuspend == 'system'
    file:
      path: /etc/systemd/system/sanoid.timer.d
      state: directory

  - name: disable persistence of sanoid timer
    when: zfs_syncoid_autosuspend == 'system'
    copy:
      content: |
        [Timer]
        Persistent=false
      dest: /etc/systemd/system/sanoid.timer.d/no-persistence.conf

  - name: install python deps
    apt:
      name: "{{ python_basename }}-dbus"
      state: present

  - name: install autosuspend script
    template:
      src: autosuspend.py.j2
      dest: /usr/local/bin/syncoid-autosuspend.py
      mode: 0755

  - name: install autosuspend systemd units
    loop:
    - service
    - timer
    template:
      src: "autosuspend.{{ item }}.j2"
      dest: "/etc/systemd/system/syncoid-autosuspend.{{ item }}"

  - name: make sure autosuspend timer unit is enabled and started
    systemd:
      daemon_reload: yes
      name: syncoid-autosuspend.timer
      enabled: yes
      state: started

- name: generate syncoid finalize script
  template:
    src: syncoid_finalize.j2
    dest: /var/lib/syncoid/syncoid_finalize
    mode: 0755