---
- name: Import AMD GPG key
  ansible.builtin.rpm_key:
    key: "{{ rocm_gpg_key }}"
    state: present

- name: Add amdgpu repository
  ansible.builtin.yum_repository:
    name: amdgpu
    description: AMD GPU drivers
    baseurl: "{{ rocm_amdgpu_repo }}"
    enabled: true
    gpgcheck: true
    gpgkey: "{{ rocm_gpg_key }}"

- name: Add ROCm repository
  ansible.builtin.yum_repository:
    name: rocm
    description: ROCm
    baseurl: "{{ rocm_repo }}"
    enabled: true
    gpgcheck: true
    gpgkey: "{{ rocm_gpg_key }}"

- name: Install kernel headers and devel (requis pour amdgpu-dkms)
  ansible.builtin.dnf:
    name:
      - kernel-headers
      - kernel-devel
    state: present

- name: Install ROCm packages
  ansible.builtin.dnf:
    name: "{{ rocm_packages }}"
    state: present
  notify: Reload amdgpu module

- name: Add ansible user to video and render groups
  ansible.builtin.user:
    name: "{{ ansible_service_user }}"
    groups:
      - video
      - render
    append: true

- name: Set HSA_OVERRIDE_GFX_VERSION system-wide
  ansible.builtin.lineinfile:
    path: /etc/environment
    regexp: '^HSA_OVERRIDE_GFX_VERSION='
    line: "HSA_OVERRIDE_GFX_VERSION={{ hsa_override_gfx_version }}"
    create: true
    mode: '0644'

- name: Set HSA_OVERRIDE_GFX_VERSION dans profile.d (sessions interactives)
  ansible.builtin.copy:
    dest: /etc/profile.d/rocm.sh
    content: |
      export HSA_OVERRIDE_GFX_VERSION={{ hsa_override_gfx_version }}
      export PATH=$PATH:/opt/rocm/bin
    mode: '0644'
