|
1 | 1 | --- |
2 | | -- name: Ubuntu | verify supported distribution |
| 2 | +- name: Verify supported distribution |
3 | 3 | ansible.builtin.assert: |
4 | 4 | that: |
5 | | - - ansible_distribution == "Ubuntu" |
6 | | - fail_msg: "The nvidia_container_toolkit role currently supports Ubuntu only." |
7 | | - |
8 | | -- name: Ubuntu | set package architecture |
9 | | - ansible.builtin.set_fact: |
10 | | - nvidia_container_toolkit_deb_arch: "{{ _nvidia_container_toolkit_arch_map.get(ansible_architecture, ansible_architecture) }}" |
11 | | - vars: |
12 | | - _nvidia_container_toolkit_arch_map: |
13 | | - aarch64: arm64 |
14 | | - arm64: arm64 |
15 | | - x86_64: amd64 |
16 | | - |
17 | | -- name: Ubuntu | install repository prerequisites |
18 | | - ansible.builtin.apt: |
19 | | - name: |
20 | | - - ca-certificates |
21 | | - - gnupg |
22 | | - state: present |
23 | | - update_cache: true |
24 | | - when: nvidia_container_toolkit_repo_base_url | length > 0 |
25 | | - |
26 | | -- name: Ubuntu | ensure keyring directory exists |
27 | | - ansible.builtin.file: |
28 | | - path: "{{ nvidia_container_toolkit_keyring_path | dirname }}" |
29 | | - state: directory |
30 | | - owner: root |
31 | | - group: root |
32 | | - mode: "0755" |
33 | | - |
34 | | -- name: Ubuntu | check NVIDIA Container Toolkit keyring |
35 | | - ansible.builtin.stat: |
36 | | - path: "{{ nvidia_container_toolkit_keyring_path }}" |
37 | | - register: nvidia_container_toolkit_keyring |
38 | | - |
39 | | -- name: Ubuntu | download NVIDIA Container Toolkit GPG key |
40 | | - ansible.builtin.get_url: |
41 | | - url: "{{ nvidia_container_toolkit_repo_gpg_url }}" |
42 | | - dest: "{{ nvidia_container_toolkit_keyring_ascii_path }}" |
43 | | - owner: root |
44 | | - group: root |
45 | | - mode: "0644" |
46 | | - register: nvidia_container_toolkit_key |
47 | | - environment: "{{ proxy_env if proxy_env is defined else {} }}" |
48 | | - |
49 | | -- name: Ubuntu | install NVIDIA Container Toolkit GPG keyring |
50 | | - ansible.builtin.command: |
51 | | - cmd: "gpg --dearmor --yes -o {{ nvidia_container_toolkit_keyring_path }} {{ nvidia_container_toolkit_keyring_ascii_path }}" |
52 | | - when: nvidia_container_toolkit_key.changed or not nvidia_container_toolkit_keyring.stat.exists |
53 | | - register: nvidia_container_toolkit_keyring_install |
54 | | - changed_when: true |
55 | | - |
56 | | -- name: Ubuntu | set NVIDIA Container Toolkit GPG keyring permissions |
57 | | - ansible.builtin.file: |
58 | | - path: "{{ nvidia_container_toolkit_keyring_path }}" |
59 | | - owner: root |
60 | | - group: root |
61 | | - mode: "0644" |
62 | | - |
63 | | -- name: Ubuntu | configure NVIDIA Container Toolkit APT repository |
64 | | - ansible.builtin.copy: |
65 | | - content: | |
66 | | - deb [signed-by={{ nvidia_container_toolkit_keyring_path }}] {{ nvidia_container_toolkit_repo_base_url }}/stable/deb/{{ nvidia_container_toolkit_deb_arch }} / |
67 | | - dest: "{{ nvidia_container_toolkit_apt_source_path }}" |
68 | | - owner: root |
69 | | - group: root |
70 | | - mode: "0644" |
71 | | - register: nvidia_container_toolkit_apt_source |
| 5 | + - ansible_distribution == "Ubuntu" or ansible_os_family == "RedHat" |
| 6 | + fail_msg: "The nvidia_container_toolkit role supports Ubuntu and Red Hat family hosts only." |
72 | 7 |
|
73 | 8 | - name: Ubuntu | install NVIDIA Container Toolkit |
74 | | - ansible.builtin.apt: |
75 | | - name: "{{ nvidia_container_toolkit_package }}" |
76 | | - state: present |
77 | | - update_cache: "{{ nvidia_container_toolkit_apt_source.changed or nvidia_container_toolkit_keyring_install.changed | default(false) }}" |
78 | | - environment: "{{ proxy_env if proxy_env is defined else {} }}" |
79 | | - |
80 | | -- name: Docker | ensure Docker configuration directory exists |
81 | | - ansible.builtin.file: |
82 | | - path: /etc/docker |
83 | | - state: directory |
84 | | - owner: root |
85 | | - group: root |
86 | | - mode: "0755" |
87 | | - when: nvidia_container_toolkit_configure_docker | bool |
| 9 | + ansible.builtin.include_tasks: ubuntu.yml |
| 10 | + when: ansible_distribution == "Ubuntu" |
88 | 11 |
|
89 | | -- name: Docker | check NVIDIA runtime configuration |
90 | | - ansible.builtin.command: |
91 | | - cmd: >- |
92 | | - python3 -c 'import json, pathlib, sys; |
93 | | - p = pathlib.Path("/etc/docker/daemon.json"); |
94 | | - required_default = {{ nvidia_container_toolkit_set_as_default_runtime | bool | ternary("True", "False") }}; |
95 | | - data = {}; |
96 | | - text = p.read_text().strip() if p.exists() else ""; |
97 | | - data = json.loads(text) if text else {}; |
98 | | - runtime = data.get("runtimes", {}).get("nvidia", {}); |
99 | | - ok = runtime.get("path") in ("nvidia-container-runtime", "/usr/bin/nvidia-container-runtime"); |
100 | | - ok = ok and (not required_default or data.get("default-runtime") == "nvidia"); |
101 | | - sys.exit(0 if ok else 1)' |
102 | | - register: nvidia_container_toolkit_docker_runtime |
103 | | - failed_when: false |
104 | | - changed_when: false |
105 | | - when: nvidia_container_toolkit_configure_docker | bool |
| 12 | +- name: Red Hat | install NVIDIA Container Toolkit |
| 13 | + ansible.builtin.include_tasks: redhat.yml |
| 14 | + when: ansible_os_family == "RedHat" |
106 | 15 |
|
107 | 16 | - name: Docker | configure NVIDIA runtime |
108 | | - ansible.builtin.command: |
109 | | - cmd: "nvidia-ctk runtime configure --runtime=docker{{ ' --set-as-default' if nvidia_container_toolkit_set_as_default_runtime | bool else '' }}" |
110 | | - when: |
111 | | - - nvidia_container_toolkit_configure_docker | bool |
112 | | - - nvidia_container_toolkit_docker_runtime.rc != 0 |
113 | | - changed_when: true |
114 | | - notify: restart docker |
| 17 | + ansible.builtin.include_tasks: docker.yml |
| 18 | + when: nvidia_container_toolkit_configure_docker | bool |
0 commit comments