-
Notifications
You must be signed in to change notification settings - Fork 0
/
system_update_checker.yml
168 lines (155 loc) · 6.4 KB
/
system_update_checker.yml
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# ex: set tabstop=8 softtabstop=0 expandtab shiftwidth=2 smarttab:
---
- hosts: localhost
connection: local
gather_facts: true
vars_files:
- "vars/{{ overwrite_distribution | default(ansible_distribution) }}.yml"
- "vars/{{ overwrite_distribution | default(ansible_distribution) }}-{{ overwrite_machine | default(ansible_machine) }}.yml"
- "vars/custom_patchmgmt.yml"
vars:
root_zfs_pool: "{{ ansible_proc_cmdline.rpool }}"
distro_filesystem_path: "{{ root_zfs_pool }}/ROOT/{{ ansible_distribution }}"
distro_snapshot_path: "{{ root_zfs_pool }}/ROOT/{{ ansible_distribution }}@before_upgrade"
distro_old_boot_path: "{{ distro_filesystem_path }}_old_boot"
distro_filesystem_path_without_zpool: "/ROOT/{{ ansible_distribution }}"
number_of_snapshots_to_keep: 3
roles:
- name: Config Grub
role: grub_mkconfig
tags:
- config_grub
post_tasks:
- name: Import informations about ZFS
zfs_facts:
name: "{{ root_zfs_pool }}"
recurse: yes
- block:
- name: "Destroy the old filesystem '{{ distro_old_boot_path }}'"
zfs:
name: "{{ distro_old_boot_path }}"
state: absent
- name: Get the creation time of the snapshot
shell: >-
date --utc "--date=@$(zfs get creation -Hpo value {{ distro_snapshot_path }})"
changed_when: false
register: _snapshot_creation_datetime
- name: "Rename to snapshot '{{ distro_snapshot_path }}'"
ansible.builtin.command:
cmd: "zfs rename '{{ distro_snapshot_path }}' '{{ distro_filesystem_path }}@{{ datetime }}'"
vars:
datetime: "{{ _snapshot_creation_datetime.stdout | regex_replace(' ', '_') }}"
when:
- ansible_zfs_datasets | json_query(filter_1) | count > 0
- ansible_zfs_datasets | json_query(filter_2) | count > 0
- ansible_zfs_datasets | json_query(filter_2) | first == distro_snapshot_path
vars:
filter_1: "[?name=='{{ distro_snapshot_path }}']"
filter_2: "[?name=='{{ distro_old_boot_path }}'].origin"
- name: Updated informations about ZFS
zfs_facts:
name: "{{ distro_filesystem_path }}"
recurse: yes
- name: Remove old snapshots
zfs:
name: "{{ item }}"
state: absent
# the workaround is to added 'to_json | from_json' and when it work (it is magic)
loop: "{{ (ansible_zfs_datasets | to_json | from_json | json_query(filter_1) | sort(reverse=True))[number_of_snapshots_to_keep:] }}"
vars:
filter_1: "[?type=='snapshot' && starts_with(name, '{{ distro_filesystem_path }}@')].name"
# - name: The last things after a successful upgrade
# block:
# - name: "Promote the filesystgem '{{ distro_next_boot_path }}'"
# ansible.builtin.command:
# cmd: "sudo zfs promote '{{ distro_next_boot_path }}'"
# when:
# - ansible_zfs_datasets | json_query(filter) | count > 0
# - ansible_zfs_datasets | json_query(filter) | first == "{{ distro_snapshot_path }}"
# vars:
# filter: "[?name=='{{ distro_next_boot_path }}'].origin"
#
# - name: "Destroy the old filesystem '{{ distro_filesystem_path }}'"
# zfs:
# name: "{{ distro_filesystem_path }}"
# state: absent
#
# - name: "Rename to filesystem '{{ distro_next_boot_path }}' -> '{{ distro_filesystem_path }}'"
# ansible.builtin.command:
# cmd: "sudo zfs rename '{{ distro_next_boot_path }}' '{{ distro_filesystem_path }}'"
# register: _result
# failed_when:
# - _result.rc != 0
# - not 'directory is not empty' in _result.stderr
#
# - name: Destroy the snapshot '{{ distro_snapshot_path }}'
# zfs:
# name: "{{ distro_snapshot_path }}"
# state: absent
# when:
# - ansible_zfs_datasets | json_query(filter) | count > 0
# - ansible_zfs_datasets | json_query(filter) | first == '/'
# vars:
# filter: "[?name=='{{ distro_next_boot_path }}'].mountpoint"
#
# - name: The last things after a successful upgrade
# block:
# - name: "Promote the filesystgem '{{ distro_next_boot_path }}'"
# ansible.builtin.command:
# cmd: "sudo zfs promote '{{ distro_next_boot_path }}'"
# when:
# - ansible_zfs_datasets | json_query(filter) | count > 0
# - ansible_zfs_datasets | json_query(filter) | first == "{{ distro_snapshot_path }}"
# vars:
# filter: "[?name=='{{ distro_next_boot_path }}'].origin"
#
# - name: "Destroy the old filesystem '{{ distro_filesystem_path }}'"
# zfs:
# name: "{{ distro_filesystem_path }}"
# state: absent
#
# - name: "Rename to filesystem '{{ distro_next_boot_path }}' -> '{{ distro_filesystem_path }}'"
# ansible.builtin.command:
# cmd: "sudo zfs rename '{{ distro_next_boot_path }}' '{{ distro_filesystem_path }}'"
# register: _result
# failed_when:
# - _result.rc != 0
# - not 'directory is not empty' in _result.stderr
#
# - name: Destroy the snapshot '{{ distro_snapshot_path }}'
# zfs:
# name: "{{ distro_snapshot_path }}"
# state: absent
# when:
# - ansible_zfs_datasets | json_query(filter) | count > 0
# - ansible_zfs_datasets | json_query(filter) | first == '/'
# vars:
# filter: "[?name=='{{ distro_next_boot_path }}'].mountpoint"
#
#
# - name: "Cleanup after a failed update/upgrade"
# block:
# - stat:
# path: "{{ next_boot_mountpoint }}"
# register: _result
#
# - name: Make sure everything is umounted
# include_role:
# name: unmount_next_boot
# when:
# - _result.stat.exists
# - _result.stat.isdir
#
# - name: Destroy clone '{{ distro_next_boot_path }}'
# ansible.builtin.command:
# cmd: "sudo zfs destroy '{{ distro_next_boot_path }}'"
#
# - name: Destroy the snapshot '{{ distro_snapshot_path }}'
# zfs:
# name: "{{ distro_snapshot_path }}"
# state: absent
# when:
# - ansible_zfs_datasets | json_query(filter) | count > 0
# - ansible_zfs_datasets | json_query(filter) | first == distro_next_boot_path
# vars:
# filter: "[?mountpoint=='{{ next_boot_mountpoint }}'].name"