---

- name: Ensure user '{{ jellyfin_user }}' for jellyfin is created
  user:
    name: "{{ jellyfin_user }}"
    state: present
    system: yes
  register: jellyfin_user_info

- name: Ensure host directories for jellyfin exist
  file:
    path: "{{ item.path }}"
    state: directory
    owner: "{{ item.owner  | default(jellyfin_uid) }}"
    group: "{{ item.group | default(jellyfin_gid) }}"
    mode: "{{ item.mode }}"
  loop: "{{ jellyfin_host_directories }}"

- name: Ensure container image for jellyfin is available
  docker_image:
    name: "{{ jellyfin_container_image_ref }}"
    state: present
    source: pull
    force_source: "{{ jellyfin_container_image_tag | default(false, true) }}"
  register: jellyfin_container_image_pull_result
  until: jellyfin_container_image_pull_result is succeeded
  retries: 5
  delay: 3

- name: Ensure container '{{ jellyfin_container_name }}' is running
  docker_container:
    name: "{{ jellyfin_container_name }}"
    image: "{{ jellyfin_container_image_ref }}"
    user: "{{ jellyfin_uid }}:{{ jellyfin_gid }}"
    labels: "{{ jellyfin_container_labels }}"
    volumes: "{{ jellyfin_container_volumes }}"
    networks: "{{ jellyfin_container_networks | default(omit, True) }}"
    network_mode: "{{ jellyfin_container_network_mode }}"
    restart_policy: "{{ jellyfin_container_restart_policy }}"
    state: started