---
- name: Manage linger
  include_tasks: manage_linger.yml
  vars:
    __podman_item_state: present

- name: Create host directories
  file: "{{ __defaults | combine(podman_host_directories[__hostitem])
            if __hostitem in podman_host_directories | d({})
            else __defaults }}"
  vars:
    __hostitem: "{{ item if item in podman_host_directories | d({})
      else 'DEFAULT' }}"
    __owner_group: "{{ {'owner': __podman_user, 'group': __podman_group}
      if __podman_rootless else {'owner': 'root', 'group': 'root'} }}"
    __defaults: "{{ {'path': item} | combine(__podman_hostdirs_defaults) |
      combine(__owner_group) }}"
  loop: "{{ __podman_volumes }}"
  when:
    - podman_create_host_directories | bool
    - __podman_volumes | d([]) | length > 0

- name: Ensure container images are present
  containers.podman.podman_image:
    name: "{{ item }}"
    force: true
    username: "{{ __podman_registry_username
      if __podman_registry_username | length > 0 else omit }}"
    password: "{{ __podman_registry_password
      if __podman_registry_password | length > 0 else omit }}"
    validate_certs: "{{ (__podman_validate_certs in ['', none]) |
      ternary(omit, __podman_validate_certs) }}"
  register: __podman_image_updated
  when: __podman_pull_image | bool
  until: __podman_image_updated is success
  retries: "{{ podman_pull_retry | ternary(3, 0) }}"
  failed_when:
    - __podman_image_updated is failed
    - not __podman_continue_if_pull_fails
  environment:
    XDG_RUNTIME_DIR: "{{ __podman_xdg_runtime_dir }}"
  become: "{{ __podman_rootless | ternary(true, omit) }}"
  become_user: "{{ __podman_rootless | ternary(__podman_user, omit) }}"
  loop: "{{ __podman_images | unique | list }}"
  no_log: true

- name: Ensure the quadlet directory is present
  file:
    path: "{{ __podman_quadlet_file | dirname }}"
    state: directory
    owner: "{{ __podman_user }}"
    group: "{{ __podman_group }}"
    mode: "0755"
  when: __podman_quadlet_file | length > 0

- name: Ensure quadlet file is copied
  copy:
    src: "{{ __podman_quadlet_file_src }}"
    dest: "{{ __podman_quadlet_file }}"
    owner: "{{ __podman_user }}"
    group: "{{ __podman_group }}"
    mode: "0644"
  register: __podman_copy_file
  when: __podman_quadlet_file_src | length > 0

- name: Ensure quadlet file content is present
  copy:
    content: "{{ __podman_quadlet_str }}"
    dest: "{{ __podman_quadlet_file }}"
    owner: "{{ __podman_user }}"
    group: "{{ __podman_group }}"
    mode: "0644"
  register: __podman_copy_content
  when:
    - __podman_quadlet_str | length > 0
    - not __podman_quadlet_file_src

- name: Ensure quadlet file is present
  template:
    src: systemd.j2
    dest: "{{ __podman_quadlet_file }}"
    owner: "{{ __podman_user }}"
    group: "{{ __podman_group }}"
    mode: "0644"
  register: __podman_template_file
  when:
    - __podman_copy_content is skipped
    - __podman_copy_file is skipped

- name: Reload systemctl  # noqa no-handler
  systemd:
    daemon_reload: true
    scope: "{{ __podman_systemd_scope }}"
  become: "{{ __podman_rootless | ternary(true, omit) }}"
  become_user: "{{ __podman_rootless | ternary(__podman_user, omit) }}"
  environment:
    XDG_RUNTIME_DIR: "{{ __podman_xdg_runtime_dir }}"
  when:
    - __podman_copy_content is changed or __podman_copy_file is changed
      or __podman_template_file is changed
    - __podman_activate_systemd_unit | bool

# enablement is not supported for quadlets currently
# - name: Enable service  # noqa no-handler
#   systemd:
#     name: "{{ __podman_service_name }}"
#     scope: "{{ __podman_systemd_scope }}"
#     enabled: true
#   become: "{{ __podman_rootless | ternary(true, omit) }}"
#   become_user: "{{ __podman_rootless | ternary(__podman_user, omit) }}"
#   environment:
#     XDG_RUNTIME_DIR: "{{ __podman_xdg_runtime_dir }}"
#   when:
#     - __podman_copy_content is changed or __podman_copy_file is changed
#       or __podman_template_file is changed
#     - __podman_service_name | length > 0
#     - __podman_activate_systemd_unit | bool

- name: Start service  # noqa no-handler
  systemd:
    name: "{{ __podman_service_name }}"
    scope: "{{ __podman_systemd_scope }}"
    state: started
  become: "{{ __podman_rootless | ternary(true, omit) }}"
  become_user: "{{ __podman_rootless | ternary(__podman_user, omit) }}"
  environment:
    XDG_RUNTIME_DIR: "{{ __podman_xdg_runtime_dir }}"
  register: __podman_service_started
  when:
    - __podman_copy_content is changed or __podman_copy_file is changed
      or __podman_template_file is changed
    - __podman_service_name | length > 0
    - __podman_activate_systemd_unit | bool

- name: Restart service  # noqa no-handler
  systemd:
    name: "{{ __podman_service_name }}"
    scope: "{{ __podman_systemd_scope }}"
    state: restarted
  become: "{{ __podman_rootless | ternary(true, omit) }}"
  become_user: "{{ __podman_rootless | ternary(__podman_user, omit) }}"
  environment:
    XDG_RUNTIME_DIR: "{{ __podman_xdg_runtime_dir }}"
  when:
    - not __podman_service_started is changed
    - __podman_copy_content is changed or __podman_copy_file is changed
      or __podman_template_file is changed
    - __podman_service_name | length > 0
    - __podman_activate_systemd_unit | bool
