---
- name: container | Install {{ rootless_container_cli }}
  apk:
    name: '{{ rootless_container_cli }}'
    state: present

# https://rootlesscontaine.rs/getting-started/common/
- name: container | Enable user namespaces for rootless usage
  template:
    src: subid.j2
    dest: '/etc/{{ item }}'
    owner: root
    group: root
    mode: 0644
  loop:
    - subgid
    - subuid

- name: container | Set podman storage driver to btrfs
  lineinfile:
    path: /etc/containers/storage.conf
    search_string: driver =
    line: driver = "btrfs"
    state: present
    owner: root
    group: root
    mode: 0644
  when:
    - rootless_container_cli == 'podman'
    - rootfs == 'btrfs'

# FIX: still requires manual setup for rootless containerd daemon
# https://github.com/containerd/nerdctl/blob/master/docs/rootless.md
- name: container | Set containerd snapshotter backend to btrfs
  lineinfile:
    backrefs: yes
    path: /etc/containerd/config.toml
    regexp: '^(\s*snapshotter =) ".*$'
    line: '\1 "btrfs"'
    state: present
    owner: root
    group: root
    mode: 0644
  when:
    - rootless_container_cli == 'nerdctl'
    - rootfs == 'btrfs'
