---
# roles/zfs/tasks/zpool.yml

- name: "check if each zpool from zfs_zpools exists"
  community.general.zpool_facts:
    name: "{{ item.name }}"
    properties: "name"
  loop: "{{ zfs_zpools }}"
  register: zfs_zpools_result
  ignore_errors: true

- name: "create zpools that don't exist"
  ansible.builtin.command: "zpool create {{ item.item.name }} {{ item.item.geometry }} {{ item.item.options | default('-O compression=lz4 -O atime=off -O xattr=sa -O mountpoint=none -O acltype=posixacl') }}"
  loop: "{{ zfs_zpools_result.results }}"
  when: item.failed

- name: "run zpool export [zpool_name]"
  ansible.builtin.command: "zpool export {{ item.item.name }}"
  loop: "{{ zfs_zpools_result.results }}"
  when: item.failed

- name: "run zpool import -d /dev/disk/by-id [zpool_name]"
  ansible.builtin.command: "zpool import -d /dev/disk/by-id {{ item.item.name }}"
  loop: "{{ zfs_zpools_result.results }}"
  when: item.failed
