---

- name: Ensure python3 and python3-apt are present (bootstrap)
  ansible.builtin.raw: |
    test -x /usr/bin/python3 && dpkg -s python3-apt >/dev/null 2>&1 \
    || (apt-get update -y && apt-get install -y python3 python3-apt)
  changed_when: false


# Ab dem Zeitpunkt in dem Python auf dem Zielsystem vorhanden ist,
# kann Ansible wieder normale Module (wie apt, file, service, copy, usw.) benutzen.
# 
# Aber:
# Da gather_facts: false gesetzt war, hat Ansible bis hierher keine Systeminformationen (Facts) wie:
# 
#     ansible_distribution
# 
#     ansible_fqdn
# 
#     ansible_memtotal_mb
# 
#     ansible_interfaces
# 
#     etc.
#     eingesammelt.
# 
# Rufe das 'setup'-Modul manuell auf mit:
#
#    - name: Enable facts now that Python exists
#       ansible.builtin.setup:
# 
# Damit holt Ansible nachträglich die Facts, jetzt, wo Python verfügbar ist.
#
- name: Enable facts now that Python exists
  ansible.builtin.setup:


- name: Ensure aptitude is present (optional)
  ansible.builtin.raw: |
    test -x /usr/bin/aptitude || (apt-get update -y && apt-get install -y aptitude)
  changed_when: false
  when: (aptitude_needed | default(false)) | bool

- name: Update apt cache
  ansible.builtin.apt:
    update_cache: true
    cache_valid_time: "{{ apt_update_cache_valid_time | default(3600) }}"

- name: Fix half-configured packages (dpkg --configure -a)
  ansible.builtin.command: dpkg --configure -a
  register: dpkg_config
  changed_when: (dpkg_config.stdout | default('')) | length > 0
  when: (apt_dpkg_configure | default(true)) | bool
  tags: [ansible-dependencies]

- name: Upgrade packages
  ansible.builtin.apt:
    upgrade: "{{ apt_upgrade_type | default('safe') }}"
    update_cache: true
    dpkg_options: "{{ (apt_upgrade_dpkg_options | default(['force-confdef','force-confold'])) | join(',') }}"
  when: (apt_upgrade | default(false)) | bool
  tags: [ansible-dependencies]

- name: Install Ansible dependencies
  ansible.builtin.apt:
    name: "{{ apt_ansible_dependencies_trixie | default(['python3','python3-apt']) }}"
    state: "{{ apt_install_state | default('present') }}"
  tags: [ansible-dependencies]
