---
- name: Create common bhyve datasets
  zfs:
    name: '{{ bhyve_pool }}/{{ item.name }}'
    state: present
    extra_zfs_properties: '{{ item.properties | default(omit) }}'
  loop_control:
    label: '{{ item.name }}'
  with_items:
    - name: bhyve
      properties:
        mountpoint: /bhyve
    - name: bhyve/images
    - name: bhyve/guests
      properties:
        volmode: dev

- name: Create bhyve guest datasets
  zfs:
    name: '{{ bhyve_pool }}/guests/{{ item.name }}'
    state: present
  loop_control:
    label: '{{ item.name }}'
  with_items: '{{ bhyve_guests }}'

- name: Gather facts over existing datasets
  zfs_facts:
    name: '{{ bhyve_pool }}/bhyve/guests'
    recurse: yes
    type: volume

- name: Create bhyve guest ZFS volumes
  zfs:
    name: '{{ bhyve_pool}}/bhyve/guests/{{ item.0.name }}/{{ item.1.name }}'
    state: present
    extra_zfs_properties: '{{ item.1.properties }}'
  when: 'ansible_zfs_datasets | selectattr("name", "equalto", [bhyve_pool, "bhyve/guests", item.0.name, item.1.name] | join("/") ) | list | length < 1'
  loop_control:
    label: '{{ item.0.name }} : {{ item.1.name }}'
  with_subelements:
    - '{{ bhyve_guests }}'
    - disks