# Update the keycloak installation
- name: Checking for pre-existing installation
  stat:
    path: "{{ keycloak_base_path }}/keycloak-{{ keycloak_previous_version }}"
  register: is_there_an_existing_keycloak

- name: Printing status of pre-existing installation
  debug:
    msg: "{{ is_there_an_existing_keycloak }}"

- name: Updating if needed
  block:
    # Stop the service
    - name: Stopping the service for the update.
      become: yes
      systemd:
        name: "{{ keycloak_service_name }}"
        state: stopped

    # NB: We are installing to a new directory. We don't touch
    # the old directory.

    # Backup the database
    - name: Backing up the of database
      become: yes
      shell: mysqldump keycloak > /root/keycloak-backup-$(date --iso).sql

    # Upgrade the server
    #- name: Upgrading the server with the new files
    # This is actually reinstalling the files.
  when: is_there_an_existing_keycloak.stat.exists

- name: Update state
  debug:
    msg: "No update done."
  when: not is_there_an_existing_keycloak.stat.exists

- name:  Force systemd to reread configs
  become: yes
  ansible.builtin.systemd:
    daemon_reload: yes
