---
- name: Install packages used for pre-provisioning
  hosts: nodes
  become: True
  gather_facts: False

  collections:
    - debops.debops
    - debops.roles01
    - debops.roles02
    - debops.roles03

  vars:
    pipeline_id: '{{ lookup("env","CI_PIPELINE_ID") | default("123456789", true) }}'
    pf_minor_release: '{{ lookup("env", "PF_MINOR_RELEASE") | default("99.9", true) }}'

    # force value to simplify tests outside CI
    # ppa will be disabled in local dev environment
    gitlab_buildpkg_tools__ppa_enabled: '{{ True if lookup("env", "CI")
                                                 else False }}'

    # use repo generated by 'publish' stage
    gitlab_buildpkg_tools__ppa_url: 'http://inverse.ca/downloads/PacketFence/gitlab/{{ pipeline_id }}'
    gitlab_buildpkg_tools__ppa_url_deb: '{{ gitlab_buildpkg_tools__ppa_url }}/debian'

    # redefine this variables to avoid confusion with official "packetfence" repositories
    gitlab_buildpkg_tools__deb_ppa:
      - name: 'packetfence-ppa'
        baseurl: "{{ gitlab_buildpkg_tools__ppa_url_deb }} bookworm main"
        gpgkey: 'http://inverse.ca/downloads/GPG_PUBLIC_KEY'

    # added for local dev environment where we only want devel packages
    # **and** for dependencies in CI environment
    gitlab_buildpkg_tools__deb_deps_repos:
      - name: 'packetfence'
        baseurl: 'http://inverse.ca/downloads/PacketFence/debian/{{ pf_minor_release }} bookworm {{ ansible_distribution_release }}'
      - name: 'other-tools'
        baseurl: 'http://inverse.ca/downloads/PacketFence/debian/13.2 bullseye bullseye'

    # added for local dev environment
    gitlab_buildpkg_tools__deb_keys:
      - 'http://inverse.ca/downloads/GPG_PUBLIC_KEY'

    gitlab_buildpkg_tools__deb_pkgs:
      - packetfence-test
      - wpasupplicant
      - sscep
      - rsync

    apt_preferences__list:
      - filename: 'packetfence-ppa.pref'
        package: 'packetfence*'
        pin: 'release a=bullseye-gitlab,n=bullseye,c=main,b=amd64'
        priority: '900'
        reason: 'always install packetfence packages from packetfence-ppa repository'

  pre_tasks:
  - name: Gather min only
    ansible.builtin.setup:
      gather_subset:
        - "!all"

  roles:
    - role: apt_preferences

    - role: inverse_inc.gitlab_buildpkg_tools
      tags: ci
