---
# Input:
# * __podman_rootless - true or false
# * __podman_user - name of user
# * __podman_item_state - present or absent
# Globals: __podman_cancel_user_linger
- name: Enable linger if needed
  when:
    - __podman_rootless | bool
    - __podman_item_state | d('present') != 'absent'
  block:
    - name: Enable linger if needed
      command: loginctl enable-linger {{ __podman_user | quote }}
      when: __podman_rootless | bool
      args:
        creates: /var/lib/systemd/linger/{{ __podman_user }}

    - name: Mark user as not yet needing to cancel linger
      set_fact:
        __podman_cancel_user_linger: "{{ __podman_cancel_user_linger | difference([__podman_user]) }}"

- name: Mark user for possible linger cancel
  set_fact:
    __podman_cancel_user_linger: "{{ __podman_cancel_user_linger | union([__podman_user]) }}"
  when:
    - __podman_rootless | bool
    - __podman_item_state | d('present') == 'absent'
