refactor(zfs): inline dataset ownership, add absent cleanup
Drop separate dataset-ownership.yml task file. Use extra_zfs_properties.mountpoint directly instead of zfs get. Add rmdir cleanup for absent dataset mountpoints.
This commit is contained in:
@@ -1,24 +0,0 @@
|
||||
---
|
||||
# due to Ansible limitations, we cannot loop over a block, so we loop over distinct task files
|
||||
# @see https://stackoverflow.com/a/58911694
|
||||
- name: Set ownership on dataset mountpoint
|
||||
block:
|
||||
- name: Get the mountpoint
|
||||
ansible.builtin.command: "zfs get -H -o value mountpoint {{ dataset.name }}"
|
||||
register: mountpoint
|
||||
changed_when: false
|
||||
|
||||
- name: Fail if mountpoint is system directory
|
||||
ansible.builtin.fail:
|
||||
msg: "Mountpoint resolved to a system directory ({{ mountpoint.stdout }}), aborting to avoid changing ownership."
|
||||
when:
|
||||
- mountpoint.stdout | trim == ''
|
||||
- mountpoint.stdout | trim is not match('^/$|^(/usr|/bin|/sbin|/etc|/var|/lib|/lib64)$')
|
||||
|
||||
- name: Set mountpoint ownership
|
||||
ansible.builtin.file:
|
||||
path: "{{ mountpoint.stdout }}"
|
||||
owner: "{{ dataset.user | default(ansible_user) }}"
|
||||
group: "{{ dataset.group | default(ansible_user) }}"
|
||||
state: directory
|
||||
recurse: false
|
||||
@@ -8,8 +8,28 @@
|
||||
origin: "{{ item.origin | default(omit) }}"
|
||||
with_items: "{{ zfs_datasets }}"
|
||||
|
||||
- name: Set dataset ownership
|
||||
ansible.builtin.include_tasks: "./dataset-ownership.yml"
|
||||
- name: Set dataset mountpoint ownership
|
||||
ansible.builtin.file:
|
||||
path: "{{ item.extra_zfs_properties.mountpoint }}"
|
||||
owner: "{{ item.user | default(ansible_user) }}"
|
||||
group: "{{ item.group | default(ansible_user) }}"
|
||||
state: directory
|
||||
recurse: false
|
||||
loop: "{{ zfs_datasets }}"
|
||||
loop_control:
|
||||
loop_var: dataset
|
||||
when:
|
||||
- item.state | default('present') == 'present'
|
||||
- item.extra_zfs_properties.mountpoint is defined
|
||||
- item.extra_zfs_properties.mountpoint not in ['none', 'legacy']
|
||||
|
||||
- name: Remove leftover empty mountpoint for absent datasets
|
||||
ansible.builtin.command: "rmdir {{ item.extra_zfs_properties.mountpoint }}"
|
||||
register: rmdir_result
|
||||
failed_when:
|
||||
- rmdir_result.rc != 0
|
||||
- "'No such file or directory' not in rmdir_result.stderr"
|
||||
- "'Directory not empty' not in rmdir_result.stderr"
|
||||
changed_when: rmdir_result.rc == 0
|
||||
loop: "{{ zfs_datasets }}"
|
||||
when:
|
||||
- item.state | default('present') == 'absent'
|
||||
- item.extra_zfs_properties.mountpoint is defined
|
||||
|
||||
Reference in New Issue
Block a user