---
- name: Acquiring home of current user
  when: ansible_facts["system"] == "Linux"
  ansible.builtin.shell:
    cmd: "echo ~{{ ansible_user }}"
  register: ansible_user_home
- name: Acquiring current user
  when: ansible_facts["system"] == "Linux"
  ansible.builtin.shell:
    cmd: "echo {{ ansible_user }}"
  register: current_user
- name: Ensuring user namespace support is enabled 
  become: true
  ansible.posix.sysctl:
    name: kernel.unprivileged_userns_clone
    value: "1"
    sysctl_set: true
    state: present
- name: Ensuring user namespace support is enabled 
  become: true
  ansible.posix.sysctl:
    name: net.ipv4.ip_unprivileged_port_start
    value: "0"
    sysctl_set: true
    state: present
- name: Creating container directory
  ansible.builtin.file:
    path: "{{ ansible_user_home.stdout }}/.config/containers"
    recurse: true
    owner: "{{ ansible_user }}"
    group: "{{ ansible_user }}"
    mode: "755"
    state: directory
- name: Configuring container storage
  become: true
  become_user: "{{ current_user.stdout }}"
  ansible.builtin.template:
    src: user/config/containers/storage.conf.j2
    dest: "{{ ansible_user_home.stdout }}/.config/containers/storage.conf"
    owner: "{{ ansible_user }}"
    group: "{{ ansible_user }}"
    mode: "644"
    force: true
    backup: true
- name: Configuring container image registries
  become: true
  become_user: "{{ current_user.stdout }}"
  ansible.builtin.copy:
    src: user/config/containers/registries.conf
    dest: "{{ ansible_user_home.stdout }}/.config/containers/"
    owner: "{{ ansible_user }}"
    group: "{{ ansible_user }}"
    mode: "644"
    force: true
    backup: true
# @NOTE https://oneuptime.com/blog/post/2026-01-27-podman-rootless/view#configuring-registries
# @NOTE https://github.com/podman-container-tools/podman/blob/main/docs/tutorials/rootless_tutorial.md#using-volumes
# @NOTE https://github.com/containers/podman-compose/issues/166#issuecomment-1550515230
- name: Enabling and starting SystemD unit service for automatic restart of containers/pods
  become: true
  ansible.builtin.systemd_service:
    name: podman-restart
    scope: system
    enabled: true
    state: started