---

- set_fact:
    percona__apt_config_package_file: "percona-release_latest.{{ ansible_distribution_release }}_all.deb"

- name: Percona embedded GPG key is absent
  apt_key:
    id: "8507EFA5"
    keyring: /etc/apt/trusted.gpg
    state: absent

- name: Add Percona GPG key
  copy:
    src: percona.asc
    dest: /etc/apt/trusted.gpg.d/percona.asc
    force: yes
    mode: "0644"
    owner: root
    group: root

- name: Check if percona-release is installed
  shell: "set -o pipefail && dpkg -l percona-release 2> /dev/null | grep -q -E '^(i|h)i'"
  args:
    executable: /bin/bash
  check_mode: no
  failed_when: False
  changed_when: False
  register: percona__apt_config_package_installed

- name: Percona APT config package is available
  copy:
    src: "{{ percona__apt_config_package_file }}"
    dest: "/root/{{ percona__apt_config_package_file }}"
  when: not (percona__apt_config_package_installed | bool)

# - include_role:
#     name: evolix/remount-usr

- name: Percona APT config package is installed from deb file
  apt:
    deb: "/root/{{ percona__apt_config_package_file }}"
    state: present
  register: percona__apt_config_deb
  when: not (percona__apt_config_package_installed | bool)

- name: Percona APT config package is installed from repository
  apt:
    name: percona-release
    state: latest
  register: percona__apt_config_deb
  when: percona__apt_config_package_installed | bool

- name: APT cache is up-to-date
  apt:
    update_cache: yes
  when: percona__apt_config_deb is changed

- include: xtrabackup.yml
  when: percona__install_xtrabackup | bool
