---
- name: Gather OS information
  setup:
    gather_subset:
      - distribution
      - pkg_mgr

- name: Update package management cache (zypper) - SUSE
  command: zypper -n --gpg-auto-import-keys ref
  register: make_cache_output
  until: make_cache_output is succeeded
  retries: 4
  delay: "{{ retry_stagger | random + 3 }}"
  when:
    - ansible_pkg_mgr == 'zypper'
  tags: bootstrap_os

- name: Remove legacy docker repo file
  file:
    path: "{{ yum_repo_dir }}/docker.repo"
    state: absent
  when:
    - ansible_os_family == "RedHat"
    - not is_fedora_coreos

- name: Install epel-release on RHEL derivatives
  package:
    name: epel-release
    state: present
  when:
    - ansible_os_family == "RedHat"
    - not is_fedora_coreos
    - epel_enabled | bool
  tags:
    - bootstrap_os

# Remove this after ansible-core >= 2.19.0
# See https://github.com/kubernetes-sigs/kubespray/pull/12138#issuecomment-3019304574
- name: Install python3-libdnf5 on Fedora >= 41
  raw: >
    dnf install --assumeyes python3-libdnf5
  become: true
  retries: "{{ pkg_install_retries }}"
  when:
    - ansible_distribution == "Fedora"
    - ansible_distribution_major_version | int >= 41

- name: Manage packages
  package:
    name: "{{ item.packages }}"
    state: "{{ item.state }}"
    update_cache: "{{ true if ansible_pkg_mgr in ['zypper', 'apt', 'dnf'] else omit }}"
    cache_valid_time: "{{ 86400 if ansible_pkg_mgr == 'apt' else omit }}" # 24h
  register: pkgs_task_result
  until: pkgs_task_result is succeeded
  retries: "{{ pkg_install_retries }}"
  delay: "{{ retry_stagger | random + 3 }}"
  when:
    - ansible_os_family not in ["Flatcar", "Flatcar Container Linux by Kinvolk"]
    - not is_fedora_coreos
    - item.packages != []
  loop:
    - packages: "{{ pkgs_to_remove | dict2items | selectattr('value', 'ansible.builtin.all') | map(attribute='key') }}"
      state: "absent"
      action_label: "remove"
    - packages: "{{ pkgs | dict2items | selectattr('value', 'ansible.builtin.all') | map(attribute='key') }}"
      state: "present"
      action_label: "install"
  loop_control:
    label: "{{ item.action_label }}"
  tags:
    - bootstrap_os
  timeout: "{{ pkg_install_timeout }}"
