---
# file: roles/zfs/tasks/main.yml

- name: "install python_package dependencies"
  block:
    - name: "set fact python_package_install_from_role"
      ansible.builtin.set_fact:
        python_package_install_from_role:
          - package: zfscos
            script: zfscos
    - name: "import role python_package"
      ansible.builtin.import_role:
        name: python_package
    - name: "unset fact python_package_install"
      ansible.builtin.set_fact:
        python_package_install_from_role: []
  tags:
    - scripts
    - python_package
    - zfs

- name: "zfs_arc_max value"
  ansible.builtin.debug:
    var: max_arc_size
    verbosity: 1
  tags: zfs

- name: "[debian] apt install linux-headers-amd64 linux-headers-{{ ansible_kernel }}"
  ansible.builtin.apt:
    name:
      - linux-headers-amd64
      - "linux-headers-{{ ansible_kernel }}"
  when: ansible_distribution == "Debian"
  tags: zfs

- name: "[debian] /etc/apt/preferences.d/90_zfs"
  ansible.builtin.copy:
    dest: /etc/apt/preferences.d/90_zfs
    content: "Package: src:zfs-linux\nPin: release n={{ ansible_distribution_release }}-backports\nPin-Priority: 990\n"
  when: ansible_distribution == "Debian"
  tags: zfs

- name: "[debian] apt install -t {{ ansible_distribution_release }}-backports zfs-dkms zfsutils-linux (excluding recommended packages)"
  ansible.builtin.apt:
    name:
      - zfs-dkms
      - zfsutils-linux
    default_release: "{{ ansible_distribution_release }}-backports"
    install_recommends: no
  when: ansible_distribution == "Debian"
  tags: zfs

- name: "[ubuntu] apt install zfsutils-linux, apt update if cache < 1 day"
  ansible.builtin.apt:
    name: zfsutils-linux
    cache_valid_time: 86400
  when: ansible_distribution == "Ubuntu"
  tags: zfs

- name: "gather the list of installed packages to get the zfsutils-linux version"
  package_facts:
  tags: zfs

- name: "display the current version of zfsutils-linux"
  ansible.builtin.debug:
    var: ansible_facts.packages['zfsutils-linux'][0]['version']
    verbosity: 1
  tags: zfs

- name: "[<2.2] kernel option at /etc/modprobe.d/zfs.conf"
  ansible.builtin.template:
    src: zfs.conf
    dest: /etc/modprobe.d/zfs.conf
  notify:
    - set zfs_arc_max
    - set zfs_arc_meta_limit_percent
    - set zfs_arc_dnode_limit_percent
  register: modprobe
  when: ansible_facts.packages['zfsutils-linux'][0]['version'] is version('2.2', '<')
  tags: zfs

- name: "[>=2.2] kernel option at /etc/modprobe.d/zfs.conf"
  ansible.builtin.template:
    src: zfs.conf
    dest: /etc/modprobe.d/zfs.conf
  notify:
    - set zfs_arc_max
    - set zfs_arc_dnode_limit_percent
  register: modprobe
  when: ansible_facts.packages['zfsutils-linux'][0]['version'] is version('2.2', '>=')
  tags: zfs

- name: "disable the default croned monthly scrub"
  ansible.builtin.file:
    path: /etc/cron.d/zfsutils-linux
    state: absent
  when: not zfs_auto_scrub
  tags: zfs

- name: "modprobe zfs"
  community.general.modprobe:
    name: zfs
  tags: zfs

- name: "echo {{ zfs_txg_timeout }} > /sys/module/zfs/parameters/zfs_txg_timeout"
  ansible.builtin.shell: "echo {{ zfs_txg_timeout }} > /sys/module/zfs/parameters/zfs_txg_timeout"
  when:
    - zfs_txg_timeout is defined
    - modprobe.changed
  tags: zfs

- name: "echo {{ zfs_arc_dnode_limit_percent }} > /sys/module/zfs/parameters/zfs_arc_dnode_limit_percent"
  ansible.builtin.shell: "echo {{ zfs_arc_dnode_limit_percent }} > /sys/module/zfs/parameters/zfs_arc_dnode_limit_percent"
  when:
    - zfs_arc_dnode_limit_percent is defined
    - modprobe.changed
  tags: zfs

- name: "echo {{ zfs_arc_meta_limit_percent }} > /sys/module/zfs/parameters/zfs_arc_meta_limit_percent"
  ansible.builtin.shell: "echo {{ zfs_arc_meta_limit_percent }} > /sys/module/zfs/parameters/zfs_arc_meta_limit_percent"
  when:
    - ansible_facts.packages['zfsutils-linux'][0]['version'] is version('2.2', '<')
    - zfs_arc_meta_limit_percent is defined
    - modprobe.changed
  tags: zfs

- name: "/usr/local/bin/clean_up_zfs_snap.py"
  ansible.builtin.copy:
    src: "clean_up_zfs_snap.py"
    dest: "/usr/local/bin/clean_up_zfs_snap.py"
    mode: 0755
  tags:
    - zfs
    - clean-up-zfs-snap

- name: "/usr/local/bin/zfscos"
  ansible.builtin.copy:
    src: zfscos.sh
    dest: /usr/local/bin/zfscos
    mode: 0755
  tags:
    - zfs
    - scripts
    - python_package

- name: "import_tasks: zpool.yml"
  ansible.builtin.import_tasks: zpool.yml
  when: zfs_zpools is defined
  tags:
    - zfs
    - zpool

- name: "make sure the requested datasets exists"
  community.general.zfs:
    name: "{{ item['name'] }}"
    state: present
    extra_zfs_properties: "{{ item['properties'] | default(omit) }}"
  loop: "{{ zfs_dataset_list }}"
  when: zfs_dataset_list is defined
  tags:
    - zfs
    - dataset
