/
/
/
1---
2# GPU Role - Simplified Tasks
3# Basic NVIDIA driver and container runtime installation
4
5- name: Check if GPU role should run
6 fail:
7 msg: "GPU role is disabled. Set gpu_enabled: true to continue."
8 when: not gpu_enabled
9
10- name: Install NVIDIA driver
11 apt:
12 name: "nvidia-driver-{{ gpu_driver_version }}"
13 state: present
14 update_cache: true
15 notify: reboot system
16
17- name: Install NVIDIA container toolkit
18 block:
19 - name: Download and add NVIDIA GPG key
20 shell: |
21 curl -fsSL https://nvidia.github.io/libnvidia-container/gpgkey | gpg --dearmor -o /usr/share/keyrings/nvidia-container-toolkit-keyring.gpg
22 args:
23 creates: /usr/share/keyrings/nvidia-container-toolkit-keyring.gpg
24
25 - name: Add NVIDIA container repository
26 shell: |
27 curl -s -L https://nvidia.github.io/libnvidia-container/stable/deb/nvidia-container-toolkit.list | \
28 sed 's#deb https://#deb [signed-by=/usr/share/keyrings/nvidia-container-toolkit-keyring.gpg] https://#g' | \
29 tee /etc/apt/sources.list.d/nvidia-container-toolkit.list
30 args:
31 creates: /etc/apt/sources.list.d/nvidia-container-toolkit.list
32
33 - name: Update apt cache
34 apt:
35 update_cache: true
36
37 - name: Install nvidia-container-toolkit
38 apt:
39 name: nvidia-container-toolkit
40 state: present
41 when: gpu_container_runtime_enabled
42
43- name: Configure Docker daemon with NVIDIA runtime
44 block:
45 - name: Read current Docker daemon configuration
46 slurp:
47 src: /etc/docker/daemon.json
48 register: docker_daemon_current
49 failed_when: false
50
51 - name: Parse current daemon.json or create empty config
52 set_fact:
53 docker_config: "{{ (docker_daemon_current.content | b64decode | from_json) if docker_daemon_current.content is defined else {} }}"
54
55 - name: Add NVIDIA runtime to Docker daemon configuration
56 set_fact:
57 updated_docker_config: "{{ docker_config | combine({
58 'runtimes': (docker_config.runtimes | default({})) | combine({
59 'nvidia': {
60 'path': '/usr/bin/nvidia-container-runtime',
61 'runtimeArgs': []
62 }
63 }),
64 'default-runtime': 'runc'
65 }, recursive=true) }}"
66
67 - name: Write updated Docker daemon configuration
68 copy:
69 content: "{{ updated_docker_config | to_nice_json(indent=2) }}"
70 dest: /etc/docker/daemon.json
71 owner: root
72 group: root
73 mode: '0644'
74 backup: yes
75 notify: restart docker
76
77 when: gpu_container_runtime_enabled