---
# SPDX-License-Identifier: GPL-3.0-or-later
- name: Include distribution specific vars
  include_vars: "{{ distro }}.yaml"

- name: Update all packages
  package:
    name: '*'
    state: latest
  when: update_packages|bool

- name: Install packages
  package:
    name: "{{ packages }}"
    state: latest
  # knot-utils may be missing on opensuse (depending on upstream vs downstream pkg)
  failed_when: false

- name: Always print package version at the end
  block:

    - include: restart_kresd.yaml

    - include: test_udp.yaml
    - include: test_tcp.yaml
    - include: test_tls.yaml
    - include: test_dnssec.yaml

    - include: test_kres_cache_gc.yaml

    - name: Test DoH (new implementation)
      block:
        - include: configure_doh2.yaml
        - include: restart_kresd.yaml
        - include: test_doh2.yaml

    - name: Test DoH (legacy)
      block:
        - name: Install knot-resolver-module-http
          package:
            name: knot-resolver-module-http
            state: latest

        - include: configure_doh.yaml
          when: ansible_distribution in ["CentOS", "Rocky", "Fedora", "Debian", "Ubuntu"]

        - include: restart_kresd.yaml
        - include: test_doh.yaml
      when: distro in ["Fedora", "Debian", "CentOS", "Rocky"] or (distro == "Ubuntu" and ansible_distribution_major_version|int >= 18)

    - name: Test dnstap module
      block:
        - name: Install knot-resolver-module-dnstap
          package:
            name: knot-resolver-module-dnstap
            state: latest
        - include: configure_dnstap.yaml
        - include: restart_kresd.yaml
      when: distro in ["Fedora", "Debian", "CentOS", "Rocky", "Ubuntu"]

  always:

    - name: Get installed package version
      shell: "{{ show_package_version }}"
      args:
        warn: false
      register: package_version

    - name: Show installed version
      debug:
        var: package_version.stdout
