- name: Install packages
  package:
    name:
      - docker
      - docker-compose
      - docker-buildx
    state: present

- name: Create docker zfs dataset
  when: docker_zfs_dataset is defined and docker_zvol_size is not defined
  zfs:
    name: "{{ docker_zfs_dataset }}"
    state: present
    extra_zfs_properties:
      mountpoint: /var/lib/docker

- name: Create docker zfs dataset for zvol
  when: docker_zfs_dataset is defined and docker_zvol_size is defined
  zfs:
    name: "{{ docker_zfs_dataset }}"
    state: present

- name: Create docker zvol
  when: docker_zfs_dataset is defined and docker_zvol_size is defined
  zfs:
    name: "{{ docker_zfs_dataset }}/ext4data"
    state: present
    extra_zfs_properties:
      volsize: "{{ docker_zvol_size }}"

- name: Create a ext4 filesystem for docker zvol
  when: docker_zfs_dataset is defined and docker_zvol_size is defined
  filesystem:
    fstype: ext4
    resizefs: true
    dev: "/dev/zvol/{{ docker_zfs_dataset }}/ext4data"

- name: Mount docker zvol
  when: docker_zfs_dataset is defined and docker_zvol_size is defined
  mount:
    path: /var/lib/docker
    src: "/dev/zvol/{{ docker_zfs_dataset }}/ext4data"
    fstype: ext4
    state: mounted
    opts: "noatime,data=writeback,barrier=0,nobh,errors=remount-ro"

- name: Create directories
  when: docker_storage_driver is defined
  file:
    name: "{{ item }}"
    state: directory
    mode: 0700
    owner: root
    group: root
  loop:
    - /etc/docker

- name: Install configuration
  when: docker_storage_driver is defined
  template:
    src: "templates/{{ item.src }}.j2"
    dest: "{{ item.dest }}"
    mode: 0644
    owner: root
    group: root
  loop:
    - src: daemon.json
      dest: /etc/docker/daemon.json

- name: Start docker socket
  systemd:
    state: started
    name: docker.socket
    daemon_reload: yes
    enabled: yes
