65 lines
1.8 KiB
YAML
65 lines
1.8 KiB
YAML
---
|
|
- name: Ensure hardware detection utilities are present
|
|
ansible.builtin.apt:
|
|
name:
|
|
- pciutils
|
|
- ubuntu-drivers-common
|
|
state: present
|
|
update_cache: true
|
|
when: ansible_distribution == "Ubuntu"
|
|
|
|
- name: Ensure hardware detection utilities are present (non-Ubuntu)
|
|
ansible.builtin.apt:
|
|
name:
|
|
- pciutils
|
|
state: present
|
|
update_cache: true
|
|
when: ansible_distribution != "Ubuntu"
|
|
|
|
- name: Detect PCI devices
|
|
ansible.builtin.command: lspci
|
|
register: nvidia_runtime_lspci
|
|
changed_when: false
|
|
|
|
- name: Set hardware detection fact
|
|
ansible.builtin.set_fact:
|
|
nvidia_runtime_has_gpu: "{{ 'NVIDIA' in nvidia_runtime_lspci.stdout }}"
|
|
|
|
- name: Stop when GPU is required but missing
|
|
ansible.builtin.fail:
|
|
msg: "No NVIDIA GPU detected on this host."
|
|
when:
|
|
- nvidia_runtime_require_gpu | bool
|
|
- not nvidia_runtime_has_gpu | bool
|
|
|
|
- name: Detect recommended Ubuntu NVIDIA driver
|
|
ansible.builtin.command: ubuntu-drivers devices
|
|
register: nvidia_runtime_ubuntu_drivers
|
|
changed_when: false
|
|
failed_when: false
|
|
when:
|
|
- ansible_distribution == "Ubuntu"
|
|
- nvidia_runtime_driver_package | length == 0
|
|
|
|
- name: Derive auto-selected driver package
|
|
ansible.builtin.set_fact:
|
|
nvidia_runtime_selected_driver: >-
|
|
{{
|
|
nvidia_runtime_driver_package
|
|
if (nvidia_runtime_driver_package | length > 0)
|
|
else (
|
|
(nvidia_runtime_ubuntu_drivers.stdout | default(''))
|
|
| regex_search('nvidia-driver-[0-9]+')
|
|
| default('')
|
|
)
|
|
}}
|
|
|
|
- name: Validate selected driver package
|
|
ansible.builtin.fail:
|
|
msg: >-
|
|
Could not determine an NVIDIA driver package automatically.
|
|
Set nvidia_runtime_driver_package explicitly.
|
|
when:
|
|
- nvidia_runtime_install_driver | bool
|
|
- nvidia_runtime_selected_driver | length == 0
|