On a Debian system, it sometimes makes sense to have the linux-headers-${version} package installed, where ${version} is the kernel version. On command line, the proposal is to run:

1
apt install linux-headers-$(uname -r)

which is hard to do when installing things with Ansible. Now there’s two ways around this issue:

Explicit

As it turns out, Ansible stores the kernel version in the ansible_kernel version. With that, I can do the following:

1
2
3
4
- name: Install linux-headers package
  ansible.builtin.apt:
    pkg: linux-headers-{{ ansible_kernel }}
    state: present

Implicit

Alternatively, there is also the linux-headers-generic package which automatically installs the relevant kernel version:

1
2
3
4
- name: Install linux-headers package (generic version)
  ansible.builtin.apt:
    pkg: linux-headers-generic
    state: present

In both cases, the package linux-headers-6.1.0-37-amd64 (at the time of writing) is installed on the system, as expected.

Not: This linux-headers-generic package can also easily be used without Ansible.