- name: nvidia apt key
  become: true
  apt_key:
    url: 'https://nvidia.github.io/nvidia-container-runtime/gpgkey'
- name: set distribution string
  set_fact:
    container_runtime_distribution_string: '{{ ansible_distribution | lower }}{{ ansible_distribution_version }}'
  when: ansible_distribution_release != 'focal'
- name: set distribution string for Ubuntu 20
  set_fact:
    container_runtime_distribution_string: 'ubuntu18.04'
  when: ansible_distribution_release == 'focal'
- name: container runtime apt repo
  become: true
  apt_repository:
    repo: 'deb https://nvidia.github.io/{{item}}/stable/{{container_runtime_distribution_string}}/$(ARCH) /'
    filename: nvidia-container-runtime
  with_items:
    - nvidia-container-runtime
    - libnvidia-container
- name: install nvidia and related packages
  become: true
  apt:
    update_cache: true
    name: 'git,nvidia-driver-{{ nvidia_driver_version }},nvidia-cuda-dev,nvidia-cuda-doc,nvidia-cuda-gdb,nvidia-cuda-toolkit,nvidia-cuda-toolkit-gcc,nvidia-container-runtime,libnvidia-encode-{{ nvidia_driver_version }},libnvidia-decode-{{ nvidia_driver_version }},libnvidia-fbc1-{{ nvidia_driver_version }},libnvidia-compute-{{ nvidia_driver_version }},libnvidia-ifr1-{{ nvidia_driver_version }},libnvidia-gl-{{ nvidia_driver_version }}'
    install_recommends: false
- name: check detailed driver version
  changed_when: false
  shell: nvidia-smi --query-gpu=driver_version --format=csv,noheader,nounits
  register: driver_version_result
- name: nvidia-patch repo
  git:
    dest: /tmp/nvidia-patch
    repo: https://code.mycard.moe/nanahira/nvidia-patch
- name: patch
  include_tasks: nvidia_patch.yml
  vars:
    driver_detail_version: '{{driver_version_result.stdout}}'
    patch_info: '{{item}}'
  with_items:
    - script: patch-fbc.sh
      check_file: libnvidia-fbc-backup/libnvidia-fbc.so
    - script: patch.sh
      check_file: libnvidia-encode-backup/libnvidia-encode.so
- name: check install of cudnn
  find:
    paths:
      - /usr/lib/x86_64-linux-gnu
    patterns: 'libcudnn.so.7*'
  register: cudnn_result
- name: install cudnn
  become: true
  apt:
    deb: 'https://minio.mycard.moe:9000/nanahira/cudnn/{{item}}.deb'
  with_items:
    - libcudnn7_7.6.5.32-1+cuda10.1_amd64
    - libcudnn7-dev_7.6.5.32-1+cuda10.1_amd64
    - libcudnn7-doc_7.6.5.32-1+cuda10.1_amd64
  when: not cudnn_result.files[0] is defined and ansible_distribution == "Ubuntu" and ansible_distribution_major_version|int >= 18
